-2

SmtpClient.SendAsync() 调用不会像 SmtpClient.Send() 那样返回结果,但会继续并且无法在视图中显示结果。那么如何在这里挂钩回调函数,获取发送电子邮件结果/错误并将其显示在视图中?

谢谢。

4

1 回答 1

0

你有两个选择:

SmtpClient.Send()a)改为打电话。

b)SmtpClient.SendAsync()从异步控制器调用它:

public class HomeController : AsynController
{
    [HttpPost]
    public void IndexAsync()
    {
        SmtpClient client = new SmtpClient();

        client.SendCompleted += (s,e) =>
        {
            AsyncManager.Parameters["exception"] = e.Error;
            AsyncManager.OutstandingOperations.Decrement();
        };

        AsyncManager.OutstandingOperations.Increment();

        client.Send(GetMessage());
    }

    public void IndexCompleted(Exception exception)
    {
        if (exception != null)
        {
            ModelState.AddError("", "Email send failed");
            return View();
        }
        else
        {
            return Redirect("Complete");
        }
    }
}
于 2013-01-19T22:18:27.723 回答