开发者

What could be the reasons for SendAsync to fail?

In the following code

public static void Send(SmtpClient smtpClient, MailMessage email)
{
    try
    {
        smtpClient.SendCompleted += (sender, e) =>
        {
            var x = e.Error; // can't access discarded o开发者_如何学Cbject
        };
        smtpClient.SendAsync(email, null);
    }
    catch // never reach
    {
        // this works
        smtpClient.Send(email);
    }
}


Your smtpClient object has been disposed or finalized after the call to Send has been completed but before the asynchronous send method can be run. Try moving the scope of the variable that is passed to the Send method so that it lasts through the asynchronous execution.

Another gotcha is that only one SendAsync call can be executed at a time. You have to implement your own wait queue in order to reliably use SendAsync or else an InvalidOperationException is thrown.


Your mail can be recognized as spam. Check in your spam folder


Not sure about this - but try passing in something other than null in the userToken

smtpClient.SendAsync(email, "test");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜