Zholen
Zholen

Reputation: 1790

Unknown Failure to Send Email when using Async

I have a website which needs to send an email on a particular action.

        var mail = new MailMessage("[email protected]",
                   "[email protected]", "Generic Subject Title",
                   string.Format("Some body content");

        using (var eServer = new SmtpClient("mailsrv.mycompany.com"))
        {
            eServer.Send(mail);
        }

The above code works fine, though the Send function takes between 8-15 seconds to complete, I then receive the email a few seconds later. I would like to remove the delay in page response at the very least.

        using (var eServer = new SmtpClient("mailsrv.mycompany.com"))
        {
            eServer.SendMailAsync(mail);
        }

When using the above my page loads quickly and finishes all other processes but I never receive the email.

Does anyone have an idea why I wouldn't receive the email simply because I'm using the Async version?

Upvotes: 0

Views: 45

Answers (1)

asfaloth.arwen
asfaloth.arwen

Reputation: 126

Because of the using block, you dispose the 'eServer' variable before it has a chance to actually send the mail. If you use SendMailAsync, you should not directly dispose the SmtpClient afterwards.

Upvotes: 1

Related Questions