Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to send email with verification link when user completes his registration in asp.net c# this is my code...plz tell me that how can i send verification link on users email when he create an account.....plz help....thanks in advance



C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Linq;
using System.Xml.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Configuration;

public partial class Registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
     SqlConnection con = new SqlConnection(
     ConfigurationManager.ConnectionStrings["BMCConnectionString"].ConnectionString);

    // open the data connection.  
    con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
     if (IsPostBack)
    {
        Response.Write("<script>alert('Thanking you .....Registration Successful')</script>");
        TextBoxDate.Text = DateTime.Now.ToString();

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BMCConnectionString"].ConnectionString);

        con.Open();
        string insCmd = "insert into Registration(UserName, Password, RePassword, Email, FullName ,Date ,Month, Year, Gender, Area, Date1) values (@UserName, @Password, @RePassword, @Email, @FullName ,@Date ,@Month, @Year, @Gender, @Area, @Date1)";
        SqlCommand insertUser = new SqlCommand(insCmd, con);
        insertUser.Parameters.AddWithValue("@UserName", TextBoxUN.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        insertUser.Parameters.AddWithValue("@RePassword", TextBoxRP.Text);
        insertUser.Parameters.AddWithValue("@Email", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@FullName", TextBoxFN.Text);
        insertUser.Parameters.AddWithValue("@Date", DropDownListDate.SelectedItem.Text);
        insertUser.Parameters.AddWithValue("@Month", DropDownListMonth.SelectedItem.Text);
        insertUser.Parameters.AddWithValue("@Year", DropDownListYear.SelectedItem.Text);
        insertUser.Parameters.AddWithValue("@Gender", RadioButtonList1.SelectedItem.Text);
        insertUser.Parameters.AddWithValue("@Area", DropDownListArea.SelectedItem.Text);
        insertUser.Parameters.AddWithValue("@Date1", TextBoxDate.Text);
{
                //create the mail message
                MailMessage mail = new MailMessage();
                //set the addresses
                mail.From = new MailAddress("salvevishal9@gmail.com");
                mail.To.Add(TextBoxEA.Text);
                //set the content
                mail.Subject = "This is an email";
                mail.Body = "this is the body content of the email.";
                //send the message
                
                SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.Send(mail);

        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();

        }
        catch (Exception er)
        {
            Response.Write(er);
        }
    }
}
Posted
Updated 14-Mar-13 9:27am
v2
Comments
Richard C Bishop 14-Mar-13 14:19pm    
What is the problem you are having?
vishal salve 14-Mar-13 15:27pm    
i want to send a mail to user when he register
Rohit Syal 17-Jun-15 2:16am    
where is the link which is used to verify user ?
Rohit Syal 17-Jun-15 2:17am    
where u have generated a that Link ?

1 solution

You're actually asking a several things in one question, so I'll have to break it down. These will be fairly high level steps, hopefully enough to get you on the right path. If you're still stuck then maybe ask a question related specifically to the step you're stuck on...

1. Email Sending util
Consider setting up a dedicated class to handle the sending of email. This class will look something like this: Simple SMTP E-Mail Sender in C#… Console application[^]

2. Database change
You need a flag on your user table (field of type bit) which represents whether they've verified themselves or not.

3. Creating the verification page
Create a new page which is the target of the link. This page will check the query string (recommend you encrypt these) for the user's id. When they enter their password it would validate this and then mark them as verified on the new column.

Then you just add a link in the email body to the above page with the user id as an encrypted parameter.

Hope that helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900