配置文件的配置:
<add key="Email" value="邮箱用户名"/> <add key="Emailpwd" value="邮箱密码"/> <add key="EmailUrl" value="回调地址"/> <add key="Host" value="邮箱host"/>
/// <summary> /// 发送邮箱(注:QQ邮箱需要开通POP、STMP) /// </summary> /// <param name="Email">发件人邮箱</param> /// <param name="pwd">发件人邮箱密码</param> /// <param name="sendEmail">接收人的邮箱</param> /// <param name="title">标题</param> /// <param name="content">内容</param> /// <param name="Host">代理服务器(如:smtp.qq.com)</param> public static bool SendMail(string Email, string pwd, string sendEmail, string title, string content, string Host) { try { System.Net.Mail.MailMessage objMailMessage;//实例化对象 // 创建邮件消息 objMailMessage = new System.Net.Mail.MailMessage(); objMailMessage.From = new MailAddress(Email);//发件人邮箱 objMailMessage.To.Add(sendEmail); //收件人邮箱 objMailMessage.Subject = title;//邮件的标题 objMailMessage.Body = content;//邮件的内容 objMailMessage.IsBodyHtml = true; //设置邮件的正文是否可以是html格式的。 objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码格式 objMailMessage.Priority = System.Net.Mail.MailPriority.High;//设置此邮件的优先级 SmtpClient smtpClient = new SmtpClient(Host);//代理服务器 smtpClient.Credentials = new System.Net.NetworkCredential(Email, pwd);//邮件登陆信息 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtpClient.Host = Host;//Host.Substring(0, Host.IndexOf(‘.‘)) + "." + objMailMessage.From.Host; smtpClient.Send(objMailMessage);//发送 return true; } catch( Exception ex ) { return false; } } /// <summary> /// 将字符串反序列化为对象 /// </summary> /// <param name="str"></param> /// <returns></returns> public static object StringDeserializeForObject(string str) { str = HttpContext.Current.Server.UrlDecode(str); Stream serializationStream = new MemoryStream(Convert.FromBase64String(str)); IFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(serializationStream); } /// <summary> /// 将对象序列化为字符串 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ObjectSerializeForString(object obj) { IFormatter formatter = new BinaryFormatter(); string s = ""; using (MemoryStream stream = new MemoryStream()) { formatter.Serialize(stream, obj); stream.Seek(0L, SeekOrigin.Begin); byte[] buffer = new byte[stream.Length]; s = Convert.ToBase64String(stream.ToArray()); return HttpContext.Current.Server.UrlEncode(s); } } public static bool SendMailString(Object content, string ToEmail) { string Email = ConfigurationManager.AppSettings["Email"].ToString(); string Emailpwd = ConfigurationManager.AppSettings["Emailpwd"].ToString(); string EmailUrl = ConfigurationManager.AppSettings["EmailUrl"].ToString(); string Host = ConfigurationManager.AppSettings["Host"].ToString(); string url = EmailUrl + "LostPwd.aspx?lostid=" + ObjectSerializeForString(content); return SendEmailDefault(Email, Emailpwd, ToEmail, Host, url); } //默认发送格式 public static bool SendEmailDefault(string Email, string pwd, string ToEmail, string Host,string url) { StringBuilder MailContent = new StringBuilder(); MailContent.Append("亲爱的*****会员:<br/>"); MailContent.Append(" 您好!你于"); MailContent.Append(DateTime.Now.ToLongTimeString()); MailContent.Append("通过******管理中心审请找回密码。<br/>"); MailContent.Append(" 为了安全起见,请用户点击以下链接重设个人密码:<br/><br/>"); MailContent.Append("<a href=‘" + url + "‘>" + url + "</a><br/><br/>"); return SendMail(Email, pwd, ToEmail, "******回密码", MailContent.ToString(), Host); }
事件:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { ImageButton1.Enabled = false; CMS_UsersBLL userbll = new CMS_UsersBLL(); CMS_Users model = new CMS_Users(); model = userbll.GetModel(this.regname.Value); if (model != null) { string Email = model.Mail; string time = DateTime.Now.ToString(); Object obj = new Object(); obj = this.regname.Value + "," + time; bool isOk = Helper.SendMailString(obj, Email); if (isOk) { ImageButton1.Enabled = true; Maticsoft.Common.MessageBox.Show(this, "已发送到你的邮箱请及时查看!"); } else { ImageButton1.Enabled = true; Maticsoft.Common.MessageBox.Show(this, "发送失败!!"); } } else { ImageButton1.Enabled = true; Maticsoft.Common.MessageBox.Show(this, "用户名不存在"); } } }
接受返回:
protected void updepwd_Click(object sender, ImageClickEventArgs e) { try { Object lostid = Helper.StringDeserializeForObject(Helper.GetQueryStringToString("lostid")); string strr = lostid.ToString(); string name = strr.Split(‘,‘)[0]; string passwd = DEncrypt.Encrypt(this.regPasw2.Value); string pwd = DEncrypt.Encrypt(passwd); CMS_Users user = new CMS_UsersBLL().GetModel(name); if (user != null) { user.User_Name = name; user.User_Pwd = pwd; if (new CMS_UsersBLL().Update(user)) { Response.Redirect("Register.aspx?update=update"); } } } catch (Exception) { Maticsoft.Common.MessageBox.ShowAndRedirect(this, "非法地址", "Index.aspx"); return; } }