使用 SSL 发送电子邮件

Send e-mail using SSL

我希望我的应用程序使用 'SMTP over SSL' 发送电子邮件,即使服务器不支持 TLS。 到目前为止我已经试过了

        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());
        }

通过设置名为 EnableSsl 的 属性,我可以通过支持 TLS 的服务器发送邮件,但我无法通过仅支持 SMTP over SSL 的服务器发送邮件。 我怎样才能支持这种 SMTP/SSL 方法?

请更改您的代码..

 SmtpServer.EnableSsl = false; 

根据 SMTPClient 规范: 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.

您可以尝试使用已弃用但支持 SSL 的 System.Web.Mail.SmtpMail:

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

TBH 我认为您应该对您的服务提出警告,并声明仅支持使用 TLS 的 SMTP 服务器。但归根结底,这取决于你。

shows one more way that I can send email using SMTP over SSL with the help of Collaboration Data Objects component。这种方式也支持嵌入图片到邮件。