本文實例講述了C#實現異步發送郵件的方法。分享給大家供大家參考。具體如下:
下面的代碼可以實現異步發送郵件,等郵件發送出去后會自動調用回調函數,這樣在發送郵件時就不會卡住程序不動了
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");}
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答