using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Net;
|
using System.Net.Mail;
|
using System.Security.Cryptography;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace PublicCode.MailHelper
|
{
|
/// <summary>
|
/// 邮件发送助手
|
/// </summary>
|
public class SmtpMailHelper
|
{
|
/// <summary>
|
/// 发件服务器
|
/// </summary>
|
public string host { get; set; }
|
/// <summary>
|
/// 发件邮箱
|
/// </summary>
|
public string senderMail { get; set; }
|
/// <summary>
|
/// 发件邮箱的授权key
|
/// </summary>
|
public string senderKey { get; set; }
|
/// <summary>
|
/// 发件邮箱名
|
/// </summary>
|
public string senderName { get; set; }
|
|
|
/// <summary>
|
/// 使用默认参数实例化
|
/// </summary>
|
public SmtpMailHelper() {
|
host = "smtp.163.com";
|
senderMail = "linmolilin@163.com";
|
senderKey = "QWXSUGVVKNHCZVQJ";
|
senderName = "Moli";
|
|
}
|
/// <summary>
|
/// 指定发件服务器实例化
|
/// </summary>
|
/// <param name="host"></param>
|
public SmtpMailHelper(string host) {
|
this.host = host;
|
senderMail = "linmolilin@163.com";
|
senderKey = "QWXSUGVVKNHCZVQJ";
|
senderName = "Moli";
|
}
|
/// <summary>
|
/// 指定连接参数
|
/// </summary>
|
public SmtpMailHelper(string host,string senderMail,string senderKey) {
|
this.host = host;
|
this.senderMail = senderMail;
|
this.senderKey = senderKey;
|
senderName = "Moli";
|
}
|
/// <summary>
|
/// 自定所有连接参数
|
/// </summary>
|
/// <param name="host">发件服务器主机</param>
|
/// <param name="senderMail">发件邮箱</param>
|
/// <param name="senderKey">发件邮箱的授权key</param>
|
/// <param name="senderName">发件邮箱名</param>
|
public SmtpMailHelper(string host, string senderMail, string senderKey,string senderName)
|
{
|
this.host = host;
|
this.senderMail = senderMail;
|
this.senderKey = senderKey;
|
this.senderName = senderName;
|
}
|
|
/// <summary>
|
/// 发送邮件
|
/// </summary>
|
/// <param name="title">邮件主题</param>
|
/// <param name="htmlContent">邮件内容(html格式,例<p>hello</p>)</param>
|
/// <param name="toMails">收件人邮箱列表</param>
|
/// <param name="mailPriority">优先级,默认正常</param>
|
/// <returns>发送成功返回true</returns>
|
public bool Send(string title,string htmlContent,List<string> toMails, MailPriority mailPriority = MailPriority.Normal) {
|
if (!ParamCheck()) {
|
throw new Exception("未设置有效的连接参数");
|
}
|
|
if (toMails.Count == 0) {
|
throw new Exception("接收者不能为空");
|
}
|
if (string.IsNullOrWhiteSpace(title)) {
|
title = "无主题";
|
}
|
|
SmtpClient mail = new SmtpClient(this.host);
|
mail.Credentials = new NetworkCredential(senderMail, senderKey);
|
MailMessage msg = new MailMessage();
|
|
msg.IsBodyHtml = true;
|
msg.Subject = title;
|
msg.SubjectEncoding = Encoding.UTF8;
|
msg.BodyEncoding = Encoding.UTF8;
|
msg.Priority = mailPriority;
|
|
#region 邮件内容设置
|
StringBuilder body = new StringBuilder();
|
body.AppendLine("<html lang=\"zh-CN\">");
|
body.AppendLine("<head>");
|
body.AppendLine("<meta charset=\"utf-8\" />");
|
body.AppendLine("</head>");
|
body.AppendLine("<body>");
|
body.AppendLine(htmlContent);
|
body.AppendLine("</body>");
|
body.AppendLine("</html>");
|
#endregion
|
|
|
|
MailAddress fromAddress = new MailAddress(senderMail, senderName, Encoding.UTF8);
|
msg.From = fromAddress;
|
foreach (var a in toMails) {
|
MailAddress toAddress = new MailAddress(a);
|
msg.To.Add(toAddress);
|
}
|
|
mail.Send(msg);
|
mail.Dispose();
|
return true;
|
}
|
/// <summary>
|
/// 发送邮件(含附件)
|
/// </summary>
|
/// <param name="title">邮件主题</param>
|
/// <param name="htmlContent">邮件内容(html格式,例<p>hello</p>)</param>
|
/// <param name="toMails">收件人邮箱列表</param>
|
/// <param name="files">附件列表,Dictionary格式,key为文件唯一标识(可用于html多媒体内容(src='pid:key')),value为文件路径</param>
|
/// <param name="mailPriority">优先级,默认正常</param>
|
/// <returns></returns>
|
public bool Send(string title, string htmlContent, List<string> toMails,Dictionary<string,string> files, MailPriority mailPriority = MailPriority.Normal) {
|
if (!ParamCheck())
|
{
|
throw new Exception("未设置有效的连接参数");
|
}
|
|
if (toMails.Count == 0)
|
{
|
throw new Exception("接收者不能为空");
|
}
|
if (string.IsNullOrWhiteSpace(title))
|
{
|
title = "无主题";
|
}
|
|
SmtpClient mail = new SmtpClient(this.host);
|
mail.Credentials = new NetworkCredential(senderMail, senderKey);
|
MailMessage msg = new MailMessage();
|
|
msg.IsBodyHtml = true;
|
msg.Subject = title;
|
msg.SubjectEncoding = Encoding.UTF8;
|
msg.BodyEncoding = Encoding.UTF8;
|
msg.Priority = mailPriority;
|
|
#region 附件添加
|
foreach (var a in files) {
|
if (File.Exists(a.Value)){
|
var t = new Attachment(a.Value);
|
t.ContentId = a.Key;
|
msg.Attachments.Add(t);
|
}
|
}
|
#endregion
|
|
#region 邮件内容设置
|
StringBuilder body = new StringBuilder();
|
body.AppendLine("<html lang=\"zh-CN\">");
|
body.AppendLine("<head>");
|
body.AppendLine("<meta charset=\"utf-8\" />");
|
body.AppendLine("</head>");
|
body.AppendLine("<body>");
|
body.AppendLine(htmlContent);
|
body.AppendLine("</body>");
|
body.AppendLine("</html>");
|
#endregion
|
|
|
|
MailAddress fromAddress = new MailAddress(senderMail, senderName, Encoding.UTF8);
|
msg.From = fromAddress;
|
foreach (var a in toMails)
|
{
|
MailAddress toAddress = new MailAddress(a);
|
msg.To.Add(toAddress);
|
}
|
|
mail.Send(msg);
|
mail.Dispose();
|
return true;
|
}
|
/// <summary>
|
/// 发送需要抄送的邮件
|
/// </summary>
|
/// <param name="title">邮件主题</param>
|
/// <param name="htmlContent">邮件内容(html格式,例<p>hello</p>)</param>
|
/// <param name="toMails">收件人邮箱列表</param>
|
/// <param name="CC">抄送邮件列表</param>
|
/// <param name="IsBCC">抄送是否加密</param>
|
/// <param name="mailPriority">优先级,默认正常</param>
|
/// <returns></returns>
|
public bool Send(string title, string htmlContent, List<string> toMails, List<string> CC,bool IsBCC=false, MailPriority mailPriority = MailPriority.Normal) {
|
if (!ParamCheck())
|
{
|
throw new Exception("未设置有效的连接参数");
|
}
|
|
if (toMails.Count == 0)
|
{
|
throw new Exception("接收者不能为空");
|
}
|
if (string.IsNullOrWhiteSpace(title))
|
{
|
title = "无主题";
|
}
|
|
SmtpClient mail = new SmtpClient(this.host);
|
mail.Credentials = new NetworkCredential(senderMail, senderKey);
|
MailMessage msg = new MailMessage();
|
|
msg.IsBodyHtml = true;
|
msg.Subject = title;
|
msg.SubjectEncoding = Encoding.UTF8;
|
msg.BodyEncoding = Encoding.UTF8;
|
msg.Priority = mailPriority;
|
|
#region 邮件内容设置
|
StringBuilder body = new StringBuilder();
|
body.AppendLine("<html lang=\"zh-CN\">");
|
body.AppendLine("<head>");
|
body.AppendLine("<meta charset=\"utf-8\" />");
|
body.AppendLine("</head>");
|
body.AppendLine("<body>");
|
body.AppendLine(htmlContent);
|
body.AppendLine("</body>");
|
body.AppendLine("</html>");
|
#endregion
|
|
|
|
MailAddress fromAddress = new MailAddress(senderMail, senderName, Encoding.UTF8);
|
msg.From = fromAddress;
|
foreach (var a in toMails)
|
{
|
MailAddress toAddress = new MailAddress(a);
|
msg.To.Add(toAddress);
|
}
|
if (IsBCC) {
|
foreach (var a in CC) {
|
msg.Bcc.Add(a);
|
}
|
} else {
|
foreach (var a in CC) {
|
msg.CC.Add(a);
|
}
|
}
|
|
mail.Send(msg);
|
mail.Dispose();
|
return true;
|
}
|
/// <summary>
|
/// 发送需要抄送的邮件(含附件)
|
/// </summary>
|
/// <param name="title">邮件主题</param>
|
/// <param name="htmlContent">邮件内容(html格式,例<p>hello</p>)</param>
|
/// <param name="toMails">收件人邮箱列表</param>
|
/// <param name="files">附件列表,Dictionary格式,key为文件唯一标识(可用于html多媒体内容(src='pid:key')),value为文件路径</param>
|
/// <param name="CC">抄送邮件列表</param>
|
/// <param name="IsBCC">抄送是否加密</param>
|
/// <param name="mailPriority">优先级,默认正常</param>
|
/// <returns></returns>
|
public bool Send(string title, string htmlContent, List<string> toMails, Dictionary<string, string> files, MailAddressCollection CC,bool IsBCC = false, MailPriority mailPriority = MailPriority.Normal) {
|
if (!ParamCheck())
|
{
|
throw new Exception("未设置有效的连接参数");
|
}
|
|
if (toMails.Count == 0)
|
{
|
throw new Exception("接收者不能为空");
|
}
|
if (string.IsNullOrWhiteSpace(title))
|
{
|
title = "无主题";
|
}
|
|
SmtpClient mail = new SmtpClient(this.host);
|
mail.Credentials = new NetworkCredential(senderMail, senderKey);
|
MailMessage msg = new MailMessage();
|
|
msg.IsBodyHtml = true;
|
msg.Subject = title;
|
msg.SubjectEncoding = Encoding.UTF8;
|
msg.BodyEncoding = Encoding.UTF8;
|
msg.Priority = mailPriority;
|
|
#region 附件添加
|
foreach (var a in files)
|
{
|
if (File.Exists(a.Value))
|
{
|
var t = new Attachment(a.Value);
|
t.ContentId = a.Key;
|
msg.Attachments.Add(t);
|
}
|
}
|
#endregion
|
|
#region 邮件内容设置
|
StringBuilder body = new StringBuilder();
|
body.AppendLine("<html lang=\"zh-CN\">");
|
body.AppendLine("<head>");
|
body.AppendLine("<meta charset=\"utf-8\" />");
|
body.AppendLine("</head>");
|
body.AppendLine("<body>");
|
body.AppendLine(htmlContent);
|
body.AppendLine("</body>");
|
body.AppendLine("</html>");
|
#endregion
|
|
|
|
MailAddress fromAddress = new MailAddress(senderMail, senderName, Encoding.UTF8);
|
msg.From = fromAddress;
|
foreach (var a in toMails)
|
{
|
MailAddress toAddress = new MailAddress(a);
|
msg.To.Add(toAddress);
|
}
|
if (IsBCC)
|
{
|
foreach (var a in CC)
|
{
|
msg.Bcc.Add(a);
|
}
|
}
|
else
|
{
|
foreach (var a in CC)
|
{
|
msg.CC.Add(a);
|
}
|
}
|
mail.Send(msg);
|
mail.Dispose();
|
return true;
|
}
|
|
/// <summary>
|
/// 参数检验
|
/// </summary>
|
/// <returns></returns>
|
private bool ParamCheck() {
|
if (string.IsNullOrWhiteSpace(host)|| string.IsNullOrWhiteSpace(senderMail)
|
|| string.IsNullOrWhiteSpace(senderKey) || string.IsNullOrWhiteSpace(senderName)) {
|
return false;
|
}
|
return true;
|
}
|
}
|
}
|