开发者

What could be the reasons for SendAsync to fail?

开发者 https://www.devze.com 2023-01-04 10:27 出处:网络
In the following code public static void Send(SmtpClient smtpClient, MailMessage email) { try { smtpClient.SendCompleted += (sender, e) =>

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

精彩评论

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