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)文章
.NET使用IResourceMonitor實現(xiàn)獲取資源信息
在 Microsoft.Extensions.Diagnostics.ResourceMonitoring 包提供了一系列定制 API,專用于監(jiān)視 .NET 應(yīng)用程序的資源利用率,本文將利用IResourceMonitor來實現(xiàn)獲取資源狀態(tài)信息,感興趣的可以了解下2024-01-01
C#多線程學(xué)習(xí)之(一)多線程的相關(guān)概念分析
這篇文章主要介紹了C#多線程學(xué)習(xí)之多線程的相關(guān)概念,涉及C#中多線程的相關(guān)概念與使用技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
C#用websocket實現(xiàn)簡易聊天功能(客戶端)
這篇文章主要為大家詳細介紹了C#用websocket實現(xiàn)簡易聊天功能,客戶端方向,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
Linq利用Distinct去除重復(fù)項問題(可自己指定)
這篇文章主要介紹了Linq利用Distinct去除重復(fù)項問題(可自己指定),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01

