手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#实现异步发送邮件的方法
C#实现异步发送邮件的方法
摘要:本文实例讲述了C#实现异步发送邮件的方法。分享给大家供大家参考。具体如下:下面的代码可以实现异步发送邮件,等邮件发送出去后会自动调用回调函数...

本文实例讲述了C#实现异步发送邮件的方法。分享给大家供大家参考。具体如下:

下面的代码可以实现异步发送邮件,等邮件发送出去后会自动调用回调函数,这样在发送邮件时就不会卡住程序不动了

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#程序设计有所帮助。

【C#实现异步发送邮件的方法】相关文章:

c#实现输出本月的月历

C# 屏蔽关键字的实现方法

C#操作config文件的具体方法

C#图片压缩的实现方法

C#编程实现Excel文档中搜索文本内容的方法及思路

C#删除文件目录或文件的解决方法

C#访问应用程序配置文件的方法

c# 重载WndProc,实现重写“最小化”的实现方法

c#重写TabControl控件实现关闭按钮的方法

C#实现的最短路径分析

精品推荐
分类导航