I want to send a confirmation email to the user after they register to my website. I am using CreateUserWizard of .NET and I use C#.
My idea is after users register they get an email with a link. If they click the link, they will come to an activation page.
I have done this so far:
Register.aspx:
<asp:CreateUserWizard ID="CreateUserWizard1" Runat="server"
ContinueDestinationPageUrl="default.aspx"
EmailRegularExpression="\S+@\S+\.\S+"
EmailRegularExpressionErrorMessage="The email format is invalid."
ForeColor="#333333" Font-Names="Verdana" Font-Size="12px"
CreateUserButtonStyle-CssClass="button"
oncreateduser="CreateUserWizard1_CreatedUser"
LoginCreatedUser="false" DisableCreatedUser="True">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep2" Runat="server"
Title="Sign Up for Your New Account">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep2" Runat="server" Title="Completeeeeee">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
I am very confused and not sure what to do. I understand that i need to email an url with the userID to user's email upon registration, but how to do this, and when user clicks that url in that email, how that url will automatically come to activation page?
Please give suggestion and code example.
c sharpASP .NET 2. 0 .net SMTP Registration Wizard Activate New User
<MailDefinition BodyFileName="~/Docs/NewUserEmailBody.txt"
CC="postmaster@mysite.com" From="postmaster@mysite.com"
Subject="Welcome to my site">
</MailDefinition>
Now, the NewUserEmailBody.txt has your email with <Placeholders> for actual values.
<%NEWUSERID%> is one field. All fields in your email will get replaced automatically and the email will also be sent automatically. You don't need to do anything else except change your ASP code to what I showed you and then write the email with placeholders. Here is the example from mine. Mine does not have an activation page but you can see the other available replacement fields.
Welcome to mysite.
User id: <%UserName%>
Password key: <%Password%>
Thanks for your interest in mysite. We welcome you to our family!
We invite you to explore our site and get to know us better. A few features you'll come across are:
Be sure to check back frequently, as our website is being updated and improved daily. You can sign up for automatic notifications when our website changes by simply checking "Send website change alerts" in the "My Profile" section.
mysite wants to make sure we offer you services and opportunities that will get you involved in this horse loving community. We welcome your ideas and input on games, training, and group ride activities. Click on "Contact Us" to shoot us an email with your thoughts.
We look forward to seeing you soon!
string ID will be part of that ActivationPage url (like http://localhost:72/ActivationPage.aspx?str=axxxxxxxx), which I have to put in the MailFile.txt of
MailDefinition
However, I am not sure how to construct this url and send this unique url to whenever a user registers.
For clearification, I am showing the codes:
Register.aspx:
<asp:CreateUserWizard ID="CreateUserWizard1" Runat="server"
ContinueDestinationPageUrl="default.aspx"
EmailRegularExpression="\S+@\S+\.\S+"
EmailRegularExpressionErrorMessage="The email format is invalid."
ForeColor="#333333" Font-Names="Verdana" Font-Size="12px"
CreateUserButtonStyle-CssClass="button"
oncreateduser="CreateUserWizard1_CreatedUser"
LoginCreatedUser="false" DisableCreatedUser="True">
<MailDefinition
BodyFileName="MailFile.txt"
From="xxx@xxx.com"
Subject="Word has it, you forgot your password?">
</MailDefinition>
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep2" Runat="server"
Title="Sign Up for Your New Account">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep2" Runat="server" Title="Completeeeeee">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
Your account was set up on our Web site with the following:
Username: <%UserName%>
Password key: <%Password%>
http://localhost:72/ActivationPage.aspx
Thank you for creating an account with our Web site.
Like I said in the previous post, you can use additional tags in your email. The example email did not have the <%NEWUSERID%> in it but you can add it to yours.
change you email to iinclude this field.
Your account was set up on our Web site with the following:
Username: <%UserName%>
Password key: <%Password%>
http://localhost:72/ActivationPage.aspx?id=<%NEWUSERID%>
Thank you for creating an account with our Web site.
Thank you for creating an account with our Web site.
Although the Username and the Password appears in the email but the UserId value does not come from the aspnetdb. What maight be wrong? What am I missing? I thought instead of
<%NEWUSERID%> it would be
<%UserID%> as in aspnetdb database, in the aspnet_Users table, the column name is UserID, but that does not work either. Please help.
I thought that I read somewhere where this is a field. There does not seem to be any complete documentatio on all the fields that can be used.
So here is your other solution. Obviously you already have the mail server working so this will be easy.
Step 1. Remove that mail definition from your wizard
Step 2. Add this try catch block to your function UserCreated to send the email
Try
Dim insMail As New MailMessage("postmaster@mysite.com", CreateUserWizard1.Email, "my subject", BuildEmailBody)
Dim c As System.Net.Mail.SmtpClient = New SmtpClient("mail.mysite.com")
insMail.CC.Add("postmaster@mysite.com")
c.Send(insMail)
Catch xx As Exception
End Try
Create a function that returns a string called 'BuildEmailBody' and create any body that you want. This function is called in the MailMessage constructor.
<asp:CreateUserWizard ID="CreateUserWizard1" Runat="server"
ContinueDestinationPageUrl="default.aspx"
EmailRegularExpression="\S+@\S+\.\S+"
EmailRegularExpressionErrorMessage="The email format is invalid."
ForeColor="#333333" Font-Names="Verdana" Font-Size="12px"
CreateUserButtonStyle-CssClass="button"
OnSendingMail="CreateUserWizard1_CreatedUser"
LoginCreatedUser="false" DisableCreatedUser="True">
<MailDefinition
BodyFileName="MailFile.txt"
From="xxx@xxx.com"
Subject="Word has it, you forgot your password?">
</MailDefinition>
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep2" Runat="server"
Title="Sign Up for Your New Account">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep2" Runat="server" Title="Completeeeeee">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
And the MailFile.txt is:
Your account was set up on our Web site with the following:
Username: <%UserName%>
Password key: <%Password%>
Verification Url: <%VerificationUrl%>
Thank you for creating an account with our Web site
Thank you for the suggestions about the MailDefinition and the QueryString information. I have found a good tutorial site about this also, which is:
ashley2009
Member
19 Points
46 Posts
How to send user confirmation email after registration with activation link?
Jan 21, 2010 06:17 PM|LINK
Hello,
I want to send a confirmation email to the user after they register to my website. I am using CreateUserWizard of .NET and I use C#.
My idea is after users register they get an email with a link. If they click the link, they will come to an activation page.
I have done this so far:
Register.aspx:
<asp:CreateUserWizard ID="CreateUserWizard1" Runat="server" ContinueDestinationPageUrl="default.aspx" EmailRegularExpression="\S+@\S+\.\S+" EmailRegularExpressionErrorMessage="The email format is invalid." ForeColor="#333333" Font-Names="Verdana" Font-Size="12px" CreateUserButtonStyle-CssClass="button" oncreateduser="CreateUserWizard1_CreatedUser" LoginCreatedUser="false" DisableCreatedUser="True"> <WizardSteps> <asp:CreateUserWizardStep ID="CreateUserWizardStep2" Runat="server" Title="Sign Up for Your New Account"> </asp:CreateUserWizardStep> <asp:CompleteWizardStep ID="CompleteWizardStep2" Runat="server" Title="Completeeeeee"> </asp:CompleteWizardStep> </WizardSteps> </asp:CreateUserWizard>..........................
Register.aspx.cs: protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e) { Label2.Text = "you got registered"; Roles.AddUserToRole(CreateUserWizard1.UserName, "Friends"); CreateUserWizard cuw = (CreateUserWizard)sender; MembershipUser user = Membership.GetUser(cuw.UserName); Guid userId = (Guid)user.ProviderUserKey; //***** }After
Guid userId = (Guid)user.ProviderUserKey
I am very confused and not sure what to do. I understand that i need to email an url with the userID to user's email upon registration, but how to do this, and when user clicks that url in that email, how that url will automatically come to activation page? Please give suggestion and code example.
c sharp ASP .NET 2. 0 .net SMTP Registration Wizard Activate New User
dzieba
Contributor
2670 Points
602 Posts
Re: How to send user confirmation email after registration with activation link?
Jan 21, 2010 06:51 PM|LINK
Add a mail definition to your CreateUserWizard:
<MailDefinition BodyFileName="~/Docs/NewUserEmailBody.txt" CC="postmaster@mysite.com" From="postmaster@mysite.com" Subject="Welcome to my site"> </MailDefinition>Now, the NewUserEmailBody.txt has your email with <Placeholders> for actual values.
<%NEWUSERID%> is one field. All fields in your email will get replaced automatically and the email will also be sent automatically. You don't need to do anything else except change your ASP code to what I showed you and then write the email with placeholders. Here is the example from mine. Mine does not have an activation page but you can see the other available replacement fields.
In your activation page, do something like:
protected void Page_Load(object sender, EventArgs e) { Guid oGuid = new Guid(Request.QueryString["ID"]); //Guid oGuid = new Guid(); MembershipUser oUser = Membership.GetUser(oGuid); if (oUser != null && oUser.IsApproved == false) { oUser.IsApproved = true; Membership.UpdateUser(oUser); System.Web.Security.FormsAuthentication.RedirectFromLoginPage(oUser.UserName, false); } }ashley2009
Member
19 Points
46 Posts
Re: How to send user confirmation email after registration with activation link?
Jan 21, 2010 10:51 PM|LINK
Hello dzieba,
I cannot figure out how to construct the ActivationPage url with the Guid.
I came up to this point:
Registration.aspx.cs
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
Roles.AddUserToRole(CreateUserWizard1.UserName, "Friends");
CreateUserWizard cuw = (CreateUserWizard)sender;
MembershipUser user = Membership.GetUser(cuw.UserName);
Guid userId = (Guid)user.ProviderUserKey;
string ID = userId.ToString();
}
I think that
string ID will be part of that ActivationPage url (like http://localhost:72/ActivationPage.aspx?str=axxxxxxxx), which I have to put in the MailFile.txt of
MailDefinition
However, I am not sure how to construct this url and send this unique url to whenever a user registers.
For clearification, I am showing the codes:
Register.aspx:
<asp:CreateUserWizard ID="CreateUserWizard1" Runat="server" ContinueDestinationPageUrl="default.aspx" EmailRegularExpression="\S+@\S+\.\S+" EmailRegularExpressionErrorMessage="The email format is invalid." ForeColor="#333333" Font-Names="Verdana" Font-Size="12px" CreateUserButtonStyle-CssClass="button" oncreateduser="CreateUserWizard1_CreatedUser" LoginCreatedUser="false" DisableCreatedUser="True"> <MailDefinition BodyFileName="MailFile.txt" From="xxx@xxx.com" Subject="Word has it, you forgot your password?"> </MailDefinition> <WizardSteps> <asp:CreateUserWizardStep ID="CreateUserWizardStep2" Runat="server" Title="Sign Up for Your New Account"> </asp:CreateUserWizardStep> <asp:CompleteWizardStep ID="CompleteWizardStep2" Runat="server" Title="Completeeeeee"> </asp:CompleteWizardStep> </WizardSteps> </asp:CreateUserWizard>Register.aspx.cs:
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e) { Roles.AddUserToRole(CreateUserWizard1.UserName, "Friends"); CreateUserWizard cuw = (CreateUserWizard)sender; MembershipUser user = Membership.GetUser(cuw.UserName); Guid userId = (Guid)user.ProviderUserKey; string ID = userId.ToString(); }MailFile.txt:
Your account was set up on our Web site with the following: Username: <%UserName%> Password key: <%Password%> http://localhost:72/ActivationPage.aspx Thank you for creating an account with our Web site.ActivationPage.aspx.cs
protected void Page_Load(object sender, EventArgs e) { Guid oGuid = new Guid(Request.QueryString["ID"]); MembershipUser oUser = Membership.GetUser(oGuid); if (oUser != null && oUser.IsApproved == false) { oUser.IsApproved = true; Membership.UpdateUser(oUser); System.Web.Security.FormsAuthentication.RedirectFromLoginPage(oUser.UserName, false); } }ActivationPage.aspx.cs is your suggested code but is not working because when the user registers, the user only gets
C# ASP.net 2.0 Registration Wizard Activation Url Membership
dzieba
Contributor
2670 Points
602 Posts
Re: How to send user confirmation email after registration with activation link?
Jan 21, 2010 10:57 PM|LINK
<%NEWUSERID%>
Like I said in the previous post, you can use additional tags in your email. The example email did not have the <%NEWUSERID%> in it but you can add it to yours.
change you email to iinclude this field.
Your account was set up on our Web site with the following: Username: <%UserName%> Password key: <%Password%> http://localhost:72/ActivationPage.aspx?id=<%NEWUSERID%> Thank you for creating an account with our Web site.let me know if this worked
ashley2009
Member
19 Points
46 Posts
Re: How to send user confirmation email after registration with activation link?
Jan 21, 2010 11:29 PM|LINK
Hello,
still cannot make the link work with the query string.
The email body appears as
Your account was set up on our Web site with the following:
Username: myName
Password key: myPassWord
http://localhost:72/ActivationPage.aspx?id=<%NEWUSERID%>
Thank you for creating an account with our Web site.
Although the Username and the Password appears in the email but the UserId value does not come from the aspnetdb. What maight be wrong? What am I missing? I thought instead of <%NEWUSERID%> it would be <%UserID%> as in aspnetdb database, in the aspnet_Users table, the column name is UserID, but that does not work either. Please help.
C# Activation Page registration Wizard .NET 2.0 SMTP Membership
dzieba
Contributor
2670 Points
602 Posts
Re: How to send user confirmation email after registration with activation link?
Jan 21, 2010 11:46 PM|LINK
I thought that I read somewhere where this is a field. There does not seem to be any complete documentatio on all the fields that can be used.
So here is your other solution. Obviously you already have the mail server working so this will be easy.
Step 1. Remove that mail definition from your wizard
Step 2. Add this try catch block to your function UserCreated to send the email
Try Dim insMail As New MailMessage("postmaster@mysite.com", CreateUserWizard1.Email, "my subject", BuildEmailBody) Dim c As System.Net.Mail.SmtpClient = New SmtpClient("mail.mysite.com") insMail.CC.Add("postmaster@mysite.com") c.Send(insMail) Catch xx As Exception End TryCreate a function that returns a string called 'BuildEmailBody' and create any body that you want. This function is called in the MailMessage constructor.
ashley2009
Member
19 Points
46 Posts
Re: How to send user confirmation email after registration with activation link?
Jan 22, 2010 12:43 AM|LINK
Hi dzieba,
actually you are right from the beginning, and sorry if I have confused you. Per your old suggestions, now the whole process is working.
My mistake was having at Register.aspx
1) CreateUserWizard.oncreateduser
instead of
CreateUserWizard.OnSendingMail
The correct code is:
Register.aspx.cs:
protected void CreateUserWizard1_CreatedUser(object sender, MailMessageEventArgs e) { Roles.AddUserToRole(CreateUserWizard1.UserName, "Friends"); CreateUserWizard cuw = (CreateUserWizard)sender; MembershipUser user = Membership.GetUser(cuw.UserName); Guid userId = (Guid)user.ProviderUserKey; string verifyUrl = " http://localhost:72/ActivationPage.aspx?id=" + userId.ToString(); e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", verifyUrl); }Register.aspx:
<asp:CreateUserWizard ID="CreateUserWizard1" Runat="server" ContinueDestinationPageUrl="default.aspx" EmailRegularExpression="\S+@\S+\.\S+" EmailRegularExpressionErrorMessage="The email format is invalid." ForeColor="#333333" Font-Names="Verdana" Font-Size="12px" CreateUserButtonStyle-CssClass="button" OnSendingMail="CreateUserWizard1_CreatedUser" LoginCreatedUser="false" DisableCreatedUser="True"> <MailDefinition BodyFileName="MailFile.txt" From="xxx@xxx.com" Subject="Word has it, you forgot your password?"> </MailDefinition> <WizardSteps> <asp:CreateUserWizardStep ID="CreateUserWizardStep2" Runat="server" Title="Sign Up for Your New Account"> </asp:CreateUserWizardStep> <asp:CompleteWizardStep ID="CompleteWizardStep2" Runat="server" Title="Completeeeeee"> </asp:CompleteWizardStep> </WizardSteps> </asp:CreateUserWizard>And the MailFile.txt is:
Your account was set up on our Web site with the following: Username: <%UserName%> Password key: <%Password%> Verification Url: <%VerificationUrl%> Thank you for creating an account with our Web siteThank you for the suggestions about the MailDefinition and the QueryString information. I have found a good tutorial site about this also, which is:
http://www.asp.net/%28S%28pdfrohu0ajmwt445fanvj2r3%29%29/learn/security/tutorial-14-cs.aspx
C# ASP.net 2.0 .NET MailDefinition SMTP Membership
thulasiram12...
Member
393 Points
200 Posts
Re: How to send user confirmation email after registration with activation link?
Oct 04, 2011 06:22 AM|LINK
Hi ashley....
Your code is working fine....Thanks alot
let me clarify one doubt
After getting mail(verification link) from website to respective registered user . He needs to click the link in his account to activate it..
Once he clicks the link then only his account should activate..How to do for that
plz help me
Thanks alot in advance...
C# ASP.net 2.0 .NET MailDefinition SMTP Membership