(C#)发送邮件后如何删除附件? [英] (C#)how to delete attach file after send mail?

查看:521
本文介绍了(C#)发送邮件后如何删除附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SmtpClient的SendAsync方法,当发送邮件后,我删除文件,提示进程无法访问该文件,因为它正被另一个进程使用。



I use SmtpClient's SendAsync method,when after send mail, i delete the file ,tip The process cannot access the file because it is being used by another process.

System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
SmtpClient client = new SmtpClient();

 if(attachmentPath != "")
   {
    using (Attachment dataAttach = new Attachment(attachmentPath))
    {
        msg.Attachments.Add(new Attachment(attachmentPath));
        dataAttach.Dispose();
    }
   }
   .....
   .....
   client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
   client.SendAsync(msg, userState);

   private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
  {
    ......
    ......
    ......  
    System.IO.File.Delete(AttachFileNamePath);  //here tip: The process cannot access the file because it is being used by another process.
}

推荐答案

错误总是自我解释。它并不仅仅是为了向你展示出现了问题,而是它还说了些什么。很明显,某个进程正在使用该文件。为了克服这个错误,请先尝试释放该资源。



您正在尝试删除附加和发送的文件。



问候..:笑:
Error is always self explanatory. Its not there just to show you that something went wrong,but further it also says something. Its very clear that file is being used by some process. In order to overcome this error,try to release that resource first.

You are trying to delete the file which is being attached and sent.

Regards..:laugh:


我省略了部分代码,附件是图片



I omitted part of the code, the attachment is a picture

System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
SmtpClient client = new SmtpClient();
string attachmentPath="c:\\test.jpg" 
 
 if(attachmentPath != "")
   {
    using (Attachment dataAttach = new Attachment(attachmentPath))
    {
        msg.Attachments.Add(new Attachment(attachmentPath));
        dataAttach.Dispose();
    }
   }


这篇关于(C#)发送邮件后如何删除附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆