本文實(shí)例講述了C#實(shí)現(xiàn)異步發(fā)送郵件的方法。分享給大家供大家參考。具體如下:
下面的代碼可以實(shí)現(xiàn)異步發(fā)送郵件,等郵件發(fā)送出去后會(huì)自動(dòng)調(diào)用回調(diào)函數(shù),這樣在發(fā)送郵件時(shí)就不會(huì)卡住程序不動(dòng)了
MailMessage m = new MailMessage ("item@VeVB.COm", "raja@VeVB.COm", "This is the subject for the authorized email.", "This is the body of the authorized mail!...");// Send the message using authorizationSmtpClient client = new SmtpClient("smtp.VeVB.COm");client.Credentials = new NetworkCredential("user", "password");client.EnableSsl = true;// Add the event handlerclient.SendCompleted += new SendCompletedEventHandler(mail_SendCompleted);// Send the message asynchronouslyclient.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");}希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選