title: C# Smtp delete file after sending as an attachment; denied because it's in use?
body: <p>I have problem after sending an email with an attachment. 
  After such a send I would like to delete this file and the process is denied because another process is using the file I want to delete. </p>
  
  <p>This is message I am getting: 
  "The process cannot access the file 'C:\Users\x\AppData\Roaming\ipconfig.txt' because it is being used by another process."</p>
  
  <p>There is some code from me:</p>
  
  <pre><code>    public static void SendAnEmail(string host, string username, string password, string mailTo, string subject, string body, List&lt;string&gt; attachmentsFileNames)
      {
          SmtpClient client = new SmtpClient();
          client.Port = 587;
          client.Host = host;
          client.EnableSsl = true;
          client.Timeout = 60000;
          client.DeliveryMethod = SmtpDeliveryMethod.Network;
          client.UseDefaultCredentials = false;
          client.Credentials = new System.Net.NetworkCredential(username, password);
  
          MailMessage mailMessage = new MailMessage(username, mailTo, subject, body);
          foreach(string singleAttachmentFileName in attachmentsFileNames)
          {
              Attachment attachment = new Attachment(singleAttachmentFileName, "text/plain");
              mailMessage.Attachments.Add(attachment);
          }
          mailMessage.BodyEncoding = UTF8Encoding.UTF8;
          mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
      }
  </code></pre>
  
  <p>And here is the call of the SendAnEmail function and try of deleting those files I sent as an attachments.</p>
  
  <pre><code>            Transport.SendAnEmail(Settings.mailHost, Settings.mailUsername, Settings.mailPassword, Settings.mailTargetAddress, Settings.ftpDirectory, " ", Settings.attachmentsListFileNames);
  
              foreach(string singlePath in Settings.attachmentsListFileNames)
                  File.Delete(singlePath);
  </code></pre>
  
  <p>I would like an advise how I may resolve this issue and after send an email with attachment - delete those attachment.</p>
  
  <p>Please ask me if something is not clear.</p>
  
  <p>edit:
  Sure, I am using C#, framework 4.5, WPF. Platform targeted is Windows and the environment used to programming is Visual Studio 2013 :)</p>