C # implement send mail retrieve password function

Source: Internet
Author: User
Tags mail code md5 encryption mailmessage smtpclient

First, let's analyze the idea:

Three-step walk:

1. To send a message first

2. Let the user click the URL in the message

3. Implementing the Change Password

1. In order to ensure security, the URL that is sent to the message needs to be generated, the main parameters (user name, expiration time, key (key needs to automatically generate random code on each run), IP, etc.) and then send the URL to the mailbox.

2. Save the sent parameters (recommended to be saved in the database)

3. Resolve the URL: first, according to the user name from the database to find the key key and expiration time, no means that the request is forged or expired, and then verify the signature, verify the expiration time, both verify through, you can modify the password, after the password modification, delete the database records.

Second, the specific code implementation

The

is divided into Send mailbox page and Change Password page:
I. Send Mailbox page
String strusername = TxtUserName.Value.Trim ();
String streail = TxtEmail.Value.Trim ();
//Generate random password
string rand = "";
Random rampwd = new Random ();
String pwd = Rampwd.next (100000, 9999999). ToString ();
Rand = pwd;
String key = rand + strName + email + "Base";


if (!string. IsNullOrEmpty (strUserName) &&!string. IsNullOrEmpty (Streail))
{
Send mail
StringBuilder sb = new StringBuilder ();
Sb. Append ("Dear" + strUserName + "Hello:<br/><br/>");
Sb. Append ("Click on the link below to set a new password.) <br/><br/> ");
Sb. Append ("<a href =\" http://www.xxxx.com/findpwd.aspx?key= "+ key +" &time= "+ Time +" \ ">http://www.xxxx.com/ Findpwd.aspx?key= "+ key +" &time= "+ Time +" </a><br/><br/> ");
Sb. Append ("(If you can't click the URL link address, copy it and paste it into the browser's address input box, and then click Enter.) ) <br/><br/> ");
Sb. Append ("Note: please use it within 24 hours of receiving the message, otherwise the link will expire.") <br/><br/> ");
Sb. Append ("We will, as always, dedicated to serve you!") <br/><br/> ");
String messagebody = sb. ToString ();
Sends (Streail, "[email protected]", "xxxx--retrieve password", MessageBody, "123456");

Adding data to the Find_password_log table
Addfindpassword (strUserName, Streail,key);

Response.Write ("<script>alert" e-Mail has been sent to your mailbox, please pay attention to check! '); location.href= '/';</script> ');

}
}


Adding data to the table Find_password_log
private void Addfindpassword (string strName, string email, string key)
{
Insert a randomly generated password (MD5 encryption) into the Find_password_log table, time, IP
SummerBase.BLL.find_password_log bllfind = new SummerBase.BLL.find_password_log ();
SummerBase.Model.find_password_log modelfind = new SummerBase.Model.find_password_log ();
if (modelfind! = null)
{
MODELFIND.MD5 = Mfunction.md5new (key). ToString ();
Modelfind.createtime = SummerBase.Utils.Util.TimeToUnixTimes (DateTime.Now.ToString ());
Modelfind.ip = Page.Request.UserHostAddress;

Time = Modelfind.createtime;
key = MODELFIND.MD5;
}
Bllfind.add (Modelfind);

}


Send mail code
public static void sends (string email, string formto, string content, String body, string upass)
{
String name = "[email protected]";

String smtp = "smtp.exmail.sina.com";

SmtpClient _smtpclient = new SmtpClient ();
_smtpclient.deliverymethod = smtpdeliverymethod.network;//Specify how e-mail is sent
_smtpclient.host = SMTP; Specify the SMTP server
_smtpclient.credentials = new System.Net.NetworkCredential (name, upass);//user name and password
MailMessage _mailmessage = new MailMessage ();
Sender, Sender name
_mailmessage.from = new MailAddress (Formto, "xxxx Company");
Recipient
_mailmessage.to.add (email);
_mailmessage.subjectencoding = System.Text.Encoding.GetEncoding ("gb2312");
_mailmessage.subject = content;//Theme

_mailmessage.body = body;//Content
_mailmessage.bodyencoding = System.Text.Encoding.GetEncoding ("gb2312");//Body Code
_mailmessage.isbodyhtml = true;//is set to HTML format
_mailmessage.priority = mailpriority.high;//Priority
Try
{
_smtpclient.send (_mailmessage);
}
catch (Exception)
{

}
}

Second, change the password

Compare the parameters that were added to the data, and if the validation passes, change the password and, if the validation fails, send a prompt to resend the message.

Specific parameters explained:
public void Sendsmtpemail (string strsmtpserver, String strfrom, String Strfrompass, String Strto, String strsubject, Strin G strbody)
{
System.Net.Mail.SmtpClient client = new SmtpClient (strsmtpserver);
Client. useDefaultCredentials = false;
Client. Credentials =
New System.Net.NetworkCredential (Strfrom, Strfrompass);
Client. Deliverymethod = Smtpdeliverymethod.network;

System.Net.Mail.MailMessage message =
New MailMessage (Strfrom, Strto, Strsubject, strbody);
Message. bodyencoding = System.Text.Encoding.UTF8;
Message. Isbodyhtml = true;
Client. Send (message);
}
The first parameter, if it's a 163 mailbox, write smtp.163.com.
Second parameter sender's account number
Third parameter sender password
Fourth parameter recipient account number
Fifth parameter topic
The sixth parameter content.

To send a mailbox, the main call is a method:

public void Sendsmtpemail (string strsmtpserver, String strfrom, String Strfrompass, String Strto, String strsubject, Strin G strbody) {

The Simple Mail Transfer Protocol (Simple Mail Transfer Protocol) SMTP is the hosting protocol used by the business interoperability interface MM4 in the 3GPP Multimedia Messaging specification

At present, the host protocol of this interface is used in the deployment of Multimedia messaging service system in the network.

System.Net.Mail.SmtpClient client = new SmtpClient (strsmtpserver);

Client. useDefaultCredentials = false;//is sent with the request

Client. Credentials = new System.Net.NetworkCredential (strfrom, strfrompass);//Gets or sets the credentials that verify the identity of the sender

Client. Deliverymethod = smtpdeliverymethod.network;//Specifies how outgoing messages are processed t//e-mail sent to the SMTP server over the network

System.Net.Mail.MailMessage message = new MailMessage (strfrom, Strto, Strsubject, strbody); Indicates that the program can use

--system.net.mail.smtpclient (indicates that the application can use the Simple Mail Transfer Protocol to send Í e-mail messages) The class sends E-mail

Message. bodyencoding = system.text.encoding.utf8;//Set message encoding to character encoding

Message. isbodyhtml = true;//setting indicates whether the value of the message body is in HTML format

Client. Send (message);//sends the specified message to the STMP server for delivery

}

Explanation of the parameters involved:

The first parameter is 163 if the mailbox is written smtp.163.com, using the service string strsmtpserver = "smtp.qq.com";

Sender account String strfrom = "[email protected]";

Sender Password String strfrompass = "chen15993920970";

Who to send string strto = "[email protected]";

The subject string strsubject = "hem";

Send content string strbody = "Your Verification code is:" + yanzhengma;



C # implement send mail retrieve password function

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.