分享一个异步发送邮件的类
来源:互联网 发布:linux多进程服务器 编辑:程序博客网 时间:2024/05/17 10:24
首先要定义一个邮件信息的基类,如下所示:
/// <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
}
- 分享一个异步发送邮件的类
- 分享一个thinkphp邮件发送类
- 邮件的异步发送
- 【demo】自己写的一个 SpringMail 异步发送邮件的demo。 javamail 邮件异步发送
- 自己封装的一个支持回调和独立线程的异步发送邮件的类
- 利用Spring框架封装的JavaMail实现同步或异步邮件发送 分享
- 改进了的一个邮件发送类
- 改进了的一个邮件发送类
- PHP一个发送邮件的类
- PHP一个发送邮件的类
- php发送邮件的一个类
- php发送邮件的一个类
- 自己写的一个发送邮件类
- C#,一个邮件发送的类
- [PHP]PHP一个发送邮件的类
- PHP一个发送邮件的类
- PHP邮件发送的一个类
- 一个C++ 发送邮件的类
- 含虚函数的struct在其构造中要谨慎使用memset函数对数据成员清零
- base-kernel-file-layout
- sqlserver2008附加数据库是“只读”的解决办法
- Linux进程的实际用户ID和有效用户ID
- windows 2003 server IIS权限设置
- 分享一个异步发送邮件的类
- Shell scripting 1 - introduction
- 获得文件头编辑框中多行文本
- 在iOS应用中使用GPS
- 黑马程序员-委托
- VC:利用 CPtrArray 类保存图像
- 模拟设计工具大比拼
- js随笔一
- mongodb 更新操作