在电子邮件删除附件文件后,错误“进程无法访问该文件,因为它正被另一个进程使用”。

时间:2011-04-28 13:58:37

标签: c# .net asp.net

我正在做一个电子邮件表格。电子邮件附件,附加文件后发送电子邮件。接下来需要从服务器删除文件。当我试图获取文件时,它给了我主题错误。我甚至在删除文件之前调用GC.Collect()但仍然存在错误。我删除文件的代码是:

 private void DeleteFiles(DataTable dt)
{
    GC.Collect();
    String[] sAttachments = new String[dt.Rows.Count];
    try
    {

        sAttachments = new String[dt.Rows.Count];
        for (Int32 J = 0; J < dt.Rows.Count; J++)
        {
            sAttachments[J] = dt.Rows[J]["AttachmentExt"].ToString().Trim();
            string workDir = Server.MapPath(".") + @"\upload\";
            if (File.Exists(workDir + sAttachments[J]))
                File.Delete(workDir + sAttachments[J]);                
        }
    }
    catch (Exception ex)
    {

    }

要将文件附加到电子邮件,我的代码是:

 oMess.Subject = sSubject; 
        string workDir = System.Web.HttpContext.Current.Server.MapPath(".") + @"\upload\";
        if (Attachments != null)
        {
            for (Int32 I = 0; I < Attachments.Length; I++)
            {
                oatt = new Attachment(workDir+ sAttachments[I]);
                oMess.Attachments.Add(oatt);
            }
        }
        oMess.IsBodyHtml = IsHtml;
        oMess.Body = sBody;
       SendMessageGmail(oMess);

编辑:我的邮件发送代码是:

 private void SendMessageGmail(MailMessage message)        
    {
        SmtpClient client = new SmtpClient("smtp.gmail.com");
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        NetworkCredential loginInfo = new NetworkCredential("myid", "mypassword");
        client.Credentials = loginInfo;
        client.Port = 587;
        client.Send(message);
    }

Plz指南和帮助。感谢

7 个答案:

答案 0 :(得分:16)

使用这个。它对我有用

client.Send(oMess);
oMess.Attachments.Dispose();

我试过这个,但它对我不起作用

client.Send(oMess);
oMess.Dispose();

答案 1 :(得分:12)

你应该尝试做 oatt.Dispose();

答案 2 :(得分:3)

执行“SendMessageGmail”后,通过迭代释放MailMessage.Attachments的附件。

SendMessageGmail(oMess);
if(oMess.Attachments != null) {
  for(Int32 I = oMess.Attachments.Count - 1; I >= 0;I--) {
    oMess.Attachments[i].Dispose();
  }
  oMess.Attachments.Clear();
  oMess.Attachments.Dispose();
}
oMess.Dispose();
oMess = null;

答案 3 :(得分:2)

使用以下代码填充图像框,这将解决删除文件的问题。

pictureBox1.Load(filePath);   

答案 4 :(得分:2)

oMess.Dispose()

这对我有用。

答案 5 :(得分:1)

这对我有用:

FileStream file = new FileStream(attachment, FileMode.Open, FileAccess.Read);
MemoryStream ms = new MemoryStream();
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
ms.Write(bytes, 0, (int)file.Length);

objEmail.Attachments.Add(new Attachment(ms, new FileInfo(attachment).Name));

file.Close();
ms.Close();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(attachment);

答案 6 :(得分:0)

private void SendMessageGmail(MailMessage message)       
{
    SmtpClient client = new SmtpClient("smtp.gmail.com");
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    NetworkCredential loginInfo = new NetworkCredential("myid", "mypassword");
    client.Credentials = loginInfo;
    client.Port = 587;
    client.Send(message);
    oatt.Dispose();
}