C# .NET - E-mail - Asked By hiren dhameliya on 15-Dec-11 06:50 PM


I am currently working on one website. I want to implement   like this

When customer register with site automatic email go to in inbox with one link 
link should be active your account. when customer cilck on the link his account will be actived by it self..
 I don't know how to implement this things 

Please help me as soon as possible...
Riley K replied to hiren dhameliya on 15-Dec-11 07:44 PM

I will tell you the steps on how to Implelment  it 

  1. User Registers
  2. Once registration guid is created
  3. guid is put into a table associated with a username
  4. E-Mail is sent to user with a url based on the GUID something like ( http://example.com/Activate.aspx?key=1234-1234-1234 )
  5. User Opens Email and click link
  6. Person comes to activation page
  7. Activation page reads querystring and associates the string with guid in db.  If a match executes code to “approve” account for use
  8. User can login

Using membership it will be more easy

You can use the Create User Wizard Control

    <asp:Login ID="Login1" runat="server">
    <asp:CreateUserWizard ID="CreateUserWizard1" runat="server"
      oncreateduser="CreateUserWizard1_CreatedUser" DisableCreatedUser="true">
        <asp:CreateUserWizardStep runat="server" />
        <asp:CompleteWizardStep runat="server" />

The login.aspx.cs will look like this 

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    string userName = CreateUserWizard1.UserName;
    string emailAddress = CreateUserWizard1.Email;
    // Lets get the user's id
    Guid userId = (Guid)Membership.GetUser(userName).ProviderUserKey;
    // Now lets create an email message
    StringBuilder emailMessage = new StringBuilder();
    emailMessage.Append("Thank you for creating an account with YourDomain.com");
    emailMessage.Append("<br />");
    emailMessage.Append("Please click the below link to activate your account <br />");
    emailMessage.Append(string.Format("<a href='http://www.YourDomain.com/ActivateUser.aspx?userName{0}&Id={1}'>Activate {0} </a>", userName, userId.ToString()));
    MailMessage email = new MailMessage();
    email.From = new MailAddress("noReply@YourDomain.com");
    email.To.Add(new MailAddress(emailAddress));
    email.Subject = "Please activate your account with YourDomain";
    email.Body = emailMessage.ToString();
    email.IsBodyHtml = true;
    // Send the email
    SmtpClient client = new SmtpClient();
    // Redirecto to What ever page
    Response.Redirect("Default.aspx", true);
ActivateUser.aspx.cs : Here, we get the UserId and his email address as QueryString parameters and use membership api to activate the user.

protected void Page_Load(object sender, EventArgs e)
    // Lets Activate the User
    if (String.IsNullOrEmpty(Request.Params["Id"]))
      // We do not have the userId. Redirect some where
      // We have a userId.
        Guid userId = new Guid(Request.Params["Id"]);
        MembershipUser user = Membership.GetUser(userId);
        // Activate the user
        user.IsApproved = true;
        // Update the user activation
        // We success fully activate the user. Redirect somewhere
        Response.Redirect("Welcome.aspx", true);
        // Error. Redirect some where
    // We should never reach here. Just in case redirect some where
    Response.Redirect("Default.aspx", true);

Refer this link also


Jitendra Faye replied to hiren dhameliya on 15-Dec-11 10:58 PM

After registration of user just use this code to send Activation link to user.

#define Gmail
#define SMTP

using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
StringBuilder bodyMsg = new StringBuilder();
TextBox username = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName");
TextBox password = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Password");
TextBox email = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Email");

CreateUserWizard cuw = (CreateUserWizard)sender;
MembershipUser user = Membership.GetUser(cuw.UserName);
Guid userID = (Guid)user.ProviderUserKey;

bodyMsg.Append("Thank you for creating your account.\n\nPlease follow this link to activate: ");
bodyMsg.Append("<br /><br /><a href=http://yourserver/SendEmailConfirmationSample/Activate.aspx?ID=" + userID.ToString() + ">Activate Your Account</a>");
bodyMsg.Append("<br />");
bodyMsg.Append("<br />");
bodyMsg.AppendFormat("UserName: {0}", username.Text);
bodyMsg.Append("<br />");
bodyMsg.AppendFormat("Password: {0}", password.Text);
bodyMsg.Append("<br />");
bodyMsg.AppendFormat("Registered Email: {0}", email.Text);

#if SMTP
// Sending email via local or web hosts smtp server.
NetworkCredential loginInfo = new NetworkCredential("yourusername", "yourpassword");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("youremailaddress");
msg.To.Add(new MailAddress(CreateUserWizard1.Email));
msg.Subject = "Account Information";
msg.Body = bodyMsg.ToString();
msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient("smtpserver", 25);
client.Credentials = loginInfo;

#if Gmail
// Sending email via Gmail.
NetworkCredential loginInfo = new NetworkCredential("yourUsername@gmail.com", "yourGmailPassword");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("yourUsername@gmail.com");
msg.To.Add(new MailAddress(CreateUserWizard1.Email));
msg.Subject = "Account Information";
msg.Body = bodyMsg.ToString();
msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;


Try this code and let me know.
Anoop S replied to hiren dhameliya on 15-Dec-11 11:46 PM
Three ways to send email to user to active his account to be able to login to website

————–First Way ————-

01   protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
02   {
03     //Send email to user for verifying account
04     CreateUserWizard cuw = (CreateUserWizard)sender;
05     MembershipUser user = Membership.GetUser(cuw.UserName);
06     Guid userId = (Guid)user.ProviderUserKey;
07     System.Net.Mail.MailMessage EmailMsg = new System.Net.Mail.MailMessage("info@ask.com", CreateUserWizard1.Email);
08     EmailMsg.Subject = "Email Verification from ask.com";
09     EmailMsg.IsBodyHtml = true;
10     EmailMsg.Body = "Thanks for registering with www.ask.com!<br /><br />Your activation link : http://www.ask.com/activate.aspx?ID=" + userId.ToString() + ">Link.";
11     System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
12     //This object stores the authentication values
13     System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("smtpserverinfo", "smtpserverpassword");
15     mailClient.Host = "mail.ask.com";
16     mailClient.UseDefaultCredentials = false;
17     mailClient.Credentials = basicAuthenticationInfo;
18     mailClient.Send(EmailMsg);  
20   }

and in activate.aspx:

01   protected void Page_Load(object sender, EventArgs e)
02   {
03     Guid oGuid = new Guid(Request.QueryString["ID"]);
04     //Guid oGuid = new Guid();
05     MembershipUser oUser = Membership.GetUser(oGuid);
06     if (oUser != null && oUser.IsApproved == false)
07     {
08       oUser.IsApproved = true;
09       Membership.UpdateUser(oUser);
10       System.Web.Security.FormsAuthentication.RedirectFromLoginPage(oUser.UserName, false);
11     }
12   }
dipa ahuja replied to hiren dhameliya on 16-Dec-11 02:56 AM
Try Simple Code

protected void Button2_Click(object sender, EventArgs e)
  string toEmailAddress = "UserToWhomSendPassword@gmail.com";
  string GmailId = "xxx@gmail.com";
  string password = "xxx";
  string bodyMsg = "<br /><br /><a href=http://yourserver/SendEmailConfirmationSample/Activate.aspx?ID=" + toEmailAddress.ToString() + ">Activate Your Account</a>";
  MailMessage mail = new MailMessage();
  mail.From = new MailAddress(GmailId);
  mail.Subject = txtSubject.Text;
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
  SmtpClient smtp = new SmtpClient("smtp.gmail.com");
  smtp.EnableSsl = true;
  smtp.UseDefaultCredentials = false;
  smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);