Password recovery web control can not send email via SSL mail servers in ASP.NET

We were using lately the login controls for ASP.NET in a website we are working on it. We discovered a bug or we can call it limitation in the password recovery web control. We used to use the membership provider and login controls, but we did not face this problem before since we were sending the email via non SSL enabled SMTP server.

Now when we try to send email using password recovery control via Gmail, we get this error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. 32sm15616652wfa.13 Password Recovery control read most of the settings from web.config file.

Internally it uses System.Net.Mail to send out emails, which does not support reading EnableSSL setting from web.config. This bring us into a situation where password recovery control can not send emails to SSL enabled smtp servers.

This because there is no settings in the web.config file for System.Net.Mail that maps to EnableSSL property of System.Net.Mail.SmtpClient.

Please find below the solution for this problem:

  1. We will consume the SendingMail event.
  2. This would provide us access to the email message being sent and also give us option to cancel the sending operation.
  3. We will make a copy of this email message, and create a new instance of System.Net.Mail.SmtpClient
  4. Now 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 will do the job:

using System.Web.UI.WebControls;
namespace NB.BlogCode
    public class RecoverPassword
        protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
            System.Net.Mail.SmtpClient smtpSender = new System.Net.Mail.SmtpClient("", 587);
            smtpSender.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtpSender.Credentials = new System.Net.NetworkCredential("username", "password");
            smtpSender.EnableSsl = true;
            e.Cancel = true;

Share this post

Leave a comment

Next Post