title: C# how to correctly dispose of an SmtpClient?
body: <p>VS 2010 code analysis reports the following:</p>
  
  <p>Warning 4   CA2000 : Microsoft.Reliability : In method 'Mailer.SendMessage()', object 'client' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'client' before all references to it are out of scope.  </p>
  
  <p>My code is :</p>
  
  <pre><code>public void SendMessage()
      {
          SmtpClient client = new SmtpClient();
  
          client.Send(Message);
          client.Dispose(); 
          DisposeAttachments(); 
      }
  </code></pre>
  
  <p>How should I correctly dispose of client?</p>
  
  <p><strong>Update:</strong> to answer Jons question, here is the dispose attachments functionality:</p>
  
  <pre><code>private void DisposeAttachments()
  {
      foreach (Attachment attachment in Message.Attachments)
      {
          attachment.Dispose();
      }
      Message.Attachments.Dispose();
      Message = null; 
  }
  </code></pre>
  
  <p><strong>Last Update</strong> full class listing (its short)</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net.Mail;
  
  public class Mailer
  {
      public MailMessage Message
      {
          get;
          set;
      }
  
      public Mailer(MailMessage message)
      {
          this.Message = message; 
      }
  
      public void SendMessage()
      {
          using (SmtpClient client = new SmtpClient())
          {
              client.Send(Message);
          }
          DisposeAttachments(); 
      }
  
      private void DisposeAttachments()
      {
          foreach (Attachment attachment in Message.Attachments)
          {
              attachment.Dispose();
          }
          Message.Attachments.Dispose();
          Message = null; 
      }
  }
  </code></pre>