欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#異步發(fā)送郵件的類

 更新時間:2014年01月20日 17:01:24   作者:  
這篇文章主要介紹了使用c#異步發(fā)送郵件的類,大家參考使用吧

首先要定義一個郵件信息的基類,如下所示:

復制代碼 代碼如下:

/// <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
}

然后定義一個郵件的發(fā)送類,使用Netmail的方式發(fā)送郵件,發(fā)送郵件時采用了.net中自帶的線程池,
通過多線程來實現(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)文章

  • 自動輸出類的字段值實用代碼分享

    自動輸出類的字段值實用代碼分享

    有點時候在測試的時候希望打印輸出返回對象的各字段的值,采用下面的代碼可以很方便的列出對象的各字段值
    2013-12-12
  • C#?SQLite庫使用技巧

    C#?SQLite庫使用技巧

    SQLite是一個開源、免費的小型RDBMS(關(guān)系型數(shù)據(jù)庫),能獨立運行、無服務器、零配置、支持事物,用C實現(xiàn),內(nèi)存占用較小,支持絕大數(shù)的SQL92標準。下面跟隨小編一起看下C#?SQLite庫使用
    2022-01-01
  • C#文件上傳的簡單實現(xiàn)

    C#文件上傳的簡單實現(xiàn)

    這篇文章主要為大家詳細介紹了C#文件上傳的簡單實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 在 C# 中使用 插值字符串

    在 C# 中使用 插值字符串

    這篇文章主要介紹了在 C# 中使用 插值字符串,字符串插值是一種將 表達式 插入到字符串字面量中的一種技術(shù),又稱為變量替換,變量插值,變量展開 等等,它是一種用相應值替換字符串中的一個或者更多個占位符的處理過程
    2022-01-01
  • C#十六進制字符串轉(zhuǎn)十進制int的方法

    C#十六進制字符串轉(zhuǎn)十進制int的方法

    這篇文章主要介紹了C#十六進制字符串轉(zhuǎn)十進制int的方法,涉及C#操作數(shù)制轉(zhuǎn)換的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • C#編程報錯System.InvalidOperationException問題及解決

    C#編程報錯System.InvalidOperationException問題及解決

    這篇文章主要介紹了C#編程報錯System.InvalidOperationException問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • C#+MO實現(xiàn)一個道路編輯軟件(剛開始)

    C#+MO實現(xiàn)一個道路編輯軟件(剛開始)

    C#+MO實現(xiàn)一個道路編輯軟件(剛開始)...
    2007-04-04
  • WPF實現(xiàn)能自由改變形狀的四邊形和六邊形

    WPF實現(xiàn)能自由改變形狀的四邊形和六邊形

    這篇文章主要為大家詳細介紹了WPF如何實現(xiàn)能自由改變形狀的四邊形和六邊形,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-03-03
  • C# ExecuteScalar()方法案例講解

    C# ExecuteScalar()方法案例講解

    這篇文章主要介紹了C# ExecuteScalar()方法案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • .net實現(xiàn)裁剪網(wǎng)站上傳圖片的方法

    .net實現(xiàn)裁剪網(wǎng)站上傳圖片的方法

    這篇文章主要介紹了.net實現(xiàn)裁剪網(wǎng)站上傳圖片的方法,比較實用的功能,需要的朋友可以參考下
    2014-07-07

最新評論