项目中使用的邮件发送类,自己封装的,多线程、自带队列、异步发送,回调方法可捕获发送中产生的错误消息,留存参考:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;
namespace Common
{
public class MyEmail
{
#region 邮件静态属性
private static string SmtpServer = "smtp.qq.com"; //邮件服务器
private static string DefaultFrom = "xxxxxxx@qq.com"; //默认发件箱
private static string Name = "Test"; //默认发件箱名字
private static string Password = "yyyyyy"; //默认发件箱密码
private static int SmtpServerPort = 587; //邮件服务器端口
private static int Timeout = 60000; //超时时间
private static bool IsRun = false; //发邮件线程是否启动
private static List<MyEmail> Mails = new List<MyEmail>(); //待发邮件列表
#endregion
#region 邮件属性
public string Subject { get; set; }
public string Body { get; set; }
public string From { get; set; }
public int Priority { get; set; }
public bool IsBodyHtml { get; set; }
public int Status { get; set; } //0:未发送,1:发送中,2:已发送,3:发送失败,4:发送取消
public int SendCnt { get; set; }
public List<string> Attachments { get; set; } //附件
public List<string> Tos { get; set; } //接收人
public List<string> CCs { get; set; } //抄送人
#endregion
#region 方法
public MyEmail()
{
Status = 0;
SendCnt = 0;
Priority = 0;
IsBodyHtml = true;
}
public static void Send(MyEmail mail)
{
Mails.Add(mail);
if (!IsRun)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(StartSend));
IsRun = true;
}
}
public static string SendMail(MyEmail email)
{
var errMsg = "";
try
{
var client = new SmtpClient(SmtpServer, SmtpServerPort);
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential(DefaultFrom, Password);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Timeout = Timeout;
var mailMessage = new MailMessage();
mailMessage.From = new MailAddress(email.From, Name, Encoding.UTF8);
mailMessage.Subject = email.Subject;
mailMessage.SubjectEncoding = Encoding.UTF8;
mailMessage.Body = email.Body;
mailMessage.BodyEncoding = Encoding.UTF8;
mailMessage.IsBodyHtml = email.IsBodyHtml;
mailMessage.Priority = MailPriority.High;
foreach (var to in email.Tos)
{
mailMessage.To.Add(to);
}
foreach (var cc in email.CCs)
{
mailMessage.CC.Add(cc);
}
foreach (var file in email.Attachments)
{
mailMessage.Attachments.Add(new Attachment(file));
}
object userState = mailMessage;
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
client.SendAsync(mailMessage, email);
}
catch (Exception ex)
{
return ex.Message;
}
return errMsg;
}
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
var mail = e.UserState as MyEmail;
if (e.Cancelled)
{
Log.WriteLog(DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss") + ", 邮件【" + mail.Subject + "】发送取消!");
mail.Status = 4;
}
else if (e.Error != null)
{ //这里根据e.UserState的值可以判断多种返回状态
Log.WriteLog(DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss") + ", 邮件【" + mail.Subject + "】发送失败!\r\n" + e.Error.Message);
mail.Status = 3;
}
else
{
Mails.Remove(mail);
}
}
private static void StartSend(object obj)
{
while (true)
{
var mail = Mails.Where(p => p.Status == 0 && p.SendCnt < 3).OrderByDescending(p => p.Priority).FirstOrDefault();
if (mail != null)
{
if (mail.Tos.Count > 0)
{
SendMail(mail);
mail.SendCnt += 1;
mail.Status = 1;
}
else
{
mail.Status = 3;
}
}
Thread.Sleep(500);
}
}
#endregion
}
}