C#完成異步發送郵件的辦法。本站提示廣大學習愛好者:(C#完成異步發送郵件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成異步發送郵件的辦法正文
本文實例講述了C#完成異步發送郵件的辦法。分享給年夜家供年夜家參考。詳細以下:
上面的代碼可以完成異步發送郵件,等郵件發送出去後會主動挪用回調函數,如許在發送郵件時就不會卡住法式不動了
MailMessage m = new MailMessage ("[email protected]", "[email protected]", "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#法式設計有所贊助。