How to send email over SSL SMTP using the .NET Framework?

Is there a way with the .NET Framework to send letters through an SSL SMTP server to 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); 

standby time:

 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 (the default port for Explicit SSL, which starts unencrypted, then issues STARTDLS, then switches to an encrypted connection: RFC 2228), but does not support Implicit SSL (full the connection is wrapped in an SSL layer) ...

+32
c # email ssl smtp
Jun 18 '09 at 7:24
source share
10 answers

Here is an example of how to send email through GMail, which also uses SSL / 465. Slight code setting 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; } } } 
+32
Jun 18 '09 at 20:01
source share

I am late for this party, but I offer my approach to any passers-by who may be interested in an alternative.

As noted in previous answers, the System.Net.Mail SmtpClient class SmtpClient not support implicit SSL. It supports Explicit SSL, which requires an insecure connection to the SMTP server through port 25 to negotiate transport layer security (TLS). I wrote about my torment with this subtlety here .

In short, SMTP through the Implict SSL port 465 requires that TLS be negotiated before connecting to the SMTP server. Instead of writing a .Net SMTPS implementation, I turned to a utility called Stunnel . This is a small service that allows you to redirect traffic on a local port to a remote port via SSL.

DISCLAIMER: Stunnel uses parts of the OpenSSL library, which recently had a high-profile exploit published in all major news technical facilities. I believe that the latest version uses the corrected OpenSSL, but please use at your own risk.

After installing the utility, 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 forward local requests to port 465 to my mail server on port 465. This happens through TLS, which satisfies the SMTP server on the other end.

Using this utility, the following code will successfully transmit through 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( "joe@schmoe.com", "jane@schmoe.com", "Test", "This is a test" ); smtpClient.Send( msg ); } } catch( Exception ex ) { Console.WriteLine( ex.Message ); } } } } 

Thus, the advantage is that you can use Implict SSL and port 465 as a security protocol, while still using the mail sending methods built into the framework. The disadvantage is that this requires the use of a third-party service, which may not be useful for anything other than this specific function.

+9
Apr 10 '15 at 0:48
source share

It works with System.Web.Mail (which is marked as deprecated):

 private const string SMTP_SERVER = "http://schemas.microsoft.com/cdo/configuration/smtpserver"; private const string SMTP_SERVER_PORT = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"; private const string SEND_USING = "http://schemas.microsoft.com/cdo/configuration/sendusing"; private const string SMTP_USE_SSL = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"; private const string SMTP_AUTHENTICATE = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"; private const string SEND_USERNAME = "http://schemas.microsoft.com/cdo/configuration/sendusername"; private const string SEND_PASSWORD = "http://schemas.microsoft.com/cdo/configuration/sendpassword"; System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); mail.Fields[SMTP_SERVER] = "tempurl.org"; mail.Fields[SMTP_SERVER_PORT] = 465; mail.Fields[SEND_USING] = 2; mail.Fields[SMTP_USE_SSL] = true; mail.Fields[SMTP_AUTHENTICATE] = 1; mail.Fields[SEND_USERNAME] = "username"; mail.Fields[SEND_PASSWORD] = "password"; System.Web.Mail.SmtpMail.Send(mail); 

What is your point of view regarding using an obsolete namespace?

+8
Jun 18 '09 at 7:45
source share

Try checking out this free open source alternative https://www.nuget.org/packages/AIM. It is free to use and open source, and uses the same system.Net.Mail. To send email to ssl implicit ports you can use the following code

 public static void SendMail() { var mailMessage = new MimeMailMessage(); mailMessage.Subject = "test mail"; mailMessage.Body = "hi dude!"; mailMessage.Sender = new MimeMailAddress("you@gmail.com", "your name"); mailMessage.To.Add(new MimeMailAddress("yourfriend@gmail.com", "your friendd name")); // You can add CC and BCC list using the same way mailMessage.Attachments.Add(new MimeAttachment("your file address")); //Mail Sender (Smtp Client) var emailer = new SmtpSocketClient(); emailer.Host = "your mail server address"; emailer.Port = 465; emailer.SslType = SslMode.Ssl; emailer.User = "mail sever user name"; emailer.Password = "mail sever password" ; emailer.AuthenticationMode = AuthenticationType.Base64; // The authentication types depends on your server, it can be plain, base 64 or none. //if you do not need user name and password means you are using default credentials // In this case, your authentication type is none emailer.MailMessage = mailMessage; emailer.OnMailSent += new SendCompletedEventHandler(OnMailSent); emailer.SendMessageAsync(); } // A simple call back function: private void OnMailSent(object sender, AsyncCompletedEventArgs asynccompletedeventargs) { if (e.UserState!=null) Console.Out.WriteLine(e.UserState.ToString()); if (e.Error != null) { MessageBox.Show(e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (!e.Cancelled) { MessageBox.Show("Send successfull!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } } 
+5
Jan 19 '15 at 2:49
source share

If this is implicit SSL, it looks like it cannot be executed using System.Net.Mail and is not yet supported.

http://blogs.msdn.com/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx

To check if it is implicit SSL, try this.

+4
Jun 18 '09 at 20:28
source share

You can also connect through port 465, but due to some limitations of the System.Net.Mail namespace, you may need to change your code. This is because the namespace does not allow you to create implicit SSL connections. This is discussed at http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx .

You can create implicit connections without using the already obsolete System.Web.Mail namespace, but you need to access the Microsoft CDO (Collaborative Data Object). I gave an example of using CDO in another discussion ( GMail SMTP via C # .Net errors in all ports ).

Hope this helps!

+4
Oct 03 2018-10-03
source share

In VB.NET, when trying to connect to the Rackspace SSL port on 465, I ran into the same problem (implicit SSL is required). For a successful connection, I used https://www.nuget.org/packages/MailKit/ .

The following is an example HTML email message.

 Imports MailKit.Net.Smtp Imports MailKit Imports MimeKit Sub somesub() Dim builder As New BodyBuilder() Dim mail As MimeMessage mail = New MimeMessage() mail.From.Add(New MailboxAddress("", c_MailUser)) mail.To.Add(New MailboxAddress("", c_ToUser)) mail.Subject = "Mail Subject" builder.HtmlBody = "<html><body>Body Text" builder.HtmlBody += "</body></html>" mail.Body = builder.ToMessageBody() Using client As New SmtpClient client.Connect(c_MailServer, 465, True) client.AuthenticationMechanisms.Remove("XOAUTH2") ' Do not use OAUTH2 client.Authenticate(c_MailUser, c_MailPassword) ' Use a username / password to authenticate. client.Send(mail) client.Disconnect(True) End Using End Sub 
+3
Sep 09 '16 at 15:11
source share

For gmail, these options worked for me, the line ServicePointManager.SecurityProtocol was needed. Since I have a 2-step setup, I need a password for the application from the Google password generator. SmtpClient mailer = new SmtpClient(); mailer.Host = "smtp.gmail.com"; mailer.Port = 587; mailer.EnableSsl = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

0
Jun 10 '16 at 2:02
source share

I know that I join the discussion late, but I think it can be useful for others.

I wanted to avoid obsolete things, and after many attempts, I found an easy way to send servers that require implicit SSL: use NuGet and add the MailKit package to the project. (I used VS2017 to install .NET 4.6.2, but it should work on lower versions of .NET ...)

Then you will need to do something like this:

 using MailKit.Net.Smtp; using MimeKit; var client = new SmtpClient(); client.Connect("server.name", 465, true); // Note: since we don't have an OAuth2 token, disable the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove ("XOAUTH2"); if (needsUserAndPwd) { // Note: only needed if the SMTP server requires authentication client.Authenticate (user, pwd); } var msg = new MimeMessage(); msg.From.Add(new MailboxAddress("sender@ema.il")); msg.To .Add(new MailboxAddress("target@ema.il")); msg.Subject = "This is a test subject"; msg.Body = new TextPart("plain") { Text = "This is a sample message body" }; client.Send(msg); client.Disconnect(true); 

Of course, you can also configure it to use Explicit SSL or no transport security at all.

0
Sep 13 '17 at 17:02 on
source share

If you have any doubts about this code, ask your questions (here for Gmail port number 587)

 // code to Send Mail // Add following Lines in your web.config file // <system.net> // <mailSettings> // <smtp> // <network host="smtp.gmail.com" port="587" userName="xxx@gmail.com" password="yyy" defaultCredentials="false"/> // </smtp> // </mailSettings> // </system.net> // Add below lines in your config file inside appsetting tag <appsetting></appsetting> // <add key="emailFromAddress" value="xxxx@gmail.com"/> // <add key="emailToAddress" value="xxxxxxx@gmail.com"/> // <add key="EmailSsl" value="true"/> // Namespace Used using System.Net.Mail; public static bool SendingMail(string subject, string content) { // getting the values from config file through c# string fromEmail = ConfigurationSettings.AppSettings["emailFromAddress"]; string mailid = ConfigurationSettings.AppSettings["emailToAddress"]; bool useSSL; if (ConfigurationSettings.AppSettings["EmailSsl"] == "true") { useSSL = true; } else { useSSL = false; } SmtpClient emailClient; MailMessage message; message = new MailMessage(); message.From = new MailAddress(fromEmail); message.ReplyTo = new MailAddress(fromEmail); if (SetMailAddressCollection(message.To, mailid)) { message.Subject = subject; message.Body = content; message.IsBodyHtml = true; emailClient = new SmtpClient(); emailClient.EnableSsl = useSSL; emailClient.Send(message); } return true; } // if you are sending mail in group private static bool SetMailAddressCollection(MailAddressCollection toAddresses, string mailId) { bool successfulAddressCreation = true; toAddresses.Add(new MailAddress(mailId)); return successfulAddressCreation; } 
-one
May 15 '14 at 1:47
source share



All Articles