Send e-mail using SSL

2020-04-18 05:02发布

问题:

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?

回答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.



回答2:

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.



回答3:

Please change your code..

 SmtpServer.EnableSsl = false; 


标签: c# smtp