当前位置: 编程技术>.net/c#/asp.net
c#异步发送邮件的类实例代码
来源: 互联网 发布时间:2014-08-30
本文导语: 一个使用c#异步发送邮件的类。 首先,定义一个邮件信息的基类。 代码示例: /// /// Base message class used for emails /// public class Message { #region Constructor /// /// Constructor /// public Message() { } #endregion #region Properties /// ...
一个使用c#异步发送邮件的类。
首先,定义一个邮件信息的基类。
代码示例:
///
/// Base message class used for emails
///
public class Message
{
#region Constructor
///
/// Constructor
///
public Message()
{
}
#endregion
#region Properties
///
/// Whom the message is to
///
public virtual string To { get; set; }
///
/// The subject of the email
///
public virtual string Subject { get; set; }
///
/// Whom the message is from
///
public virtual string From { get; set; }
///
/// Body of the text
///
public virtual string Body { get; set; }
#endregion
}
/// Base message class used for emails
///
public class Message
{
#region Constructor
///
/// Constructor
///
public Message()
{
}
#endregion
#region Properties
///
/// Whom the message is to
///
public virtual string To { get; set; }
///
/// The subject of the email
///
public virtual string Subject { get; set; }
///
/// Whom the message is from
///
public virtual string From { get; set; }
///
/// Body of the text
///
public virtual string Body { get; set; }
#endregion
}
然后,定义一个邮件的发送类,使用Netmail的方式发送邮件,发送邮件时采用了.net中自带的线程池,通过多线程来实现异步的发送。
代码示例:
///
/// Utility for sending an email
///
public class EmailSender : Message
{
#region Constructors
///
/// Default Constructor
///
public EmailSender()
{
Attachments = new List();
EmbeddedResources = new List();
Priority = MailPriority.Normal;
}
#endregion
#region Public Functions
///
/// Sends an email
///
/// The body of the message
public void SendMail(string Message)
{
Body = Message;
SendMail();
}
///
/// Sends a piece of mail asynchronous
///
/// Message to be sent
public void SendMailAsync(string Message)
{
Body = Message;
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}
///
/// Sends an email
///
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);
}
}
///
/// Sends a piece of mail asynchronous
///
public void SendMailAsync()
{
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}
#endregion
#region Properties
///
/// Any attachments that are included with this
/// message.
///
public List Attachments { get; set; }
///
/// Any attachment (usually images) that need to be embedded in the message
///
public List EmbeddedResources { get; set; }
///
/// The priority of this message
///
public MailPriority Priority { get; set; }
///
/// Server Location
///
public string Server { get; set; }
///
/// User Name for the server
///
public string UserName { get; set; }
///
/// Password for the server
///
public string Password { get; set; }
///
/// Port to send the information on
///
public int Port { get; set; }
///
/// Decides whether we are using STARTTLS (SSL) or not
///
public bool UseSSL { get; set; }
///
/// Carbon copy send (seperate email addresses with a comma)
///
public string CC { get; set; }
///
/// Blind carbon copy send (seperate email addresses with a comma)
///
public string Bcc { get; set; }
#endregion
}
/// Utility for sending an email
///
public class EmailSender : Message
{
#region Constructors
///
/// Default Constructor
///
public EmailSender()
{
Attachments = new List();
EmbeddedResources = new List();
Priority = MailPriority.Normal;
}
#endregion
#region Public Functions
///
/// Sends an email
///
/// The body of the message
public void SendMail(string Message)
{
Body = Message;
SendMail();
}
///
/// Sends a piece of mail asynchronous
///
/// Message to be sent
public void SendMailAsync(string Message)
{
Body = Message;
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}
///
/// Sends an email
///
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);
}
}
///
/// Sends a piece of mail asynchronous
///
public void SendMailAsync()
{
ThreadPool.QueueUserWorkItem(delegate { SendMail(); });
}
#endregion
#region Properties
///
/// Any attachments that are included with this
/// message.
///
public List Attachments { get; set; }
///
/// Any attachment (usually images) that need to be embedded in the message
///
public List EmbeddedResources { get; set; }
///
/// The priority of this message
///
public MailPriority Priority { get; set; }
///
/// Server Location
///
public string Server { get; set; }
///
/// User Name for the server
///
public string UserName { get; set; }
///
/// Password for the server
///
public string Password { get; set; }
///
/// Port to send the information on
///
public int Port { get; set; }
///
/// Decides whether we are using STARTTLS (SSL) or not
///
public bool UseSSL { get; set; }
///
/// Carbon copy send (seperate email addresses with a comma)
///
public string CC { get; set; }
///
/// Blind carbon copy send (seperate email addresses with a comma)
///
public string Bcc { get; set; }
#endregion
}