简体   繁体   中英

How do I use TLS email with web.config

Here is my web.config file:

<system.net>
    <mailSettings>
        <smtp from="xxx@gmail.com"  >
            <network host="smtp.gmail.com" port="587" userName="xxx@gmail.com" password="yyy" />
        </smtp>
    </mailSettings>
</system.net>

I need to enable TLS, a requirement of my email server. However I only see SSL.

Its actually equivalent - TLS is kinda of broader than SSL. So use enableSsl= "true" for enabling TLS. As per MSDN documentation , that will force SMTPClient to use RFC 3207 (and RFC uses both terms TLS/SSL).

<network enableSsl="true" host="smtp.gmail.com" port="587" ...

TLS (Transport Level Security) is the slightly broader term that has replaced SSL (Secure Sockets Layer) in securing HTTP communications. So what you are being asked to do is enable SSL.

There is no setting in Web.Config for System.Net.Mail (.net 2.0) that maps to EnableSSL property of System.Net.Mail.SmtpClient.

Resolution

1) In the code behind, We need to consume PasswordRecovery1_SendingMail event of the web control
2) This event provide us access to Email Message being sent and also give us option to cancel the send operation
3) We will make a copy of this email message, and create a new instance of System.Net.Mail.SmtpClient
4) This time we have complete access to its properties and we can turn On/Off the EnableSSL setting
5) Lets set EnableSSL to true and send the email message to desired SMTP server

The below code snippet can do the job.

protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{
System.Net.Mail.SmtpClient smtpSender = new System.Net.Mail.SmtpClient("mail.google.com", 587);
smtpSender.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpSender.Credentials = new System.Net.NetworkCredential("username@gmail.com", "password");
smtpSender.EnableSsl = true;

smtpSender.Send(e.Message);
e.Cancel = true;
}

Repro Steps

1) Configure a PasswordRecovery control
2) Provide all the settings in Web.Config, including username/password, server name, email sender and others
3) Try to send a recovery email when SMTP server requires SSL

Check below link:
http://blogs.msdn.com/b/vikas/archive/2008/04/29/bug-asp-net-2-0-passwordrecovery-web-control-cannot-send-emails-to-ssl-enabled-smtp-servers.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM