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
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Extending the MailMessage class is a good way to solve this and reduce the chances of running into this problem again...
... and remember to use with a 'using' construct (which you should be using anyway)...
If your mail have lots Attachments
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.
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.
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
You just need to dispose the message object before deleting the file. E.g: