手机
当前位置:查字典教程网 >编程开发 >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#操作config文件的具体方法

C#异步调用的好处和方法分享

C# 向二进制文件进行读写的操作方法

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

C# 获取属性名的方法

c#实现无标题栏窗口的拖动

C#图片压缩的实现方法

C#实现的最短路径分析

C#读写文件的方法汇总

精品推荐
分类导航