我通过 Asp .net 发送电子邮件,当我尝试以异步模式发送它们时,它不起作用,出现错误:
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.
这是我的代码:
MailMessage message = new MailMessage();
message.From = new MailAddress("chani.poz@gmail.com");
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