Karatas, FERHAT
I would love to change the world, but they won't give me the source code!
ChangePassword control with Gmail Apps
5.09.2009 18:07:08 - Filed under : C# | Asp.net
BUG: ASP.net 2.0 PasswordRecovery/ChangePassword Web Control cannot send emails to SSL enabled SMTP Servers

This is a known issue or you can also call it as product limitation. PasswordRecovery or ChangePassword 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 PasswordRecovery control cannot send emails to SSL enabled smtp servers.

So when a user tries to send a email to SSL enable server using PasswordRecovery or ChangePassword control, he get the following error.

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
Cause :
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 :
Step 1 : Add SendingMail event to control;

<asp:ChangePassword ID="ChangePassword1" runat="server" onsendingmail = "ChangePassword1_SendingMail">


Step 2 : Add codebehind codes;
protected void ChangePassword1_SendingMail(object sender, MailMessageEventArgs e)
        {
            e.Message.Body.SendMail(e.Message.To[0].ToString(), e.Message.Subject.ToString(), string.Empty);
            e.Cancel = true;
        }

Step 3 : SendMail (extention) method;
public static void SendMail(this string body, string to, string subject, string attachFilename)
        {
            Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
            MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
            string from = mailSettings.Smtp.From;

            MailMessage message = new MailMessage();
            message.Priority = MailPriority.Normal;
            message.From = new MailAddress(from, ConfigurationManager.AppSettings["SiteName"]);
            message.To.Add(new MailAddress(to));
            message.Subject = subject;
            message.IsBodyHtml = true;
            message.SubjectEncoding = Encoding.UTF8;
            message.BodyEncoding = Encoding.UTF8;
            message.Body = body + "<br><br>" + ConfigurationManager.AppSettings["SiteDomain"] + "<br>" + DateTime.Now.ToString();

            if (attachFilename != string.Empty)
            {
                message.Attachments.Add(new Attachment(attachFilename));
            }

            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Send(message);
        }

Step 4 : Add smtp node to web.config;
<system.net>
  <mailSettings>
    <smtp from="sample@yourdomain.com" deliveryMethod="Network">
    <network host="smtp.gmail.com" port="587" userName="sample@yourdomain.com" password="xxxx" defaultCredentials="false"/>
    </smtp>
  </mailSettings>
</system.net>
<system.web>...

Keywords : Gmail apps with ChangePassword control in asp.net
with 0 comments

What do you think ?
Security Code 5353
Check S. Code
Name  
Email  
Home Page  
Comments