分享一个异步发送邮件的类
首先要定义一个邮件信息的基类,如下所示:
/// <summary>
/// Base message class used for emails
/// </summary>
public class Message
{
#region Constructor
/// <summary>
/// Constructor
/// </summary>
public Message()
{
}
#endregion
#region Properties
/// <summary>
/// Whom the message is to
/// </summary>
public virtual string To { get; set; }
/// <summary>
/// The subject of the email
/// </summary>
public virtual string Subject { get; set; }
/// <summary>
/// Whom the message is from
/// </summary>
public virtual string From { get; set; }
/// <summary>
/// Body of the text
/// </summary>
public virtual string Body { get; set; }
#endregion
}
然后定义一个邮件的发送类,使用Netmail的方式发送邮件,发送邮件时采用了.net中自带的线程池,
通过多线程来实现异步的发送,代码如下:
/// <summary>
/// Utility for sending an email
/// </summary>
public class EmailSender : Message
{
#region Constructors
/// <summary>
/// Default Constructor
/// </summary>
public EmailSender()
{
Attachments = new List<Attachment>();
EmbeddedResources = new List<LinkedResource>();
Priority = MailPriority.Normal;
}
#endregion
#region Public Functions
/// <summary>
/// Sends an email
/// </summary>
/// <param name="Message">The body of the message</param>
public void SendMail(string Message)
{
Body = Message;
SendMail();
}
/// <summary>
/// Sends a piece of mail asynchronous
/// </summary>
/// <param name="Message">Message to be sent</param>
public void SendMailAsync(string Message)
{
Body = Message;
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}
/// <summary>
/// Sends an email
/// </summary>
public void SendMail()
{
using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage())
{
char[] Splitter = { ',', ';' };
string[] AddressCollection = To.Split(Splitter);
for (int x = 0; x < AddressCollection.Length; ++x)
{
if(!string.IsNullOrEmpty(AddressCollection[x].Trim()))
message.To.Add(AddressCollection[x]);
}
if (!string.IsNullOrEmpty(CC))
{
AddressCollection = CC.Split(Splitter);
for (int x = 0; x < AddressCollection.Length; ++x)
{
if (!string.IsNullOrEmpty(AddressCollection[x].Trim()))
message.CC.Add(AddressCollection[x]);
}
}
if (!string.IsNullOrEmpty(Bcc))
{
AddressCollection = Bcc.Split(Splitter);
for (int x = 0; x < AddressCollection.Length; ++x)
{
if (!string.IsNullOrEmpty(AddressCollection[x].Trim()))
message.Bcc.Add(AddressCollection[x]);
}
}
message.Subject = Subject;
message.From = new System.Net.Mail.MailAddress((From));
AlternateView BodyView = AlternateView.CreateAlternateViewFromString(Body, null, MediaTypeNames.Text.Html);
foreach (LinkedResource Resource in EmbeddedResources)
{
BodyView.LinkedResources.Add(Resource);
}
message.AlternateViews.Add(BodyView);
//message.Body = Body;
message.Priority = Priority;
message.SubjectEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
message.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
message.IsBodyHtml = true;
foreach (Attachment TempAttachment in Attachments)
{
message.Attachments.Add(TempAttachment);
}
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(Server, Port);
if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
{
smtp.Credentials = new System.Net.NetworkCredential(UserName, Password);
}
if (UseSSL)
smtp.EnableSsl = true;
else
smtp.EnableSsl = false;
smtp.Send(message);
}
}
/// <summary>
/// Sends a piece of mail asynchronous
/// </summary>
public void SendMailAsync()
{
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}
#endregion
#region Properties
/// <summary>
/// Any attachments that are included with this
/// message.
/// </summary>
public List<Attachment> Attachments { get; set; }
/// <summary>
/// Any attachment (usually images) that need to be embedded in the message
/// </summary>
public List<LinkedResource> EmbeddedResources { get; set; }
/// <summary>
/// The priority of this message
/// </summary>
public MailPriority Priority { get; set; }
/// <summary>
/// Server Location
/// </summary>
public string Server { get; set; }
/// <summary>
/// User Name for the server
/// </summary>
public string UserName { get; set; }
/// <summary>
/// Password for the server
/// </summary>
public string Password { get; set; }
/// <summary>
/// Port to send the information on
/// </summary>
public int Port { get; set; }
/// <summary>
/// Decides whether we are using STARTTLS (SSL) or not
/// </summary>
public bool UseSSL { get; set; }
/// <summary>
/// Carbon copy send (seperate email addresses with a comma)
/// </summary>
public string CC { get; set; }
/// <summary>
/// Blind carbon copy send (seperate email addresses with a comma)
/// </summary>
public string Bcc { get; set; }
#endregion
}
转载于:https://www.cnblogs.com/kevinGao/archive/2012/02/01/2336478.html
分享一个异步发送邮件的类相关推荐
- 分享一个http请求工具类,超好用。
文章目录 一,代码片 二,maven依赖 三,最后 一,代码片 分享一个http请求的工具类,超好用,废话不多说,直接上代码: import com.alibaba.fastjson.JSON; im ...
- 分享一个自定义的 console 类,让你不再纠结JS中的调试代码的兼容
问题的产生 在写JS的过程中,为了调试我们常常会写很多 console.log.console.info.console.group.console.warn.console.error代码来查看JS ...
- 我也分享一个c# ini操作类
刚刚看了一篇 @云菲菲 的关于基于正则的INI辅助类文章:http://www.cnblogs.com/yunfeifei/p/4081977.html,作者写的不错.还看到评论处有一个的地址:htt ...
- 分享一个WininetAPI的helper类
using System; using System.Collections; using System.Drawing; using System.IO; using System.Net; usi ...
- 个人计算机技术分享,一个计算机类本科毕业设计分享
一个计算机类本科毕业设计分享 2021-01-12 东哥毕设 196 0 一个计算机类本科毕业设计分享 一个计算机类本科毕业设计分享,是关于计算机网络技术校园网络公选课的设计和实现,本章主要对该计算机 ...
- 分享一个nodejs中koa操作redis的工具类 基于 ioredis
分享一个node 操作redis的工具类 基于ioredis redis.js const config = require(':config/server.base.config'); const ...
- c dbhelper类下载mysql_分享一个简单的C#的通用DbHelper类(支持数据连接池)
每次新项目的时候,都要从头去找一遍数据库工具类.这里分享一个简单实用的C#的通用DbHelper工具类,支持数据连接池. 连接池配置 DbHelper类 public classDBHelper { ...
- 分享一个自己写的取中国农历相关数据的类。包含:农历年月日,生肖,星座,年龄,天干,地支等方法...
分享一个自己写的取中国农历相关数据的类.包含:农历年月日,生肖,星座,年龄,天干,地支等方法.此类自己花了一上午的时间写的,适用于像相亲网等类似的网站 主要使用了微软针对东亚地区的农历类Chinese ...
- php读ini的类,分享一个完善的读写ini格式的PHP配置类
分享一个完善的读写ini格式的PHP配置类 分类:PHP_Python| 发布:佚名| 查看: | 发表时间:2015/8/10 基本满足所有配置相关的需求./** * 解析.ini格式的配置文件为一 ...
最新文章
- yolov3 get_next_batch 异常
- 【OpenSSL】OpenSSL之MD5
- sublime3中文乱码解决包ConvertToUTF8.zip
- 软考中级数据库系统工程师备考经验分享
- MySQL分页查询效率
- Excel表Ctrl+v和Ctrl shift+v有什么区别_朴素的办公神器——excel
- div实现antd Descriptions描述列表(可灵活修改、固定宽度)
- C#打包文件夹成zip格式(包括文件夹和子文件夹下的所有文件)
- M1卡破解(自从学校升级系统之后,还准备在研究下)
- 医保是不是只有住院才能在单位报销,什么样的病才能报销
- Altium AD20整板放置GND过孔、批量放置GND过孔/缝合孔
- 计算机老师中专教学论文,中专计算机多元化教学论文
- Generating Summaries with Topic Templates and Structured Convolutional Decoders笔记
- Python 多线程学习
- 局域网三大攻击工具的攻击原理及其防范
- JECloud快速入门手册_【数据字典】
- h5上下滑动动画效果(vue)
- 《uni-app》一个非canvas的飞机对战小游戏-启动页
- Oracle数据库之Collection干货
- 引领材料新变革 人工合成云母的进阶之路
热门文章
- Spark 把RDD数据保存到hdfs单个文件中,而不是目录
- wireshark从入门到精通(协议排错安全篇)7
- Hadoop Pipes编程之C++实现WordCount
- iphone开发 ---- GPS
- WPF:下拉列表的简单实现
- opencms常用标签
- 使用 ale.js 制作一个小而美的表格编辑器(3)
- 常用的Git Tips
- css 兼容ie6,ie7,ff的fixed,元素上下端固定定位方法
- 为什么世界上一些最好的科学家和程序员,在世人眼里,都有点不太正常,甚至行为混乱...