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

本文关键字:文件 进程 另一个 因为 电子邮件 访问 错误 删除 | 更新日期: 2023-09-27 17:53:24

我正在做一个电子邮件表单。电子邮件有附件,附加文件后发送电子邮件。接下来需要从服务器中删除文件。当我试图获取该文件时,它给了我主题错误。我甚至在删除文件之前调用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指导和帮助。感谢

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

使用这个。它对我有效

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

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

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

您应该尝试oatt.Dispose();

执行"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;
oMess.Dispose()

这对我很有效。

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

pictureBox1.Load(filePath);   

这对我有效:

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);
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();
}