Use email activation for authentication during ASP. Net Registration

Source: Internet
Author: User
Tags email account mailmessage smtpclient

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 );
}
}

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.