4

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.

Niranjan Godbole
  • 2,055
  • 6
  • 32
  • 72
  • Are you looking for this: `smtp.EnableSsl = true;`? I think you need to enable SSL support by setting that property (also available: `smtp.Port = 25` and `smtp.Host = "youremailaccount@domain.com"`). – Tetsuya Yamamoto Nov 07 '17 at 05:37
  • You need to clarify which SSL you mean first. There is so called implicit SSL (port 465, SSL negotiated before SMTP session starts, not supported by SmtpClient) and explicit SSL (port 25, first SMTP session starts unencrypted, then SSL is negotiated - supported by SmtpClient with EnableSSL property). – Evk Nov 07 '17 at 05:59
  • Thanks evk. I have used ssl certificate while hosting. so what i should change in the above code? – Niranjan Godbole Nov 07 '17 at 06:01
  • Thanks EVK. I tested in my localhost. I am getting Server does not support secure connections. Same error is loggin in elmah also. – Niranjan Godbole Nov 07 '17 at 06:20

3 Answers3

4

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?

Tetsuya Yamamoto
  • 21,982
  • 5
  • 34
  • 53
  • Did you really try that? Because documentation of SmtpClient claims that SMTP over SSL is not supported (and EnableSSL means different thing - STARTTLS extension). – Evk Nov 07 '17 at 05:54
  • Thanks Tetsuya. SMTP client will not work with SSL? Is there any other way to send emails? – Niranjan Godbole Nov 07 '17 at 05:57
  • @NiranjanGodbole Updated with implicit & explicit SSL explanation and you're seem want to use *implicit SSL* which doesn't support by the server. Try using another server which supports implicit SSL with TLS method (change `hostServer` value with other server than you currently have). – Tetsuya Yamamoto Nov 07 '17 at 06:42
  • Thanks for your updated answer. Where can i find System.Web.Mail.MailMessage? i am using vs2015! – Niranjan Godbole Nov 07 '17 at 06:45
  • `System.Web.Mail` class is marked obsolete but I think you can still use it. Can't you type `System.Web.Mail` class in VS? That library still available as in 4.7.1: https://docs.microsoft.com/en-us/dotnet/api/system.web.mail?view=netframework-4.7.1. – Tetsuya Yamamoto Nov 07 '17 at 06:55
  • I got it i implemented as above but ends up with error The transport failed to connect to the server!!! I am testing above code in console application. No issues right? – Niranjan Godbole Nov 07 '17 at 06:58
  • @NiranjanGodbole Your current mail provider seems not support implicit SSL with TLS (possibly `CDO.Message.1` with error `0x80040213`). Try using other provider which supports implicit SSL & TLS. – Tetsuya Yamamoto Nov 07 '17 at 07:03
2

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

You also need to set smtp.EnableSsl = true

Alander
  • 561
  • 1
  • 6
  • 15
  • Thanks Alandar. I am not using gmail but i am using our own mail.c3payroll.com and @host 25. So this will not work out? – Niranjan Godbole Nov 07 '17 at 05:43
  • 1
    port 465 is a standard port for SMTPS, if your host supports SMTPS it will support 465. You cant send SMTPS on port 25, its not supported – Alander Nov 07 '17 at 05:45
  • Thanks for your information!. Now we need to configure smtp to 465? Can i get some link so that i can share to client and i can convince them? – Niranjan Godbole Nov 07 '17 at 05:46
  • 2
    Just use `smtp.Port = 465` and `smtp.EnableSsl = true` and you're done. – Tetsuya Yamamoto Nov 07 '17 at 05:47
  • I have edited my question. As you told i have added those properties and still i am not able to send emails. May i know is there anything to be configured in smtp? – Niranjan Godbole Nov 07 '17 at 05:54
  • That's not true, first you can send SSL emails on port 25 via STARTTLS extension (and that is what EnableSsl property is about). Second - ssl on port 465 is not supported by SmtpClient. – Evk Nov 07 '17 at 06:02
  • So with respect to above code port 465 will not work? Code change requires? @EVK – Niranjan Godbole Nov 07 '17 at 06:05
  • My confusion is 1. with ssl can we send emails using port 25? 2. Is above code correct? Can someone correct me? – Niranjan Godbole Nov 07 '17 at 06:06
1

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.

Evk
  • 84,454
  • 8
  • 110
  • 160
  • I do but I'm not going to provide any support via teamviewer anyway – Evk Nov 07 '17 at 06:28
  • Thank you. But you gave very useful information. So first i should telnet to SMTP server to test right? – Niranjan Godbole Nov 07 '17 at 06:29
  • You can telnet, or you can use some online tool (like this one https://mxtoolbox.com/diagnostic.aspx - put address of your smtp server there and it will tell you that SSL is not supported). But you know that already because you got error "Server does not support secure connections". And to verify implicit SSL is also not supported by your server you can try telnet to port 465 (and see that port is not open\available). – Evk Nov 07 '17 at 06:31
  • i found SMTP TLS -Does not support TLS in mxtoolbox.com. Now is there any way to fix this? – Niranjan Godbole Nov 07 '17 at 06:33
  • Only way to "fix" it is to use different smtp server (not mail.c3payroll.com) which does support TLS. With this server all your smtp sessions will travel through network in plain text. – Evk Nov 07 '17 at 06:34
  • Well I explained in not that much words in the answer already. – Evk Nov 07 '17 at 07:00