我使用System.Net.Mail在asp.net中發送郵件.. 如何刪除附件文件後它作爲附件郵件發送.. 我試圖使用File.Delete方法..但我得到這個錯誤.. 該進程無法訪問文件路徑\ fun.jpg',因爲它正在被另一個進程使用。 謝謝刪除附件文件
Q
刪除附件文件
14
A
回答
26
MAILMESSAGE的處置,當你用它做。 它仍然鎖定您作爲附件添加的文件,直到您完成。
var filePath = "C:\\path\\to\\file.txt";
var smtpClient = new SmtpClient("mailhost");
using (var message = new MailMessage())
{
message.To.Add("[email protected].com");
message.From = new MailAddress("[email protected]");
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));
輸出:假
我可以想象,如果你還有什麼配置的消息後,文件鎖定,你可能對文件的另一個鎖,但沒有代碼,我們不禁您。
1
不能發送mail.Before發送您可以刪除後刪除附加的文件。
什麼錯誤說,你提到的路徑是使用其他進程。
MailMessage Message = new MailMessage();
Message.Subject = "Attachment Test";
Message.Body = "Check out the attachment!";
Message.To.Add("[email protected]");
Message.From = "[email protected]";
Message.Attachments.Add(new Attachment(memorystream, "test.txt", MediaTypeNames.Application.Text));
請注意,我們從MemoryStream創建了附件,我們得到了附件的任何我們想要的名稱。在第二個參數附件的名稱是在電子郵件中的文件,而不是本地系統硬盤驅動器上的名稱的名稱。事實上,附件永遠不會去到本地硬盤。第三個參數是附件的Mime類型,在我們的例子中,這是文本。
編輯:use Dispose() the mail
0
擴展類MAILMESSAGE是解決這一點,並減少運行到再次這一問題的機會的好方法...
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));
}
}
...記住了「使用」結構來使用(這你應該使用任何方式)...
using(SmtpClient client = GetMySmtpClient())
using(MyMailMessage msd = new MyMailMessage())
{
msg.Attach(filename);
client.send(msg);
}
0
你只需要在刪除文件之前處理消息對象。 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!
0
如果您的郵件有很多附件
List<Attachments> lstAtt = new List<Attachments>();
Attachment att = new Attachment(file);
lstAtt.Add(att);
//finally
foreach(var a in lstAtt)
{
a.Dispose();
}
//delete file
相關問題
- 1. 的Sharepoint附件刪除文件名
- 2. 刪除CouchDb中的文檔附件
- 3. 刪除(附近)重複文件
- 4. 刪除所有文件附加信息?
- 5. 附加到pickle文件而不刪除
- 6. JQuery刪除附加的文件
- 7. 用php刪除mime附件
- 8. 刪除所有附件
- 9. MVCMailer SendAsync並刪除附件
- 10. 動態刪除附件?
- 11. VBA - Outlook不刪除附件
- 12. Wordpress刪除附件功能
- 13. 文件#刪除不刪除文件
- 14. 刪除電子郵件附件在Outlook
- 15. 發送電子郵件附件後刪除c#文件
- 16. 作爲郵件附件發送後刪除pfd文件
- 17. SevenZipSharp - 將文件附加到zip並刪除文件路徑
- 18. 從文件夾和文件中刪除附加的「-1」
- 19. 刪除文件
- 20. 刪除文件
- 21. 刪除文件
- 22. 刪除文件
- 23. 文件刪除
- 24. 刪除文件
- 25. 刪除文件
- 26. 刪除文件
- 27. 刪除文件
- 28. Delphi - TIdAttachment,從附件的文件名中刪除路徑
- 29. 通過SmtpClient(ASP.NET MVC)發送郵件後刪除附加文件
- 30. 十月CMS - 刪除未保存模型的文件附件
我認爲你需要處理的附件對象。 – anishMarokey 2010-05-18 12:43:09