c#異步發(fā)送郵件的類
首先要定義一個(gè)郵件信息的基類,如下所示:
/// <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
}
然后定義一個(gè)郵件的發(fā)送類,使用Netmail的方式發(fā)送郵件,發(fā)送郵件時(shí)采用了.net中自帶的線程池,
通過多線程來實(shí)現(xiàn)異步的發(fā)送,代碼如下:
/// <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
}
相關(guān)文章
關(guān)于C#中async/await的用法實(shí)例詳解
這篇文章主要介紹了關(guān)于C#中async/await的用法,今天寫一個(gè)demo徹底搞明白async/await的用法,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02
C#實(shí)現(xiàn)利用Windows API讀寫INI文件的方法
這篇文章主要介紹了C#實(shí)現(xiàn)利用Windows API讀寫INI文件的方法,涉及C#針對(duì)ini文件的創(chuàng)建、讀取及寫入等操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
C++聯(lián)合體轉(zhuǎn)換成C#結(jié)構(gòu)的實(shí)現(xiàn)方法
這篇文章主要介紹了C++聯(lián)合體轉(zhuǎn)換成C#結(jié)構(gòu)的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-08-08
c#的時(shí)間日期操作示例分享(c#獲取當(dāng)前日期)
這篇文章主要介紹了c#的時(shí)間日期操作示例,在給定時(shí)間戳返回指定的時(shí)間格式和獲取當(dāng)前時(shí)間方法,需要的朋友可以參考下2014-03-03
C# 7.0之ref locals and returns(局部變量和引用返回)
這篇文章主要介紹了C# 7.0之ref locals and returns,即局部變量和引用返回,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
3種方法實(shí)現(xiàn)WindowsForm切換窗口
這篇文章主要介紹了3種方法實(shí)現(xiàn)WindowsForm切換窗口,文中講解非常詳細(xì),示例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
C#將圖片和字節(jié)流互相轉(zhuǎn)換并顯示到頁面上
本文主要介紹用C#實(shí)現(xiàn)圖片轉(zhuǎn)換成字節(jié)流,字節(jié)流轉(zhuǎn)換成圖片,并根據(jù)圖片路徑返回圖片的字節(jié)流,有需要的朋友可以參考下2015-08-08

