我通過ASP.NET的發送電子郵件,當我試圖把他們在異步模式下,它沒有工作,arised錯誤: Failure sending mail. ---> System.InvalidOperationException: Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.
發送電子郵件在ASP異步失敗淨
這裏是我的代碼:
MailMessage message = new MailMessage();
message.From = new MailAddress("[email protected]");
message.To.Add(new MailAddress(to));
message.Subject = subject;
message.Body = body;
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
SmtpServer.Port = 587;
SmtpServer.Credentials = new NetworkCredential(userName, pass);
SmtpServer.EnableSsl = true;
SmtpServer.Send(message); //sends successful
SmtpServer.SendCompleted += SmtpServer_SendCompleted;
SmtpServer.SendAsync(message, null); //failure sending
在這個例子中
@Arran,臨屋後不幫我。因爲它有關於線程和'背景'的解釋,但是我有一個內置函數不起作用。 –
@Josh Mein,剛纔我注意到所有其他答案之間的答案,真的很難理解那裏的正確答案。 –