1

I want my application to send e-mail using 'SMTP over SSL' even if TLS is not supported by server. So far I have tried

        try
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("abc@xyz.com");
            mail.To.Add("to_address");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail from GMAIL";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
            SmtpServer.EnableSsl = true;    //true: sends using TLS, false: sends without security

            SmtpServer.Send(mail);
            MessageBox.Show("Mail sent");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error" + ex.ToString());
        }

by setting the property called EnableSsl, I can send mail over the servers which support TLS but I am not able to send it through server which only supports SMTP over SSL. How can I give support for this SMTP/SSL method?

Kamalesh Wankhede
  • 1,475
  • 16
  • 37

3 Answers3

1

According to the SMTPClient spec: https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl(v=vs.110).aspx

The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.

You can try using System.Web.Mail.SmtpMail, which is deprecated, but which supports SSL:

https://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail(v=vs.110).aspx

TBH I think you should place a caveat on your service and state that only SMTP servers that use TLS are supported. But at the end of the day, that is up to you.

user1666620
  • 4,800
  • 18
  • 27
  • Do you mean SMTP over SSL is not anymore supported/recommended by .Net framework? – Kamalesh Wankhede May 06 '16 at 13:09
  • @krw12572 The SmtpClient only supports TLS. The System,Web.Mail.SmtpMail class still ought to support SSL, though it has been depracated. https://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail(v=vs.110).aspx Since it has been depracated, I would say that it isn't recommended to use it any more. – user1666620 May 06 '16 at 13:35
  • I suppose, I could use this System.Web.Mail.SmtpMail class for sending mail using SMTP over SSL even if it is deprecated. So, I'm marking this as an answer to this question. – Kamalesh Wankhede May 06 '16 at 13:48
  • @krw12572 just because it's deprecated doesn't mean it won't work. It's just that if anything goes wrong or if there is ever a breaking change with a future .NET release, you're on your own. – user1666620 May 06 '16 at 16:27
  • Instead of using System.Web.Mail, you could also use [MailKit](https://github.com/jstedfast/MailKit) instead to avoid using deprecated API. – jstedfast May 07 '16 at 00:20
0

This link shows one more way that I can send email using SMTP over SSL with the help of Collaboration Data Objects component. This way also supports embedding images to email.

Community
  • 1
  • 1
Kamalesh Wankhede
  • 1,475
  • 16
  • 37
-2

Please change your code..

 SmtpServer.EnableSsl = false; 
Banwari Yadav
  • 506
  • 1
  • 3
  • 18