unable to send emails using port 25 in ssl mode?

Viewed 4255

Hi I am developing web application in mvc5 c#. I have hosted my website in ssl mode. I am using below code to send emails in production server.

                string AdminEmail = ConfigurationManager.AppSettings["AdminEmail"].ToString();
                MailMessage mail = new MailMessage();
                mail.To.Add(emailid);
                mail.Bcc.Add(AdminEmail);
                mail.From = new MailAddress(MailID);
                mail.Subject = Subject;
                mail.Body = Body;
                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient(hostserver);
                smtp.Credentials = new System.Net.NetworkCredential
                 (MailID, Password);
                smtp.Send(mail);
                return 1;

i added

   if (IsInternalBuild)
                {
                    smtp.Host = hostserver;    mail.c3payroll.com
                    smtp.Port = Convert.ToInt32(portno);  465
                    smtp.UseDefaultCredentials = true;
                    smtp.EnableSsl = true;
                }

IsInternalBuild is true in production environment.

When sending in ssl mode, do i need to change anything in above code? Can someone help me to implement this? Any help would be appreciated. Thank you.

3 Answers

Depending on your email host provider settings, it's easy to set host name, port number and SSL feature to sending mail by using corresponding properties in SmtpClient:

SmtpClient smtp = new SmtpClient(hostserver);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential(MailID, Password);
smtp.Host = hostserver; // host name as mail server
smtp.Port = Convert.ToInt32(portno); // some hosts doesn't support port 25 for SMTPS, try this port instead or use 587
smtp.EnableSsl = true; // use SSL configuration

smtp.Send(mail);

Update (Explicit & Implicit SSL)

Keep in mind that System.Net.Mail.SmtpClient only supports explicit SSL, which requires insecure connection to SMTP server over port 25 to negotiate with TLS. There is SMTP over SSL for port 465 which requires TLS negotiation before connection to SMTP server established, but still doesn't support implicit SSL with standard System.Net.Mail library. The reason is explained at this reference.

There is no way to use Implicit SSL (SMTPS) with System.Net.Mail. Implicit SSL would have the entire connection is wrapped in an SSL layer. A specific port would be used (port 465 is common). There is no formal RFC covering Implicit SSL.

Implicit SSL would go something like: Start SSL (start encryption) -> Connect -> Authenticate -> send data

This is not considered a bug, it’s by design. There are two types of SSL authentication for SMTP, and we only support one with System.Net.Mail (by design) – Explicit SSL.

To use implicit SSL, you need to utilize libraries which enables CDOSYS, such like System.Web.Mail.MailMessage and System.Web.Mail.SmtpMail (which seems to be obsolete but works with both explicit and implicit SSL, require CDO configuration schemas to work):

System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.To = emailid;
mail.Bcc = AdminEmail;
mail.From = new MailAddress(MailID);
mail.Subject = Subject;
mail.Body = Body;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", hostServer);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", MailID);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Password);

System.Web.Mail.SmtpMail.SmtpServer = hostServer;
System.Web.Mail.SmtpMail.Send(mail);

Related issue:

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

You cant send SSL emails on port 25, you need to use port 465 instead

You also need to set smtp.EnableSsl = true

There are multiple ways to use ssl in smtp. One is "implicit" ssl when you negotiate ssl before starting SMTP session (similar to https). For that one separate port is usually used (465). This way is not supported by SmtpClient, as stated in documentation:

An alternate connection method is where an SSL session is established up front before any protocol commands are sent. This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.

Another one is STARTTLS extension. This way negotiates ssl as a part of normal SMTP session, which starts unencrypted (as usual on 25 port). After usual HELO exchange client issues "250 STARTTLS" command and (if supported by server) - ssl is negotiated and session continues encrypted. This way IS supported by SmtpClient with its EnableSsl property. When set to true - it will try to negotiate ssl with STARTTLS. If server does not support it - exception is thrown.

But, your mail.c3payroll.com smtp server does not support any of the above ways. You can telnet to it on 25 port and after HELO try to do "250 STARTTLS" and it will tell you it's not supported. Port 465 is not available there too. So if you want to use ssl for your smtp sessions - find normal smpt server first.

Related