C#實現(xiàn)異步發(fā)送郵件的方法
更新時間:2015年04月04日 13:15:41 作者:令狐不聰
這篇文章主要介紹了C#實現(xiàn)異步發(fā)送郵件的方法,涉及C#異步操作與郵件發(fā)送的技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了C#實現(xiàn)異步發(fā)送郵件的方法。分享給大家供大家參考。具體如下:
下面的代碼可以實現(xiàn)異步發(fā)送郵件,等郵件發(fā)送出去后會自動調(diào)用回調(diào)函數(shù),這樣在發(fā)送郵件時就不會卡住程序不動了
MailMessage m = new MailMessage
("item@jb51.net",
"raja@jb51.net",
"This is the subject for the authorized email.",
"This is the body of the authorized mail!...");
// Send the message using authorization
SmtpClient client = new SmtpClient("smtp.jb51.net");
client.Credentials = new NetworkCredential("user", "password");
client.EnableSsl = true;
// Add the event handler
client.SendCompleted += new SendCompletedEventHandler(mail_SendCompleted);
// Send the message asynchronously
client.SendAsync(m, null);
// To Cancel the send
//client.SendAsyncCancel();
void mail_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
Console.WriteLine("Message cancelled");
else if (e.Error != null)
Console.WriteLine("Error: " + e.Error.ToString());
else
Console.WriteLine("Message sent");
}
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
windows下C#定時管理器框架Task.MainForm詳解
這篇文章主要為大家詳細介紹了windows下C#定時管理器框架Task.MainForm的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
C#使用泛型隊列Queue實現(xiàn)生產(chǎn)消費模式
這篇文章介紹了C#使用泛型隊列Queue實現(xiàn)生產(chǎn)消費模式的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
C#實現(xiàn)獲取系統(tǒng)目錄并以Tree樹叉顯示的方法
這篇文章主要介紹了C#實現(xiàn)獲取系統(tǒng)目錄并以Tree樹叉顯示的方法,可以加深讀者對于C#下數(shù)據(jù)結(jié)構(gòu)實現(xiàn)方法的認識,需要的朋友可以參考下2014-07-07
C# 通過同步和異步實現(xiàn)優(yōu)化做早餐的時間
本文以一個簡單的小例子—如何做一頓早餐及如何優(yōu)化做早餐的時間來讓大家具體了解一下同步和異步方法的區(qū)別,需要的朋友可以參考一下2021-12-12
VS2015為console.readkey添加代碼片段的方法
這篇文章主要介紹了VS2015為console.readkey添加代碼片段的方法,需要的朋友可以參考下2016-12-12

