Asp. Net registered email activation, asp.net email

Source: Internet
Author: User
Tags mailmessage smtpclient

Asp. Net registered email activation, asp.net email

Database Table Design


 

State: User status 0: Disabled 1: available default: 0, there is a UserGUID below, this field will be used to activate the account in the future

 

First, you need to write a form with a verification code. I will not write it .. The code for direct write processing is listed below

if (IsPostBack)
 2             {
 3 string userName = Request ["UserName"];
 4 string userPwd = Request ["UserPwd"];
 5 string userEmail = Request ["UserEmail"];
 6 string EmailGUID = Guid.NewGuid (). ToString ();
 7
 8 // UserInfosEntities
 9 if (CheckValidCode ())
10 {
11 if (InsertUserInfo (new userinfo ()
12 {
13 UserName = userName,
14 UserPwd = userPwd,
15 State = 0,
16 UserEmail = userEmail,
17 UserGUID = EmailGUID
18
19}))
20 {
twenty one                         
22 string str = Request.ServerVariables ["Http_Host"]; // This sentence means to get the domain name and port, because I am debugging locally, if the port is rebuilt, it will change--I am very depressed. .This is what the great god told me ...
23 MailMessage mailMsg = new MailMessage (); // To introduce System.Net Assembly
24 mailMsg.From = new MailAddress ("670196906@qq.com", "own name"); // source email address
25 mailMsg.To.Add (new MailAddress (userEmail, "name of the other party")); // destination email address. Can have multiple recipients
26 mailMsg.Subject = "Activate account"; // Subject of sending email
27 userName = Common.Base64.EncodeBase64 (Encoding.UTF8, userName); // This is to convert the passed name into base64. I tried Encoding. No, after a long search, the Chinese has been garbled, so I had to convert it to It looks like this. .

     
28 string emailStr
= string.Format ("Click the following activation link to activate your account http: // {0} /ActivUserInfo.aspx?UserName= {1} & GUID = {2}", str, userName, EmailGUID); // This is the future Send activation link to email



29 mailMsg.Body = emailStr; // The content of the email
30 mailMsg.IsBodyHtml = true; // Whether the content is HTML
31 mailMsg.BodyEncoding = Encoding.UTF8; // The encoding format is UTF-8
32 SmtpClient client = new SmtpClient ("smtp.qq.com"); // The SMTP server address of the mailbox used by the sender.
33 client.Credentials = new NetworkCredential ("account for sending mail", "password for sending mail"); // username and password of sender's mailbox.
34 client.Send (mailMsg); // Send mail
35 Response.Redirect ("/ Admin.aspx");
36
37}
38 else
39 {
40 Response.Redirect ("/ Login.aspx");
41
42}
43
44
45}
46 else
47 {
48
49 Message = "The verification code was entered incorrectly. Please re-enter it !!!";
50}

 

 

 

 

1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Web;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8 
 9 namespace WebDemoUserInfo
10 {
11     public partial class ActivUserInfo : System.Web.UI.Page
12     {
13         public string ActiveMessage { get; set; }
14         protected void Page_Load(object sender, EventArgs e)
15         {                                                        
16             string userName = Common.Base64.DecodeBase64(Encoding.UTF8, Request["UserName"]);   //Decrypt the passed UserName, Base64 code is behind
17 string gUid = Request ["GUID"]; //
18 if (userName! = Null && gUid! = Null)
19 {
20 int result = CheckUserInfo (userName, gUid);
21 switch (result)
twenty two                 {
23 case 0:
24 ActiveMessage = "Activation failed";
25 break;
26 case 1:
27 ActiveMessage = "Successfully activated";
28 break;
29 case 2:
30 ActiveMessage = "Cannot be reactivated !!!";
31 break;
32 defalut: ActiveMessage = "Unknown error, please contact system administrator !!!";  33                 }
34             }
35 
36 
37         }
38 
39         private int CheckUserInfo(string userName, string gUID)
40         {
41             try
42             {
43                 var db = new UserInfosEntities();
44                 if (db.userinfo.Count(i => i.UserName == userName && i.UserGUID == gUID) == 1)
45                 {
46                     var model = db.userinfo.FirstOrDefault(i => i.UserGUID == gUID);
47                     if (model != null && model.State == 0)
48                     {
49                         model.State = 1;
50 
51                     }
52                     else
53                     {
54                         return 2;
55                     }
56                     return db.SaveChanges() == 1 ? 1 : 0;
57                 }
58                 else
59                 {
60                     return 0;
61                 }
62             }
63             finally
64             {
65                 Dispose();
66             }
67         }
68     }
69 }
1 public static class Base64 2 {3 public static string EncodeBase64 (Encoding encoding, string source) 4 {5 byte [] bytes = encoding. getBytes (source); 6 return Convert. toBase64String (bytes); 7} 8 9 public static string DecodeBase64 (Encoding encoding, string result) 10 {11 byte [] bytes = Convert. fromBase64String (result); 12 return encoding. getString (bytes); 13} 14}Base64 encryption and decryption

I don't know how to study it all day... I think there is a lot to learn... come on...


Aspnet registered user authentication email activation account

Hi.baidu.com/...3.html
Simple and clear, there are comments, you have a look, hope to help you

Aspnet implements email verification to activate registered users

I will tell you

You need a field to store the user's verification number.

And then register

Int checkNumber = ran. Next (10000,999 99); generates a random entry and stores it in the database.

Then, send the following email:
String smtpServer = address
Int smtpPort = port
String userAccount = Account
String userPassword = Password
String userName = Name
String EmailAddress = EMAIL Address

System. Net. Mail. SmtpClient client = new SmtpClient (smtpServer, smtpPort );
Client. usedefacrecredentials = false;
Client. Credentials = new System. Net. NetworkCredential (userAccount, userPassword );
Client. DeliveryMethod = SmtpDeliveryMethod. Network;

MailAddress fromEmal = new MailAddress (EmailAddress, userName );
MailAddress toEmail = new MailAddress (strto );
System. Net. Mail. MailMessage message = new MailMessage (fromEmal, toEmail );
Message. Subject = strSubject;
Message. Body = strBody;
Message. BodyEncoding = System. Text. Encoding. UTF8;
Message. IsBodyHtml = true;

Client. Send (message );

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.