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
{
///
/// 邮件发送助手
///
public class SmtpMailHelper
{
///
/// 发件服务器
///
public string host { get; set; }
///
/// 发件邮箱
///
public string senderMail { get; set; }
///
/// 发件邮箱的授权key
///
public string senderKey { get; set; }
///
/// 发件邮箱名
///
public string senderName { get; set; }
///
/// 使用默认参数实例化
///
public SmtpMailHelper() {
host = "smtp.163.com";
senderMail = "linmolilin@163.com";
senderKey = "QWXSUGVVKNHCZVQJ";
senderName = "Moli";
}
///
/// 指定发件服务器实例化
///
///
public SmtpMailHelper(string host) {
this.host = host;
senderMail = "linmolilin@163.com";
senderKey = "QWXSUGVVKNHCZVQJ";
senderName = "Moli";
}
///
/// 指定连接参数
///
public SmtpMailHelper(string host,string senderMail,string senderKey) {
this.host = host;
this.senderMail = senderMail;
this.senderKey = senderKey;
senderName = "Moli";
}
///
/// 自定所有连接参数
///
/// 发件服务器主机
/// 发件邮箱
/// 发件邮箱的授权key
/// 发件邮箱名
public SmtpMailHelper(string host, string senderMail, string senderKey,string senderName)
{
this.host = host;
this.senderMail = senderMail;
this.senderKey = senderKey;
this.senderName = senderName;
}
///
/// 发送邮件
///
/// 邮件主题
/// 邮件内容(html格式,例
hello
)
/// 收件人邮箱列表
/// 优先级,默认正常
/// 发送成功返回true
public bool Send(string title,string htmlContent,List 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("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine(htmlContent);
body.AppendLine("");
body.AppendLine("");
#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;
}
///
/// 发送邮件(含附件)
///
/// 邮件主题
/// 邮件内容(html格式,例hello
)
/// 收件人邮箱列表
/// 附件列表,Dictionary格式,key为文件唯一标识(可用于html多媒体内容(src='pid:key')),value为文件路径
/// 优先级,默认正常
///
public bool Send(string title, string htmlContent, List toMails,Dictionary 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("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine(htmlContent);
body.AppendLine("");
body.AppendLine("");
#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;
}
///
/// 发送需要抄送的邮件
///
/// 邮件主题
/// 邮件内容(html格式,例hello
)
/// 收件人邮箱列表
/// 抄送邮件列表
/// 抄送是否加密
/// 优先级,默认正常
///
public bool Send(string title, string htmlContent, List toMails, List 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("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine(htmlContent);
body.AppendLine("");
body.AppendLine("");
#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;
}
///
/// 发送需要抄送的邮件(含附件)
///
/// 邮件主题
/// 邮件内容(html格式,例hello
)
/// 收件人邮箱列表
/// 附件列表,Dictionary格式,key为文件唯一标识(可用于html多媒体内容(src='pid:key')),value为文件路径
/// 抄送邮件列表
/// 抄送是否加密
/// 优先级,默认正常
///
public bool Send(string title, string htmlContent, List toMails, Dictionary 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("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine(htmlContent);
body.AppendLine("");
body.AppendLine("");
#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;
}
///
/// 参数检验
///
///
private bool ParamCheck() {
if (string.IsNullOrWhiteSpace(host)|| string.IsNullOrWhiteSpace(senderMail)
|| string.IsNullOrWhiteSpace(senderKey) || string.IsNullOrWhiteSpace(senderName)) {
return false;
}
return true;
}
}
}