English 中文(简体)
delete attachment file
原标题:

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 pathfun.jpg because it is being used by another process. thank you

最佳回答

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.

问题回答

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

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);
}

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

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!




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签