使用MailMessage和SmtpClient类实现asp.net的邮件发送(有详细注释并已测试通过)
1. MailMessage类主要是用于获取邮件相关信息
2. SmtpClient类主要用于同步或异步发送邮件
Html代码如下:
代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
后台代码如下:
代码
protected void Button1_Click(object sender, EventArgs e)
{
SendEmail();
}
public void SendEmail()
{
string from = "123@163.com"; //发送方邮箱
string subject = "测试邮件"; //标题
MailMessage newEmail = new MailMessage();
#region 发送方邮件
newEmail.From = new MailAddress(from, from);
#endregion
#region 发送对象,可群发
newEmail.To.Add(new MailAddress("123@vip.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("发送方邮箱地址:如123@163.com", "发送方邮箱密码:如123");
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 //发送成功
{
}
}
{
SendEmail();
}
public void SendEmail()
{
string from = "123@163.com"; //发送方邮箱
string subject = "测试邮件"; //标题
MailMessage newEmail = new MailMessage();
#region 发送方邮件
newEmail.From = new MailAddress(from, from);
#endregion
#region 发送对象,可群发
newEmail.To.Add(new MailAddress("123@vip.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("发送方邮箱地址:如123@163.com", "发送方邮箱密码:如123");
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"