Saturday, March 5, 2011

Asp.net email for Account Verification


VS2008->File->New->WebSite

After this you have default.aspx page shown in solution explorer, rename it to register.aspx
Pick createuserwizard object from toolbox and drop it into register.aspx page
and after adding maildefination in it, code is following:

register.aspx
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
  </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
   <asp:CreateUserWizard ID="RegisterUserWithRoles" runat="server"  
        ContinueDestinationPageUrl="~/Default.aspx"  
        onsendingmail="NewUserWizard_SendingMail" DisableCreatedUser="True" LoginCreatedUser="False">   
       <MailDefinition BodyFileName="~/CreateUserWizard.htm" IsBodyHtml="True"  
       Subject="Welcome to My Website! Please activate your account.">  
     </MailDefinition>  
       <WizardSteps>   
        <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">   
       </asp:CreateUserWizardStep>       
       <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">       
       </asp:CompleteWizardStep>     
       </WizardSteps>  
       </asp:CreateUserWizard>  
     </div>  
   </form>  
 </body>  
 </html>  


register.aspx.cs
Add these namespaces in already existed namespaces

 using System.Net.Mail;  
 using System.Net;  
 protected void Page_Load(object sender, EventArgs e)  
   {  
     MailMessage myMessage = new MailMessage();  
     SmtpClient mySmtpClient = new SmtpClient();  
     myMessage.To.Add(new MailAddress("youraddress@example.com"));  
     mySmtpClient.Port = 587;  
     mySmtpClient.Credentials = new System.Net.NetworkCredential("admin email id", "admin email id password"); // email will be send to users to verify their a/c from admin by using his mail a/c  
     mySmtpClient.EnableSsl = true;  
   }  
  protected void NewUserWizard_SendingMail(object sender, MailMessageEventArgs e)  
   {  
     // Get the UserId of the just-added user  
     MembershipUser newUser = Membership.GetUser(RegisterUserWithRoles.UserName);  
     Guid newUserId = (Guid)newUser.ProviderUserKey;  
     // Determine the full verification URL (i.e., http://yoursite.com/Verification.aspx?ID=...)  
     string urlBase = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;  
     string verifyUrl = "/Verification.aspx?ID=" + newUserId.ToString();  
     string fullUrl = urlBase + verifyUrl;  
     // Replace <%VerificationUrl%> with the appropriate URL and querystring  
     e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", fullUrl);  
     //e.Message.CC.Add("webmaster@example.com");  
     SmtpClient mySmtpClient = new SmtpClient();  
     mySmtpClient.EnableSsl = true;  
     mySmtpClient.Send(e.Message);  
     e.Cancel = true;  
   }  


Verification.aspx
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Verification.aspx.cs" Inherits="Verification" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title>Untitled Page</title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <h2>  
     Account Verification</h2>  
   <p>  
     <asp:Label ID="StatusMessage" runat="server"></asp:Label>  
   </p>  
   </form>  
 </body>  
 </html>  


Verification.aspx.cs
  protected void Page_Load(object sender, EventArgs e)  
   {  
     if (string.IsNullOrEmpty(Request.QueryString["ID"]))  
       StatusMessage.Text = "The UserId was not included in the querystring...";  
     else  
     {  
       Guid userId;  
       try  
       {  
         userId = new Guid(Request.QueryString["ID"]);  
       }  
       catch  
       {  
         StatusMessage.Text = "The UserId passed into the querystring is not in the proper format...";  
         return;  
       }  
       MembershipUser usr = Membership.GetUser(userId);  
       if (usr == null)  
         StatusMessage.Text = "User account could not be found...";  
       else  
       {  
         // Approve the user  
         usr.IsApproved = true;  
         Membership.UpdateUser(usr);  
         StatusMessage.Text = "Your account has been approved. Please <a href=\"Login.aspx\">login</a> to the site.";  
       }  
     }  
   }  


CreateUserWizard.htm
Checkout Filename given in MailDefinition-> BodyFileName in register.aspx Page which is CreateUserWizard.htm

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head>  
   <title>Untitled Page</title>  
 </head>  
 <body>  
   <table>  
     <tr>  
       <td>Hello <%UserName%>! Welcome aboard.</td>  
       </tr>  
         <tr>  
            <td>  
        Your new account is almost ready, but before you can login you must first visit:  
            </td>  
          </tr>  
        <tr>  
          <td>  
          <a href="<%VerificationUrl%>">verify your account here</a>  
          </td>  
       </tr>  
       <tr>  
         <td>  
        <p>If you have any problems or questions, please reply to this email.</p>  
        <p>Thanks!</p>  
         </td>  
       </tr>  
    </table>  


Handling Emails using Simple Mail Transfer Protocol (SMTP)  in IIS
Open IIS click on + sign to expand it and view your virtual directory  created under connections.
In your virtual directory click on your web application and under asp.net section in features view, you can see SMTP E-mail ,double click on it.

Here i am using Live/HotMail email id as example and if you are using mail other than Live/HotMail then you have to fill SMTP sever name corresponding to  your email address and also port no can be differ for another email id that all can you get by make a simple search on Google.

No comments:

Popular Posts