摘要:會員忘記密碼寄送電子郵件
給自己一個紀錄,假如會員忘記自己的密碼,我們把密碼寄送給會員,如果有更好的方法。
希望大家能夠請幫我指點,謝謝大家。
public bool Mail_Send(string MailFrom, string MailTo, string MailSub, string MailBody, bool isBodyHtml)
{
try
{
//防呆
if (string.IsNullOrEmpty(MailFrom))
{//※有些公司的Mail Server會規定寄信人的Domain Name要是該Mail Server的Domain Name
MailFrom = "System@gmail.com.tw";
}
//命名空間: System.Web.Mail已過時,http://msdn.microsoft.com/zh-tw/library/system.web.mail.mailmessage(v=vs.80).aspx
//建立MailMessage物件
System.Net.Mail.MailMessage mms = new System.Net.Mail.MailMessage();
//指定一位寄信人MailAddress
mms.From = new MailAddress(MailFrom);
//信件主旨
mms.Subject = MailSub;
//信件內容
mms.Body = MailBody;
//信件內容 是否採用Html格式
mms.IsBodyHtml = isBodyHtml;
if (MailTo != null)//防呆
{
//for (int i = 0; i < MailTos.Length; i++)
//{
// //加入信件的收信人(們)address
//if (!string.IsNullOrEmpty(MailTos[0].Trim()))
//{
mms.To.Add(new MailAddress(MailTo));
//}
//}
}//End if (MailTos !=null)//防呆
//if (Ccs != null) //防呆
//{
// for (int i = 0; i < Ccs.Length; i++)
// {
// if (!string.IsNullOrEmpty(Ccs[i].Trim()))
// {
// //加入信件的副本(們)address
// mms.CC.Add(new MailAddress(Ccs[i].Trim()));
// }
// }
//}//End if (Ccs!=null) //防呆
//Attachment attfile = null;
//if (files != null && files.Count > 0)//有指定夾帶附檔
//{
// foreach (string fileName in files.Keys)
// {
// attfile = new Attachment(files[fileName], fileName);
// mms.Attachments.Add(attfile);
// }
//}
SmtpClient client = new SmtpClient("so-net.net.tw");//或公司、客戶的smtp_server
//以下可以省略,因為寄信不用帳密(除非客戶特別要求)
//client.Credentials = new System.Net.NetworkCredential("帳號", "密碼");
client.Send(mms);//寄出一封信
return true;//成功
}
catch (Exception)
{
return false;
}
}
protected void ibtn_Send_Click(object sender, ImageClickEventArgs e)
{
string ID = txt_ID.Text.Trim(), EMail = txt_Email.Text.Trim();
List lsp = new List();
string strsql = "SELECT EMail ,Name ,AccountID FROM Account WHERE ID = @ID AND EMail = @EMail";
lsp.Add(new SqlParameter("ID", ID));
lsp.Add(new SqlParameter("EMail", EMail));
DBConnction db = new DBConnction();
DataTable dt = db.SQLSelect(strsql, lsp);
try
{
string emailTo = dt.Rows[0][0].ToString(),
name = dt.Rows[0][1].ToString(),
AccountID = dt.Rows[0][2].ToString();
bool bb = this.Mail_Send("System@gmail.com", emailTo, "您的帳號", "Dear " + name + "\n以下是您的帳號:" + AccountID, true);
if (bb) Session["sendEMail"] = "YES";
else Session["sendEMail"] = "NO";
}
catch (Exception)
{
Session["sendEMail"] = "NOMAN";
}
Response.Redirect("~/Default.aspx");
}