Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send emails through SSL SMTP with the .NET Framework?

Tags:

c#

.net

email

ssl

smtp

Is there a way with the .NET Framework to send emails through an SSL SMTP server on port 465?

The usual way:

System.Net.Mail.SmtpClient _SmtpServer = new System.Net.Mail.SmtpClient("tempurl.org"); _SmtpServer.Port = 465; _SmtpServer.EnableSsl = true; _SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password"); _SmtpServer.Timeout = 5000; _SmtpServer.UseDefaultCredentials = false;  MailMessage mail = new MailMessage(); mail.From = new MailAddress(from); mail.To.Add(to); mail.CC.Add(cc); mail.Subject = subject; mail.Body = content; mail.IsBodyHtml = useHtml; _SmtpServer.Send(mail); 

times out:

System.Net Verbose: 0 : [1024] SmtpClient::.ctor(host=ssl0.ovh.net, port=465) System.Net Information: 0 : [1024] Associating SmtpClient#64923656 with SmtpTransport#44624228 System.Net Verbose: 0 : [1024] Exiting SmtpClient::.ctor()  -> SmtpClient#64923656 System.Net Information: 0 : [1024] Associating MailMessage#17654054 with Message#52727599 System.Net Verbose: 0 : [1024] SmtpClient#64923656::Send(MailMessage#17654054) System.Net Information: 0 : [1024] SmtpClient#64923656::Send(DeliveryMethod=Network) System.Net Information: 0 : [1024] Associating SmtpClient#64923656 with MailMessage#17654054 System.Net Information: 0 : [1024] Associating SmtpTransport#44624228 with SmtpConnection#14347911 System.Net Information: 0 : [1024] Associating SmtpConnection#14347911 with ServicePoint#51393439 System.Net.Sockets Verbose: 0 : [1024] Socket#26756241::Socket(InterNetwork#2) System.Net.Sockets Verbose: 0 : [1024] Exiting Socket#26756241::Socket()  System.Net.Sockets Verbose: 0 : [1024] Socket#23264094::Socket(InterNetworkV6#23) System.Net.Sockets Verbose: 0 : [1024] Exiting Socket#23264094::Socket()  System.Net.Sockets Verbose: 0 : [1024] Socket#26756241::Connect(20:465#337754884) System.Net.Sockets Verbose: 0 : [1024] Exiting Socket#26756241::Connect()  System.Net.Sockets Verbose: 0 : [1024] Socket#23264094::Close() System.Net.Sockets Verbose: 0 : [1024] Socket#23264094::Dispose() System.Net.Sockets Verbose: 0 : [1024] Exiting Socket#23264094::Close()  System.Net Information: 0 : [1024] Associating SmtpConnection#14347911 with SmtpPooledStream#14303791 System.Net.Sockets Verbose: 0 : [1024] Socket#26756241::Receive() System.Net.Sockets Verbose: 0 : [2404] Socket#26756241::Dispose() System.Net.Sockets Error: 0 : [1024] Exception in the Socket#26756241::Receive - A blocking operation was interrupted by a call to WSACancelBlockingCall System.Net.Sockets Verbose: 0 : [1024] Exiting Socket#26756241::Receive()   -> 0#0 System.Net Error: 0 : [1024] Exception in the SmtpClient#64923656::Send - Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall. System.Net Error: 0 : [1024]    at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)    at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)    at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)    at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)    at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)    at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)    at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)    at System.Net.Mail.SmtpClient.GetConnection()    at System.Net.Mail.SmtpClient.Send(MailMessage message) System.Net Verbose: 0 : [1024] Exiting SmtpClient#64923656::Send()  System.Net Information: 0 : [1024] Associating MailMessage#49584532 with Message#19699911 

I googled around and found that System.Net.Mail supports connections on port 587 (default port for Explicit SSL that starts unencrypted then issues a STARTDLS then switches to an Encrypted connection: RFC 2228), but doesn't support Implicit SSL (entire connection is wrapped in an SSL layer)...

like image 596
Idriss Avatar asked Jun 18 '09 07:06

Idriss


People also ask

Can you send an email to a .NET address?

NET and . NET Core come with built-in support for sending emails through the System. Net.Mail namespace. While it might seem like the easy choice, you will need an SMTP server for this to work.


2 Answers

Here is an example of how to send email through GMail which also uses SSL/465. Minor tweaking of the code below should work!

using System.Web.Mail; using System; public class MailSender {     public static bool SendEmail(         string pGmailEmail,          string pGmailPassword,          string pTo,          string pSubject,         string pBody,          System.Web.Mail.MailFormat pFormat,         string pAttachmentPath)     {     try     {         System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();         myMail.Fields.Add             ("http://schemas.microsoft.com/cdo/configuration/smtpserver",                           "smtp.gmail.com");         myMail.Fields.Add             ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",                           "465");         myMail.Fields.Add             ("http://schemas.microsoft.com/cdo/configuration/sendusing",                           "2");         //sendusing: cdoSendUsingPort, value 2, for sending the message using          //the network.          //smtpauthenticate: Specifies the mechanism used when authenticating          //to an SMTP          //service over the network. Possible values are:         //- cdoAnonymous, value 0. Do not authenticate.         //- cdoBasic, value 1. Use basic clear-text authentication.          //When using this option you have to provide the user name and password          //through the sendusername and sendpassword fields.         //- cdoNTLM, value 2. The current process security context is used to          // authenticate with the service.         myMail.Fields.Add         ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");         //Use 0 for anonymous         myMail.Fields.Add         ("http://schemas.microsoft.com/cdo/configuration/sendusername",             pGmailEmail);         myMail.Fields.Add         ("http://schemas.microsoft.com/cdo/configuration/sendpassword",              pGmailPassword);         myMail.Fields.Add         ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",              "true");         myMail.From = pGmailEmail;         myMail.To = pTo;         myMail.Subject = pSubject;         myMail.BodyFormat = pFormat;         myMail.Body = pBody;         if (pAttachmentPath.Trim() != "")         {             MailAttachment MyAttachment =                      new MailAttachment(pAttachmentPath);             myMail.Attachments.Add(MyAttachment);             myMail.Priority = System.Web.Mail.MailPriority.High;         }          System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";         System.Web.Mail.SmtpMail.Send(myMail);         return true;     }     catch (Exception ex)     {         throw;     } } } 
like image 183
Andrew Siemer Avatar answered Sep 19 '22 21:09

Andrew Siemer


I'm late to this party but I'll offer my approach for any passersby that might be interested in an alternative.

As noted in previous answers, the System.Net.Mail SmtpClient class does not support Implicit SSL. It does support Explicit SSL, which requires an insecure connection to the SMTP server over port 25 in order to negotiate the transport level security (TLS). I blogged about my travails with this subtlety here.

In short, SMTP over Implict SSL port 465 requires TLS to be negotiated before connecting to the SMTP server. Rather than write a .Net SMTPS implementation I turned to a utility named Stunnel. It's a small service that will let you redirect traffic on a local port to a remote port via SSL.

DISCLAIMER: Stunnel uses portions of the OpenSSL library, which recently had a high-profile exploit published in all major tech news media. I believe the latest version uses the patched OpenSSL but please use at your own risk.

Once the utility is installed a small addition to the configuration file:

; Example SSL client mode services [my-smtps] client = yes accept = 127.0.0.1:465 connect = mymailserver.com:465 

...instructs the Stunnel service to reroute local requests to port 465 to my mail server on port 465. This happens over TLS, which satisfies the SMTP server on the other end.

Using this utility, the following code will successfully transmit over port 465:

using System; using System.Net; using System.Net.Mail;  namespace RSS.SmtpTest {     class Program     {         static void Main( string[] args )         {             try {                 using( SmtpClient smtpClient = new SmtpClient( "localhost", 465 ) ) { // <-- note the use of localhost                     NetworkCredential creds = new NetworkCredential( "username", "password" );                     smtpClient.Credentials = creds;                     MailMessage msg = new MailMessage( "[email protected]", "[email protected]", "Test", "This is a test" );                     smtpClient.Send( msg );                 }             }             catch( Exception ex ) {                 Console.WriteLine( ex.Message );             }         }     } } 

So the advantage here is that you can use Implict SSL and port 465 as the security protocol while still using the send mail methods built into the framework. The disadvantage is that it requires the use of a third party service that may not be useful for anything but this specific function.

like image 28
Bob Mc Avatar answered Sep 22 '22 21:09

Bob Mc