Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8265907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:58:01+00:00 2026-06-08T04:58:01+00:00

Reviewing the MSDN docs for SmtpClient , I noticed that the example code looks

  • 0

Reviewing the MSDN docs for SmtpClient, I noticed that the example code looks like this:

client.SendAsync(message, userState);
// More stuff
message.Dispose();

The call to SendAsync could take significant time to complete. How is it that message remains in a useful state if it is disposed on the line following the SendAsync call?

Further, SmtpClient implements IDisposable. The MSDN example should more property be formed like

using (SmtpClient client = new SmtpClient())
{
    // Initialize client and create the message
    using (MailMessage message = new MailMessage(from, to))
    {
        client.SendAsync(message, userState);
        // More stuff
    }
}

So not only would message.Dispose() be called while client.SendAsync() is potentially still executing, but client.Dispose() would be called as well.

How is it that client remains in a useful state for the completion of SendAsync() if Dispose() is called almost immediately after the call to SendAsync()?

Or, is the MSDN example just wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:58:03+00:00Added an answer on June 8, 2026 at 4:58 am

    You should dispose it only after async send operation completed or canceled.
    So dispose it in this situation in SendCompleted event handler

    Look at sample in MSDN:

    http://msdn.microsoft.com/en-us/library/x5x13z6h.aspx

    Or this blog post

    http://leedumond.com/blog/new-in-net-4-dont-forget-to-dispose-your-smtpclient-instances/

    Example on MSDN is bad. And you are not the first person with this problem look at comments there:

    Bad Example

    After laying out the important reasons to Dispose() your
    SmtpClient, this example does not do so. Also, Dispose() is called on
    MailMessage possibly before SendAync has completed, which I’m quite
    sure has caused exceptions for me. You can dispose of the MailMessage
    in the SendComplete event handler however.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

reviewing the code example on this page, it seem like the example is initializing
Reviewing some code that contains a bunch indirectly nested transactionscopes. I would like to
When reviewing on different OS's, I noticed that particularly in Chrome and Safari, words
I was reviewing the CSS docs for version 4.6 and saw this in the
When reviewing code I came across something odd, someone had read that you can
I am reviewing some ClearCase triggers written in Perl. I have noticed that in
I was reviewing some of my old code and I have a method that
I was reviewing code and found that $(#item1 #item2) and $(#item1>#item2) are used interchangeably.
I'm reviewing some code for a friend and say that he was using a
I've been reviewing the Apple docs and sample code to try to determine the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.