Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C#
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
SmtpClient client = new SmtpClient();

 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.
}
Posted
Updated 31-May-22 4:00am
Comments
Sergey Alexandrovich Kryukov 22-Jul-13 2:23am    
Do you understand that nothing is really "attached"? The file content is simply embedded in the body of the mail, as a "part".
So, strictly speaking, you don't need a source file.
—SA
gzdreamway 22-Jul-13 2:50am    
I omitted part of the code, the attachment is a picture
--------------------------------------------------------------------------
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
SmtpClient client = new SmtpClient();
string attachmentPath="c:\\test.jpg"
if(attachmentPath != "")
{
........
.......
}

Error is always self explanatory. Its not there just to show you that something went wrong,but further it also says something. Its very clear that file is being used by some process. In order to overcome this error,try to release that resource first.

You are trying to delete the file which is being attached and sent.

Regards..:laugh:
 
Share this answer
 
Comments
gzdreamway 22-Jul-13 2:37am    
try to release that resource
--------------------------------------------------------------------------
I release here , but not work.
dataAttach.Dispose();
Thanks7872 22-Jul-13 2:38am    
Try to release it at appropriate time,like after mail sent successfully.

msg.Attachments.Add(new Attachment(attachmentPath));

By this line,you have attached nothing in real sense.
gzdreamway 22-Jul-13 2:48am    
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
SmtpClient client = new SmtpClient();
string attachmentPath="c:\\test.jpg"

if(attachmentPath != "")
........
.......
I omitted part of the code, the attachment is a picture

C#
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();
    }
   }
 
Share this answer
 
Try to use attachment Dispose() method after sending the email

C#
smtpClient.SendCompleted += SendCompletedCallback;
await smtpClient.SendMailAsync(message);
message.Attachments.Dispose();
 
Share this answer
 
Comments
Dave Kreskowiak 31-May-22 10:35am    
You didn't understand the question nor the problem.

The attached files will be locked until they are uploaded to the email server. Disposing the attachments collection will not fix this problem, nor does it delete the files off the client machine after they are sent.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900