如何在C#.net中作为附件邮件发送后删除附件文件 [英] how to delete attachment file after it is send as attachment mail in C#.net

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

问题描述

我正在使用System.Net.Mail在asp.net中发送邮件。如何作为附件邮件发送后删除附件文件。我试图使用File.Delete方法..但我收到此错误。该进程无法访问文件路径\ 123412.html'',因为它正被另一个进程使用。请给我一个最佳解决方案



我正在使用此代码



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\123412.html'' because it is being used by another process.please give me a best solution

I am using this code

string mailUserName = ConfigurationManager.AppSettings["MailUsername"].ToString();
          string mailPassword = ConfigurationManager.AppSettings["MailPassword"].ToString();
          string mailHostAddress = ConfigurationManager.AppSettings["HostAddress"].ToString();

          MailMessage mail = new MailMessage(mailUserName, EmailAddress.ToString());
          mail.Body = "<FONT face=Arial Black color=#3377b4 size=3>Dear Customer;
          mail.Attachments.Add(new Attachment(pfname));
          mail.Subject = "Your Ticket(" + strSPNR + ") confirmation";
          mail.IsBodyHtml = true;

          SmtpClient smtp;
          System.Net.NetworkCredential basicAuth;


          //            mail.Body = "<FONT face=Arial Black color=#3377b4 size=3>Dear Customer;

          smtp = new SmtpClient();
          basicAuth = new System.Net.NetworkCredential(mailUserName, mailPassword);
          smtp.EnableSsl = false;
          smtp.Host = mailHostAddress;
          smtp.UseDefaultCredentials = false;
          smtp.Credentials = basicAuth;
          try
          {
              smtp.Send(mail);
              mail.Attachments.Clear();
              ((IDisposable)mail).Dispose();

              if (File.Exists(pfname))
              {
                   File.Delete(pfname);

              }



我正在使用此代码。文件保存在服务器路径中但文件未删除。我正在使用''File.Delete(pfname);''。但文件没有删除。如何解决............



问候


I am using this code. file saved in server path but file did not delete . I am using ''File.Delete(pfname);'' . but file not deleting . how to solve............

regards

推荐答案

我的首先想到的是使用中的文件异常是因为电子邮件客户端尚未发送消息并仍在使用该文件。你可以编写一个定时服务,按计划清除文件夹。
My first thought would be the file in use exception is because the email client has not yet sent the message and still is using the file. You could write a timer service that clears the folder on a scheduled basis.


try
            {
                smtp.Send(mail);
            }
              finally
            {
                  mail.Attachments.Clear();
                  if (File.Exists(pfname))
                {
                     File.Delete(pfname);
                    
                }
            }


我想如果使用Send方法,块终止工作,但如果解决类似的情况如何我们通过SendAsync方法发送邮件
i guess finally blocks works if you use Send method, but how to solve similar case if we send mail through SendAsync method


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

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