smtp SendAsync always throws error "Failure sending email"

SweatCoder
SweatCoder used Ask the Experts™
on
I have an ASP.NET web page that calls a separate class library that attempts to send email async. When I send the email synchronously, it works perfectly:

smtp.Send(mail)

but when I send async:

smtp.SendCompleted += new SendCompletedEventHandler(SendEmailCompletedCallback);
smtp.SendAsync(mail, GetUserIDFromCookie());

private static void SendEmailCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
            //int token = int.Parse(e.UserState.ToString());

            if (e.Cancelled)
            {
                WriteLog("SendEmailCompletedCallback", "Async email cancelled.");
            }
            if (e.Error != null)
            {
                WriteLog("SendEmailCompletedCallback", "Async email error: " + e.Error.ToString());
            }
            else
            {
                WriteLog("SendEmailCompletedCallback", "Async email sent!");
            }
        }

The callback method is never reached. Exception thrown during SendAsync(..).

Everything else is the same. Credentials the same for both methods. I have Async="true" in my @Page declaration on the calling aspx page.

I have googled on this for awhile and read several articles. I can't find anything I'm missing.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
You full error is this?
Message: "Failure sending mail."
 
InnerException: {System.ObjectDisposedException: Cannot access a disposed object.
 Object name: 'System.Net.Mail.MailMessage'.
    at System.Net.Mail.MailMessage.get_AlternateViews()
    at System.Net.Mail.MailMessage.SetContent()
    at System.Net.Mail.MailMessage.BeginSend(BaseWriter writer, Boolean sendEnvelope, AsyncCallback callback, Object state)
    at System.Net.Mail.SmtpClient.SendMailCallback(IAsync¿­Result result)} System.Exception {System.ObjectDisposedException}
 
Are you using statement if yes remove it.

Author

Commented:
What does this mean: "Are you using statement if yes remove it."  ??
Top Expert 2011
Commented:
if you have: InnerException: {System.ObjectDisposedException: Cannot access a disposed object.


somwhere in yout code:
using(SmtpClient smtp = .... )
{

}

or smtp.Dispose.

Author

Commented:
Yes, the smtp.Dispose() was wrecking the object, causing the error. When I commented that out, everything worked great.

Author

Commented:
Sorry about the delay in coming back to this.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial