15

i am using System.Net.Mail for sending mail in asp.net.. how to delete attachment file after it is send as attachment mail.. i tried to use File.Delete method.. but i am getting this error.. the process cannot access the file path\fun.jpg' because it is being used by another process. thank you

SAK
  • 3,270
  • 7
  • 24
  • 38

5 Answers5

27

Dispose of the MailMessage when you're done with it. It still has a lock on the file you've added as an attachment until you've done so.

var filePath = "C:\\path\\to\\file.txt";
var smtpClient = new SmtpClient("mailhost");
using (var message = new MailMessage())
{
    message.To.Add("to@domain.com");
    message.From = new MailAddress("from@domain.com");
    message.Subject = "Test";
    message.SubjectEncoding = Encoding.UTF8;
    message.Body = "Test " + DateTime.Now;
    message.Attachments.Add(new Attachment(filePath));
}
if (File.Exists(filePath)) File.Delete(filePath);
Console.WriteLine(File.Exists(filePath));

Output: False

I would imagine that if you still have something locking the file after disposing the message, that you likely have another lock on the file, but without code, we can't help you.

Bob G
  • 1,206
  • 2
  • 16
  • 24
1

You can't delete a attached file after sending the mail.Before sending you can delete.

What the error says that, the path you have mentioned is using some other process.

MailMessage Message = new MailMessage();

Message.Subject = "Attachment Test";
Message.Body = "Check out the attachment!";
Message.To.Add("webmaster@15Seconds.com");
Message.From = "someone@somedomain.com";

Message.Attachments.Add(new Attachment(memorystream, "test.txt", MediaTypeNames.Application.Text));

Notice that we created the attachment from the MemoryStream and we got to name the attachment anything we want. The name of the attachment in the second parameter is the name of the file in the email, not the name on the local system hard drive. In fact the attachment never goes to the local hard drive. The third parameter is the Mime type of the attachment, in our case this is text.

Edit: use Dispose() the mail

anishMarokey
  • 10,805
  • 2
  • 31
  • 45
0

Extending the MailMessage class is a good way to solve this and reduce the chances of running into this problem again...

class MyMailMessage : MailMessage, IDisposable
{
    private List<string> _tempFiles = new List<string>();

    public void Attach(string filename)
    {
        base.Attachments.Add(new Attachment(filename));
        this._tempFiles.add(filename);
    }

    new public void Dispose()
    {
        base.Dispose();
        this._tempFiles.Foreach(x => File.Delete(x));
    }
}

... and remember to use with a 'using' construct (which you should be using anyway)...

using(SmtpClient client = GetMySmtpClient())
using(MyMailMessage msd = new MyMailMessage())
{
    msg.Attach(filename);
    client.send(msg);
}
Nine Tails
  • 920
  • 1
  • 10
  • 17
0

If your mail have lots Attachments

List<Attachments> lstAtt = new List<Attachments>();
Attachment att = new Attachment(file);
lstAtt.Add(att);

//finally
foreach(var a in lstAtt)
{
    a.Dispose();
}

//delete file
Max CHien
  • 113
  • 8
-1

You just need to dispose the message object before deleting the file. E.g:

    Dim message As New MailMessage
    message.From = New MailAddress(fromEmail, fromName)
    message.Subject = subject
    message.CC.Add(toCCEmail)
    message.Bcc.Add(toBCCEmail)
    Dim attach As Attachment = New Attachment(attachmentPath)
    message.Attachments.Add(attach)
    message.IsBodyHtml = True
    message.Body = body
    mailClient.Send(message)

    message.Dispose()   'Add this line to dispose the message!
rsc
  • 9,304
  • 4
  • 32
  • 31