Testing the availability of an smtp mail server using c# - c#

I have an MVC3 web application written in C#. I am using McvMailer to send emails from within the application, which is all working fine.
The issue i have is that our smtp server is not anywhere near the web server that will be hosting the application, and that it is possible that on occasion the smtp server may not be available.
I need to be able to detect whether or not the smtp server is available before giving a user the option to send emails. Thus far the best i have come up with is to ping the smtp server using the solution from #John Leidegren answer in this post.
However, it appears that the router is responding to the ping and not the smtp server. Baring in mind that the main cause for the server not being available would be the internet connection failing, i could probably live with this solution if i had to, but it would be nice if i could ensure the availability of the smtp service prior to attempting to send emails.
Any ideas?

Yes, ping is a good start, but to determine if the SMTP gateway is available, issue the HELO command through the SMTP port.
I'll try to find a good example.
Edit:
You're in luck. Here's a complete TELNET client written in C#. https://www.nuget.org/packages/Telnet

Related

Configure IIS to receive email

According to this answer it is possible to configure IIS to receive email. That is what I would like to do. But the answer says it's done under IIS SMTP under domains.
I connected to my website (on a web host) using IIS Manager and the only SMTP option I see is SMTP E-mail under the ASP.NET heading. (I have one more heading which is IIS, but doesn't have any SMTP under it.)
Clicking on SMTP E-mail shows me an option to "Use this feature...when sending e-mail...". and it has a textbox for E-mail address and radio buttons to choose from Deliver e-mail to SMTP server and Store e-mail in pickup directory.
I don't see any option for allowing it to receive emails.
So where is the option to receive emails?
(If you have a different way to receive emails without any action taken on them, I'd be glad to hear about it. I want to process the email regardless of the address it's being sent to as long as it's to my domain.)
You need the IIS 6 manager for that - even if you have IIS 7-8-9-10 installed. The SMTP part is still only in IIS 6 :(
It's not problem to run them side-by-side and you will use IIS 6 manager only for SMTP
With this option you could send and receive emails.
The IIS 6 manager looks like this:
And with you Windows system you need to install the SMTP server:
which give you IIS 6 (SMTP only)
See also https://www.interserver.net/tips/kb/how-to-setup-and-configure-smtp-server-on-windows-server-2008-r2/
And http://www.vsysad.com/2017/05/install-and-configure-smtp-server-on-windows-server-2016/
and see also How to configure SMTP in IIS 7?
I have a scenario where I wanted my web application to accept inbound emails and process them. I didn't find the SMTP server that comes with IIS to be helpful.
What ended up being a much better solution for receiving mail messages for me was to use an inbound email parsing service. You configure a domain's MX record to point to their (parsing service provider) mail server and their mail server parses the message and Posts to a page on your web server. My ASP.Net MVC Controller Action looks like this:
public ActionResult InboundMessage(FormCollection collection, HttpPostedFileBase attachment1, string attachments, string to, string from)
They get the email and call my code. I don't have to worry about any of the SMTP details. I've been using this for several years and it's worked great for me.
I'm not sure how recommending a specific product goes over around here, but if you Google "inbound email parser", you'll find several options.
If you want to go the original route, it is possible to receive inbound mail. It will write incoming messages to the "Drop" folder. (If you Google "IIS SMTP Drop folder", you will find information about how to do this.)
Before I found the Inbound Parse service, I ended up cobbling my own mail server together from some code on found on GitHub, but even then, parsing the parts of a message was a much bigger undertaking than I was expecting.

ASP.NET not sending emails to my own domain

I have two servers one linux server running cpanel/whm which act as a mail server and DNS Server for my domain and I host the website for the same domain on a different windows web server.
I have set up windows server with SMTP service to be able to send emails from my ASP.NET application.
Everything is going OK and my application sends emails correctly everywhere except to my own domain itself. If I sent and email to an email address like this somename#mydomain.com it is never delivered although sending emails using the same application to any domain succeeds.
Could you help me what should be the reason for this problem?
Regards,
Ehab Zaky
This is most likely caused by a setting on your SMTP Server itself.
Most smtp servers by default will not accept traffic if it claims to be from a local domain but is not a valid local user. So the first thing I would try is send the email from something that is a real working email address configured on your SMTP server. If that works, then you know what the problem is and can choose your course of action from there.
If that doesn;t work, then I would check the security settings of your SMTP server and look for limitations / restrictions regarding local domains. I am not a Linux user, but this seems to me to be whats probably happening.
Dave

Sending an email in C#

Is it possible to send an email in C# console, without needing and SMTP server?
Edit:
Why do I need another SMTP server? Can not I use my localhost machine as a server..?
Edit:
I just need to send an email from with my domain name, for example abc#mydomain.com
Is that possible? what do I need to do this in my C# program... I do not care about receiving emails, I just care about sending them....
Thanks
You don't have to depend on a local SMTP server if you don't have one. However, you will have to connect to a SMTP server anyway. Here is why.
You must achieve the following steps:
Determine what is the mail exchange servers of a given domain.
Connect to that mail exchange server and deliver your mail.
Those steps are normally done by your local SMTP server. Another advantage of your local SMTP server is that it will handle its queue and continue to try to deliver your email if it fail.
How to determine the MX records of a give domain.
I suggest you to have a look at this answer. Basically, you have to do a query on a DNS server to get the list of MX records of the domain name of the email address you want to send an email to.
How to connect to a mail exchange server
Well the answer will disappoint you. Exactly like you connect to your local SMTP server. Using the TcpClient, you connect to one of the mail exchange server you got at the previous step on port 25 and start the delivering process using the SMTP protocol.
The trick here is that you must handle multiple MX servers. They are usually listed with a preference. If the first one is unreachable, you try the next one and so on...
That is something your SMTP server can handle for you too.
If you really want to build that logic yourself, please have a look at the DirectSend method of the SmtpClient class of this open source project I'm involved in.
As #TomTom points out, the entire mail infrastructure depends on SMTP. What you can do is to skip the output (relaying) SMTP server and send the message directly to the receiving SMTP server.
To do that you need to create some kind of queuing mechanism (there is no guarantee that the receiving SMTP server can serve you when you try to connect) and that you can look it up.
MX records are entries stored in DNS servers and are used to find SMTP servers. You can find a article here with a MX lookup example: http://www.codeproject.com/KB/IP/dnslookupdotnet.aspx
However, I DO recommend that you install a local SMTP server and let it take care of the above mentioned issues.
Yes, Basically figure out where to sent the email and send it. i.e. a DNS MX lookup for the domain to find out the SMTP server.
Every email needs an SMTP server on the receiving side.
You can use gmail or yahoo SMTP server, if you don't want to install your own.
Before sending mail you first need to authenticate, otherwise sending mail is not going to possible.
You need access to some kind of email server to send your email, and your email will most likely pass through one or more SMTP servers on it's way to the recipient. However, the email server you connect to might let you send the email without using SMTP. For example, Exchange might let you use MAPI or CDO to send emails. Though I think that CDO is not officially supported by .Net and simple MAPI is deprecated in Windows and should not be used. You might be able to use Exchange Web Services as described here: Introducing the Exchange Web Services Managed API 1.0
If you have another email server than Microsoft Exchange, that server might have some kind of API you can use.
Something I do often is to create gmail account and send through that account.
You just need your SmtpClient to connect to the host smtp.gmail.com on port 587 with the username, password, and enableSSL property set to true.

Creating an IMAP client application in Silverlight

I'd like to make a stripped down email client for my pre-schooler using Silverlight 3 and pulling email from a Gmail account.
I'll have some filters setup in Gmail so that only a subset of email is given a particular label, similar to creating a whitelist. Then, I'd like to pull those emails with that label to the Silverlight client. I'd like to avoid running any of the messages through the server (so that I can share this application with friends and not have their email app require a server).
I've never written any sort of email client (POP3 or IMAP) and am not sure if this will even be possible. Looking through the various libraries available for retrieving via IMAP, I can't find references to using a browser-limited client such as Silverlight.
Also, I'm guessing I'll be able to send via .NET built in SMTP objects in Silverlight, but haven't tested this yet either.
Can anyone point me in the right direction; tell me why this may or may not be feasible; or relate their own experiences regarding this type of challenge?
Silverlight does not yet allow arbitrary socket connections, which you would need to connect to an IMAP server on the privileged port of 143. Silverlight can only connect to servers, even with a client access policy file, on ports 4502-4534.
Your only options are to proxy to gmail via a server on those ports, or just do the IMAP work on the server and serve it down to the client app over HTTP.
Sorry about this-- enhanced socket support is always being looked at, but it has scary security implications and hasn't been implemented yet. Good luck finding a solution to your scenario.
There is a great example of a Silverlight based mail client here:
http://silvermail.com.au
I use this regularly to check my personal email from work, and I know that it works with GMail.
Hope that helps.

Sending mail without installing an SMTP server

I have a .Net application. I want this application to send an email to me. How do I implement this without installing an SMTP server?
Using an SmtpClient to send a MailMessage does not require you to have a server on your local machine.
Your e-mail service provider is the one with the server (e.g. smtp.gmail.com), and your SmtpClient talks to it.
This article by Peter Bromberg on eggheadcafe.com
C# SMTP Mail without SMTP Service or CDO
explains how to send email without relying on an SMTP client:
Sending email via TCP using the native
SMTP RFC commands "HELO", "MAIL From",
RCPT TO", etc. is no big deal. That's
one of the first tricks we learn with
Telnet. Finding or writing managed
code that will do so reliably is
another story. The code in the class
that follows is not my original code -
I've cobbled it together from three
different sample sources, fixing
namespaces, error handling, and other
minor items, changing console code to
class library code, and providing a
complete Winforms - based test harness
front end that illustrates its correct
usage.
I've also included sample code
to correctly process and add a mail
attachment via an OpenFileDialog here.
This code MIME encodes and transmits
the attachment(s) according to the
specification.
You can't send email without the services of a SMTP server, there is of course no need for you to install one, just point your code at your ISPs SMTP server or your companies Exchange server (or what ever they use).

Categories