如何在AsP.NET的后台发送电子邮件?

Bar*_*art 2 asp.net email sendmail

我正在使用ASP.NET Web表单,

当用户提交页面时,会向许多人发送一封电子邮件,这会减缓后退,

发送电子邮件的最佳方式是什么,而不会减慢页面的重新加载速度?

谢谢

Wal*_*eza 7

您可以使用System.Net.Mail.SmtpClient类使用SendAsync()方法发送电子邮件.

var smtpClient = new SmtpClient();
var message = new MailMessage(fromAddress, toAddress, subject, body);
smtpClient.SendCompleted += new SendCompletedEventHandler(OnSendCompletedCallback);
smtpClient.SendAsync(message, null); // Null Or pass a user token to be send when the send is complete
Run Code Online (Sandbox Code Playgroud)

如果您需要在异步发送完成后执行一些额外的操作,您也可以订阅SmtpClient 的SendCompleted事件.

private void OnSendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
     // Handle the callback if you need to do anything after the email is sent.
}
Run Code Online (Sandbox Code Playgroud)

以下是MSDN文档的链接.