using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
namespace SendMail
{
/// <summary>
/// 邮件发送类
/// 作者:ken.io
/// </summary>
/// PS:如果asp.net使用异步发送,请在发送页面的Page标签中加上 Async="true" 标记。
public class MailHelper
{
/// <summary>
/// 发送邮件,不使用SSL协议加密
/// </summary>
/// <param name="mailTitle">邮件标题</param>
/// <param name="mailContent">邮件内容</param>
/// <param name="mailAddress">收件人地址集合</param>
public void SendMail(string mailTitle, string mailContent, params string[] mailAddress)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
//添加收件人
foreach (string address in mailAddress)
{
msg.To.Add(address);
}
//设置发件邮箱地址,发件人姓名,以及邮件编码
msg.From = new MailAddress("yourmail@163.com", "Sender", System.Text.Encoding.UTF8);
//设置邮件标题
msg.Subject = mailTitle;
//设置邮件标题编码
msg.SubjectEncoding = System.Text.Encoding.UTF8;
//设置邮件内容
msg.Body = mailContent;
//设置邮件内容编码
msg.BodyEncoding = System.Text.Encoding.UTF8;
//指定是否是HTML邮件
msg.IsBodyHtml = false;
//设置邮件优先级
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//设置邮箱账号密码
client.Credentials = new System.Net.NetworkCredential("yourmail@163.com", "password");
//设置smtp服务器
client.Host = "smtp.163.com";
object userState = msg;
try
{
//异步发送
client.SendAsync(msg, userState);
//同步发送
//client.Send(msg);
JScript.Alert("邮件发送成功!");
}
catch (System.Net.Mail.SmtpException ex)
{
JScript.Alert(ex.Message);
}
}
/// <summary>
/// 发送邮件,使用SSL协议加密
/// </summary>
/// <param name="mailTitle">邮件标题</param>
/// <param name="mailContent">邮件内容</param>
/// <param name="mailAddress">收件人地址集合</param>
public void SendMailSSL(string mailTitle, string mailContent, params string[] mailAddress)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
//添加收件人
foreach (string address in mailAddress)
{
msg.To.Add(address);
}
//设置发件邮箱地址,发件人姓名,以及邮件编码
msg.From = new MailAddress("yourmail@163.com", "Sender", System.Text.Encoding.UTF8);
//设置邮件标题
msg.Subject = mailTitle;
//设置邮件标题编码
msg.SubjectEncoding = System.Text.Encoding.UTF8;
//设置邮件内容
msg.Body = mailContent;
//设置邮件内容编码
msg.BodyEncoding = System.Text.Encoding.UTF8;
//指定是否是HTML邮件
msg.IsBodyHtml = false;
//设置邮件优先级
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//设置邮箱账号密码
client.Credentials = new System.Net.NetworkCredential("yourmail@163.com", "password");
//设置邮箱服务器使用的SSL协议端口 -可能个别要见服务器要求设置SSL协议端口
//client.Port = 25;
//设置smtp服务器
client.Host = "smtp.163.com";
//使用ssl加密
client.EnableSsl = true;
object userState = msg;
try
{
//异步发送
//client.SendAsync(msg, userState);
//同步发送
client.Send(msg);
JScript.Alert("邮件发送成功!");
}
catch (System.Net.Mail.SmtpException ex)
{
JScript.Alert(ex.Message);
}
}
/// <summary>
/// 本地发送邮件
/// </summary>
/// <param name="mailTitle">邮件标题</param>
/// <param name="mailContent">邮件内容</param>
/// <param name="mailAddress">收件人地址集合</param>
public void SendMailLocal(string mailTitle, string mailContent, params string[] mailAddress)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
//添加收件人
foreach (string address in mailAddress)
{
msg.To.Add(address);
}
//设置发件邮箱地址,发件人姓名,以及邮件编码
msg.From = new MailAddress("server@local.com", "Sender", System.Text.Encoding.UTF8);
//设置邮件标题
msg.Subject = mailTitle;
//设置邮件标题编码
msg.SubjectEncoding = System.Text.Encoding.UTF8;
//设置邮件内容
msg.Body = mailContent;
//设置邮件内容编码
msg.BodyEncoding = System.Text.Encoding.UTF8;
//指定是否是HTML邮件
msg.IsBodyHtml = false;
//设置邮件优先级
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//设置smtp服务器地址
client.Host = "localhost";
object userState = msg;
try
{
//异步发送
//client.SendAsync(msg, userState);
//同步发送
client.Send(msg);
JScript.Alert("邮件发送成功!");
}
catch (System.Net.Mail.SmtpException ex)
{
JScript.Alert(ex.Message);
}
}
}
}
本文由 ken.io 创作,采用CC BY 3.0 CN协议 进行许可。 可自由转载、引用、甚至修改,但需署名作者且注明出处。