-2

SmtpClient.SendAsync()呼び出しは、SmtpClient.Send()のように結果を返しませんが、続行し、結果をビューに表示できません。では、ここでコールバック関数をフックし、電子メールの送信結果/エラーを取得してビューに表示するにはどうすればよいですか?

ありがとう。

4

1 に答える 1

0

2つの選択肢があります。

a)SmtpClient.Send()代わりに電話してください。

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 に答える