说明:
我正在发送一封来自MVC应用程序的电子邮件,当我将邮件发送给一个人时,邮件被成功发送,但是当我向多个人发送邮件然后获得例外,即操作已经超时。
所以,为了避免这个问题,我正在尝试使用smtpclient.SendAsync(message,object)方法,但是我得到了一个例外,即异步模块或处理程序在异步操作仍未完成时完成。
如何解决此问题?
代码:
public async Task<string> SendEmail(List<string> ToEmailAddresses,string body, string emailSubject)
{
var smtp = new SmtpClient
{
DeliveryMethod = SmtpDeliveryMethod.Network,
Host = "xyz-host-name",
Port = 25,
EnableSsl = false,
Timeout = 600000
};
var fromAddress = new MailAddress(ConfigurationManager.AppSettings["MailUserName"], "Rewards and Recognition Team");
using (var message = new MailMessage() { Subject = emailSubject, Body = body })
{
message.From = fromAddress;
foreach (string email in ToEmailAddresses)
{
message.To.Add(email);
}
message.IsBodyHtml = true;
try
{
_logger.Log("EmailService-SendEmail-try");
object userToken = message;
smtp.SendAsync(message,userToken);
return "Success";
}
catch (Exception ex)
{
_logger.Log("EmailService-SendEmail-" + ex.Message);
return "Error";
}
}
}
答案 0 :(得分:1)
你必须在actionresult(控制器端)
中使用await关键字像
close[X]