3 Replies - 2615 Views - Last Post: 22 July 2013 - 06:30 AM Rate Topic: -----

#1 marklr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-July 13

(C#)how to delete attach file after send mail?

Posted 21 July 2013 - 11:39 PM

I use SmtpClient's SendAsync method,when after send mail, i delete the file ,tip The process cannot access the file because it is being used by another process.

System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
SmtpClient client = new SmtpClient();
string attachmentPath="c:\\test.jpg" 
 
 if(attachmentPath != "")
   {
    using (Attachment dataAttach = new Attachment(attachmentPath))
    {
        msg.Attachments.Add(new Attachment(attachmentPath));
        dataAttach.Dispose();
    }
   }
   .....
   .....
   client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
   client.SendAsync(msg, userState);
 
   private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
  {
    ......
    ......
    ......  
    System.IO.File.Delete(AttachFileNamePath);  //here tip: The process cannot access the file because it is being used by another process.
}



Is This A Good Question/Topic? 0
  • +

Replies To: (C#)how to delete attach file after send mail?

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: (C#)how to delete attach file after send mail?

Posted 22 July 2013 - 03:28 AM

To solve this problem, you basically need to call Dispose() on the MailMessage object (of which will then call Dispose() on the Attachment object, thereby releasing the lock on the file, and allowing you to delete it).

There are two obvious ways to do that:

1) Pass the MailMessage object to SendAsync(), via the userToken argument. Then, in the SendCompletedCallback method, cast e.UserState to the type MailMessage, and call Dispose() on it.

client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
var userState = msg;
client.SendAsync(msg, userState);
...
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
    MailMessage msg = (MailMessage)e.UserState; 
    msg.Dispose();
    System.IO.File.Delete(AttachFileNamePath);
}




Or

2) Use a lambda expression to subscribe to the event, and capture the reference to the MailMessage object directly:

MailMessage msg = new MailMessage();
...
client.SendCompleted += (o,e) 
{
    msg.Dispose();
    System.IO.File.Delete(AttachFileNamePath); 
};
client.SendAsync(msg, null);



Which one you use should depend on which one is the most maintainable for you, in your project.

Note also that if you are using .NET 4.0 or greater, you should also call Dispose() on the SmtpClient instance in a similar way (the SmtpClient instance is available by casting the sender argument passed to your SendCompletedCallback method).

Finally, you should get rid of your using statement around the dataAttach object. Using statements typically don't work very well with asynchronous operations as you end up disposing of the resource before the asynchronous operation that is using that resource completes. Plus, you aren't even using the dataAttach object anyway :)
Was This Post Helpful? 1
  • +
  • -

#3 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: (C#)how to delete attach file after send mail?

Posted 22 July 2013 - 03:30 AM

Unfortunately you can't know when the memory is released from your file. What you can do, however, is avoid the file entirely with something like this which is easier overall and bypasses the file at the point of sending anyway.

if(attachmentPath != "")
{
    var myAttach = new MemoryStream(File.ReadAllBytes(attachmentPath));
    msg.Attachments.Add(new Attachment(myAttach, "test.jpg"));
}



You will then be free to delete the file, as it's only open for the duration of reading the bytes from it.

This post has been edited by RudiVisser: 22 July 2013 - 03:31 AM

Was This Post Helpful? 3
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: (C#)how to delete attach file after send mail?

Posted 22 July 2013 - 06:30 AM

You'll also want to dispose of your MemoryStream. You could use a using statement for it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1