ASP. NET, combined with databases, sends emails to retrieve passwords

Source: Internet
Author: User

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Using system. net. mail;
Public partial class default6: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Multiview1.activeviewindex = 0;

}
}
Protected void button#click (Object sender, eventargs E)
{
String name = textbox2.text. tostring ();
String SQL = "select count (*) from usermember where username = '" + name + "'";
Int n = sqldb. execscalar (SQL );
If (n> 0)
{
Multiview1.activeviewindex = 1;

String sql1 = "select pwdanswer, pwdquestion from usermember where username = '" + name + "'";
Sqldatareader DR = sqldb. getreader (sql1 );
While (dr. Read ())
{
Textbox3.text = Dr ["pwdquestion"]. tostring ();

}
Dr. Dispose ();
}
Else
{
Multiview1.activeviewindex = 0;

Response. Write ("<SCRIPT> alert ('Do not have this username'); history. Back (); </SCRIPT> ");
}
}
Protected void button2_click (Object sender, eventargs E)
{
Textbox TXT = new Textbox ();

String an = "";
String email = "";
String Pwd = "";
Random pwd1 = new random ();
Pwd = pwd1.next (100000,999 9999). tostring ();
TXT. Text = PWD;
String name = textbox2.text. tostring ();
String SQL = "select pwdanswer, pwdquestion, user_email, userpwd from usermember where username = '" + name + "'";
Sqldatareader DR = sqldb. getreader (SQL );
While (dr. Read ())
{
An = Dr ["pwdanswer"]. tostring ();
Email = Dr ["user_email"]. tostring ();
// Pwd = Dr ["userpwd"]. tostring ();

}
If (textbox4.text =)
{
String sql2 = "Update usermember set userpwd = '" + sqldb. MD5 (txt) + "'where username ='" + name + "'";
Sqldb. execsql (sql2 );
Mailaddress add = new mailaddress ("XXX@163.com ");
If (send (ADD, email, "retrieve password", PWD + "Please use this password to log on and change your password in time! Thank you ..."))
{
Response. Write ("<SCRIPT> alert ('The answer is correct. An email has been sent to your mailbox. Please check it! '); Location. href =' "+ request. url +" '; </SCRIPT> ");
}
Else
{
Response. Write ("<SCRIPT> alert ('sending failed'); javascript: history. Back (); </SCRIPT> ");
}
}
Else
{
Response. Write ("<SCRIPT> alert ('answer error'); javascript: history. Back (); </SCRIPT> ");
}

}

/// <Summary>
/// Send an email
/// </Summary>
/// <Param name = "messagefrom"> sender's email address </param>
/// <Param name = "messageto"> recipient's email address </param>
/// <Param name = "messagesubject"> subject </param>
/// <Param name = "messagebody"> email content </param>
/// <Returns> </returns>
Public static bool send (mailaddress messagefrom, string messageto, string messagesubject, string messagebody)
{
Mailmessage message = new mailmessage ();

Message. From = messagefrom;
Message. to. Add (messageto); // you can specify multiple recipient email addresses for group sending.
Message. Subject = messagesubject;
Message. Body = messagebody;
Message. isbodyhtml = true; // whether it is in HTML Format
Message. Priority = mailpriority. High; // mail priority

Smtpclient SC = new smtpclient ();
SC. Host = "smtp.163.com"; // specify the server address or IP address of the email.
SC. Port = 25; // specify the sending Port
SC. Credentials = new system. net. networkcredential ("XXX@163.com", "XXX"); // specify the username and password for logging on to the server
Try
{
SC. Send (Message); // send an email
}
Catch (smtpexception ex)
{

// Return ex. message;
}
Return true;

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.