您的位置:首页 > 其它

.net自动发送邮件,适用于MD5或sha1加密后的密码取

2010-08-07 00:54 411 查看
用不可逆加密算法sha1或则MD5来存储密码,安全性能是比较高,但是随之而来的用户忘记密码,像管理员找回,管理员也没办法,只有从新生成一个密码,然后换算成加密值更新到数据库,但是这里如果是人工操作的话,一是耗费人力不少,二是不方便,所以我在网上找到了下面的代码,加工测试,成功,现分享给大家

功能:如果是找回密码的话,直接把生成前的原文本当做内容发送邮件给收件人,当然还能群发邮件,不过我不知道这 个速度怎么样,1个邮件还是快,多了应该没有一些专业的群发邮件软件强悍。

下面为网页后台代码

using System;
using System.Data;
using System.Configuration;
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.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
send();
}
private void send()
{
string from = "hexiaosong8@163.com"; //发送方邮箱
string subject = "测试邮件"; //标题

MailMessage newEmail = new MailMessage();

#region 发送方邮件
newEmail.From = new MailAddress(from, from);
#endregion

#region 发送对象,可群发
newEmail.To.Add(new MailAddress("10439474@qq.com")); //接收方邮箱一
//newEmail.To.Add(new MailAddress("132@hotmail.com")); //接收方邮箱二
#endregion

#region Subject
newEmail.Subject = subject; //标题
#endregion

#region Body
string strBody = "<p><b>这里是内容</b></p>"; //html格式,也可以是普通文本格式
newEmail.Body = strBody; //内容
#endregion

#region 附件
// Attachment MsgAttach = new Attachment(this.FileUpload1.PostedFile.FileName);//可通过一个FileUpload地址获取附件地址
//newEmail.Attachments.Add(MsgAttach);
#endregion

#region Deployment
newEmail.IsBodyHtml = true; //是否支持html
newEmail.Priority = MailPriority.Normal; //优先级
#endregion

//发送方服务器信息
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("hexiaosong8@163.com", "这是密码,不显示了");
smtpClient.Host = "smtp.163.com"; //主机

//smtpClient.Send(newEmail); //同步发送,程序将被阻塞

#region 异步发送, 会进入回调函数SendCompletedCallback,来判断发送是否成功
smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);//回调函数
string userState = "测试";
smtpClient.SendAsync(newEmail, userState);
#endregion

}

private static void SendCompletedCallback(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Cancelled) //邮件发送被取消
{

}
if (e.Error != null) //邮件发送失败
{

}
else //发送成功
{

}
}

//:<%@ Page%>添加Async="true"

}

页面代码就不用写了,直接是个id=“Button1”的提交按钮,若要传送附件,加个id为“FileUpload1” 的FileUpload控件,上面我已经将这个功能注释掉了

我个人建议若用于固定的发送人和信息,将这个类提取到另外一个类中

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: