Activation verification mainly implements the following functions:
When a user registers data on the registration page, an activation link is sent to the user's registered email address. When a user clicks this link, the user-registered account can be activated, otherwise, you cannot log in even if you enter the correct user name and password on the logon page.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Data;
Using System. Data. SqlClient;
Using System. Configuration;
Using System. Net;
Using System. Net. Mail;
Namespace ValidateEmail
{
Public partial class WebForm1: System. Web. UI. Page
{
String sqlstr = ConfigurationManager. ConnectionStrings ["sqlstr"]. ConnectionString;
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void btnLogon_Click (object sender, EventArgs e)
{
Using (SqlConnection sqlcnn = new SqlConnection (sqlstr ))
{
Using (SqlCommand sqlcmm = sqlcnn. CreateCommand ())
{
Sqlcmm. CommandText = "insert into T_mail (id, username, userpas, mail) values (@ id, @ username, @ userpas, @ mail )";
Sqlcmm. Parameters. AddWithValue ("@ id", System. Guid. NewGuid (); // insert a GUID to the database
Sqlcmm. Parameters. AddWithValue ("@ username", this.txt UerName. Text );
Sqlcmm. Parameters. AddWithValue ("@ userpas", this.txt Password. Text );
Sqlcmm. Parameters. AddWithValue ("@ mail", this.txt Mail. Text );
Sqlcnn. Open ();
Int I = sqlcmm. ExecuteNonQuery ();
If (I> 0)
{
SendMail ();
Response. Write ("<script> alert ('registration successful, please go to your mailbox to activate it! ') </Script> ");
}
Else
{
Response. Write ("<script> alert ('registration failed! ') </Script> ");
}
}
}
}
Private void SendMail ()
{
MailMessage msg = new MailMessage ();
Msg. From = new MailAddress ("1607977350@qq.com"); // the sender's email address
Msg. Subject = "* Welcome to your registration *"; // Email Subject
Msg. Body = "<a href = 'HTTP: // localhost: 38966/WebForm2.aspx? Name = "maid. UerName. Text +" '> click to activate your account </a> "; // mail body
Msg.To.Add(this.txt Mail. Text );
Msg. IsBodyHtml = true; // whether the mail body supports html values
SmtpClient SC = new SmtpClient ();
SC. Host = "smtp.qq.com ";
SC. Port = 25;
NetworkCredential nc = new NetworkCredential ("1607977350", "*********"); // verify the credential 1607977350: it is an email account, * *******: the email password.
SC. Credentials = nc;
SC. Send (msg );
}
}
}