BrunoLM
BrunoLM

Reputation: 100331

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 object
        };
        smtpClient.SendAsync(email, null);
    }
    catch // never reach
    {
        // this works
        smtpClient.Send(email);
    }
}

Upvotes: 1

Views: 363

Answers (3)

BC.
BC.

Reputation: 24918

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.

Upvotes: 2

VoodooChild
VoodooChild

Reputation: 9784

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

smtpClient.SendAsync(email, "test");

Upvotes: -1

Sergej Andrejev
Sergej Andrejev

Reputation: 9403

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

Upvotes: 0

Related Questions