删除附件文件 [英] delete attachment file
问题描述
我使用System.Net.Mail在asp.net发送邮件..
如何删除附件文件后,它作为附件发送邮件..
I试图使用File.Delete方法。 。但我得到这个错误..
中的进程无法访问该文件path\fun.jpg',因为它正由另一个进程使用。
谢谢
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
推荐答案
MAILMESSAGE的处置,当你用它做。
它仍然有对你作为附件添加,直到你已经这样做了该文件的锁。
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.
这篇关于删除附件文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!