异步邮件发送
using System;
using System.Net.Mail;
using System.Collections.Specialized;
namespace MS.Core.Common
{
public sealed class SMTPEmailer
{
#region 私有常量
private static string _fromAddress = "Jay.Sky001@163.com";
private static string _fromAlias = "Mr Vis";
private static string _toAlias = "Dear Friends";
private static string _toAddress = "MyMuse@foxmail.com";
private static string _ccAddress = "";//抄送
private static string _bccAddress = "";//密送
private static string _smtpServer = "smtp.163.com";
private static bool _isEmailEnabled = true;
#endregion
#region 私有变量
private static MailMessage message;
#endregion
#region 公有方法
static SMTPEmailer()
{
}
/// <summary>
/// 邮件发送重载方法
/// </summary>
/// <param name="subject"></param>
/// <param name="body"></param>
public static void SendEmail(string subject, string body)
{
if (_isEmailEnabled)
{
CreateMessage(subject, body);
SendMail();
}
}
/// <summary>
/// 邮件发送重载方法
/// </summary>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <param name="attachment"></param>
public static void SendEmail(string subject, string body, string attachment)
{
if (_isEmailEnabled)
{
CreateMessage(subject, body);
if (!string.IsNullOrEmpty(attachment))
{
if (System.IO.File.Exists(attachment))
message.Attachments.Add(new Attachment(attachment));
else
throw new Exception(string.Format("Attachment {0} not found", attachment));
}
SendMail();
}
}
/// <summary>
/// 邮件发送重载方法
/// </summary>
/// <param name="fromAddress"></param>
/// <param name="fromAlias"></param>
/// <param name="toAddress"></param>
/// <param name="ccAddress"></param>
/// <param name="bccAddress"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <param name="attachment"></param>
public static void SendEmail(string fromAddress, string fromAlias, string toAddress,
string ccAddress, string bccAddress, string subject, string body, string attachment)
{
if (_isEmailEnabled)
{
_fromAddress = fromAddress;
_fromAlias = fromAlias;
_toAddress = toAddress;
_ccAddress = ccAddress;
_bccAddress = bccAddress;
CreateMessage(subject, body);
if (!string.IsNullOrEmpty(attachment))
{
if (System.IO.File.Exists(attachment))
message.Attachments.Add(new Attachment(attachment));
else
throw new Exception(string.Format("Attachment {0} not found", attachment));
}
SendMail();
}
}
#endregion
#region 私有方法
/// <summary>
/// 创建邮件信息
/// </summary>
/// <param name="subject"></param>
/// <param name="body"></param>
private static void CreateMessage(string subject, string body)
{
if (message == null)
{
message = new MailMessage();
message.From = new MailAddress(_fromAddress, _fromAlias);
message.Subject = subject;
message.Body = body;
//message.To.Add(_toAddress);
message.To.Add(new MailAddress(_toAddress, _toAlias));
if (!string.IsNullOrEmpty(_ccAddress))
message.CC.Add(_ccAddress);
if (!string.IsNullOrEmpty(_bccAddress))
message.Bcc.Add(_bccAddress);
}
}
/// <summary>
/// 发送邮件
/// </summary>
private static void SendMail()
{
SmtpClient client = new SmtpClient(_smtpServer);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new System.Net.NetworkCredential("邮箱账号", "邮箱密码");
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
client.SendAsync(message, "发送中..");
message = null;
}
/// <summary>
/// 邮件发送之后的异步操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
// 获取此异步操作的独特标识符
String token = (string)e.UserState;
if (e.Error != null)
{
throw e.Error;
}
else
{
//成功后操作
}
}
#endregion
}
}
using System.Net.Mail;
using System.Collections.Specialized;
namespace MS.Core.Common
{
public sealed class SMTPEmailer
{
#region 私有常量
private static string _fromAddress = "Jay.Sky001@163.com";
private static string _fromAlias = "Mr Vis";
private static string _toAlias = "Dear Friends";
private static string _toAddress = "MyMuse@foxmail.com";
private static string _ccAddress = "";//抄送
private static string _bccAddress = "";//密送
private static string _smtpServer = "smtp.163.com";
private static bool _isEmailEnabled = true;
#endregion
#region 私有变量
private static MailMessage message;
#endregion
#region 公有方法
static SMTPEmailer()
{
}
/// <summary>
/// 邮件发送重载方法
/// </summary>
/// <param name="subject"></param>
/// <param name="body"></param>
public static void SendEmail(string subject, string body)
{
if (_isEmailEnabled)
{
CreateMessage(subject, body);
SendMail();
}
}
/// <summary>
/// 邮件发送重载方法
/// </summary>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <param name="attachment"></param>
public static void SendEmail(string subject, string body, string attachment)
{
if (_isEmailEnabled)
{
CreateMessage(subject, body);
if (!string.IsNullOrEmpty(attachment))
{
if (System.IO.File.Exists(attachment))
message.Attachments.Add(new Attachment(attachment));
else
throw new Exception(string.Format("Attachment {0} not found", attachment));
}
SendMail();
}
}
/// <summary>
/// 邮件发送重载方法
/// </summary>
/// <param name="fromAddress"></param>
/// <param name="fromAlias"></param>
/// <param name="toAddress"></param>
/// <param name="ccAddress"></param>
/// <param name="bccAddress"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <param name="attachment"></param>
public static void SendEmail(string fromAddress, string fromAlias, string toAddress,
string ccAddress, string bccAddress, string subject, string body, string attachment)
{
if (_isEmailEnabled)
{
_fromAddress = fromAddress;
_fromAlias = fromAlias;
_toAddress = toAddress;
_ccAddress = ccAddress;
_bccAddress = bccAddress;
CreateMessage(subject, body);
if (!string.IsNullOrEmpty(attachment))
{
if (System.IO.File.Exists(attachment))
message.Attachments.Add(new Attachment(attachment));
else
throw new Exception(string.Format("Attachment {0} not found", attachment));
}
SendMail();
}
}
#endregion
#region 私有方法
/// <summary>
/// 创建邮件信息
/// </summary>
/// <param name="subject"></param>
/// <param name="body"></param>
private static void CreateMessage(string subject, string body)
{
if (message == null)
{
message = new MailMessage();
message.From = new MailAddress(_fromAddress, _fromAlias);
message.Subject = subject;
message.Body = body;
//message.To.Add(_toAddress);
message.To.Add(new MailAddress(_toAddress, _toAlias));
if (!string.IsNullOrEmpty(_ccAddress))
message.CC.Add(_ccAddress);
if (!string.IsNullOrEmpty(_bccAddress))
message.Bcc.Add(_bccAddress);
}
}
/// <summary>
/// 发送邮件
/// </summary>
private static void SendMail()
{
SmtpClient client = new SmtpClient(_smtpServer);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new System.Net.NetworkCredential("邮箱账号", "邮箱密码");
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
client.SendAsync(message, "发送中..");
message = null;
}
/// <summary>
/// 邮件发送之后的异步操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
// 获取此异步操作的独特标识符
String token = (string)e.UserState;
if (e.Error != null)
{
throw e.Error;
}
else
{
//成功后操作
}
}
#endregion
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!