Tuesday, August 5, 2008

Password Recovery Web Control cannot send email Via SSL Mail Servers

I was using lately the Login Controls in a Website I am working on it, and I discovered a bug or you can call it limitation in Password Recovery Web Control. I used to use the Membership provider and Login Controls, but I didn't face this problem before since I was send the email via Non SSL Enabled SMTP server.

Now I was trying to send email using Password Recovery Control via Gmail, I got 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 email, which does not support reading EnableSSL setting from web.config. This bring us into a situation where Password Recovery control cannot send emails to SSL enabled smtp servers.

This because there is no settings in the Web.Config file for System.Net.Mail (.NET 2.0) that maps to EnableSSL Property of System.Net.Mail.SmtpClient.

Here is the solution for this problem:

1- We will consume the SendingMail Event.
2- This 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- 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("smtp.google.com", 587);
    smtpSender.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    smtpSender.Credentials = new System.Net.NetworkCredential("username@gmail.com", "password");
    smtpSender.EnableSsl = true;
    e.Cancel = true;

Source: http://www.nilebits.com/blog/2007/01/password-recovery-web-control-can-not-send-email-via-ssl-mail-servers-in-asp-dot-net/

Hire Me

Follow me on Facebook

Follow me

Do you find this Blog helpful?

Follow by Email

About Me

My Photo
Expert Senior Software Developer

Microsoft Business Card