【问题标题】:Getting "process cannot access file" error when deleting files after sending email发送电子邮件后删除文件时出现“进程无法访问文件”错误
【发布时间】:2011-06-22 17:46:17
【问题描述】:

我收到如下所述的错误:

进程无法访问文件“E:\TempPDFs\Sample.pdf”,因为它正被另一个进程使用

我碰巧通过电子邮件发送 pdf,发送电子邮件后我需要删除 Sample.pdf 文件。我写的代码不起作用

FileInfo DeleteFileInfo = new FileInfo(directoryPath + "\\" + filename + ".pdf");
                            if (DeleteFileInfo.Exists)
                                File.Delete(directoryPath + "\\" + filename + ".pdf");

这里的目录路径是 E:\TempPDFs,文件名是 Sample

更新:

public static void SendMail(string fromAddress, string[] toAddress, string[] ccAddress, string[] bccAddress, string subject, string messageBody, bool isBodyHtml, ArrayList attachments, string host, string username, string pwd, string port)
    {

        {
            try
            {
                if (isBodyHtml && !htmlTaxExpression.IsMatch(messageBody))
                    isBodyHtml = false;
                // Create the mail message
                MailMessage objMailMsg;
                objMailMsg = new MailMessage();
                if (toAddress != null)
                {
                    foreach (string toAddr in toAddress)
                        objMailMsg.To.Add(new MailAddress(toAddr));
                }
                if (ccAddress != null)
                {
                    foreach (string ccAddr in ccAddress)
                        objMailMsg.CC.Add(new MailAddress(ccAddr));
                }
                if (bccAddress != null)
                {
                    foreach (string bccAddr in bccAddress)
                        objMailMsg.Bcc.Add(new MailAddress(bccAddr));
                }

                if (fromAddress != null && fromAddress.Trim().Length > 0)
                {
                    //if (fromAddress != null && fromName.trim().length > 0)
                    //    objMailMsg.From = new MailAddress(fromAddress, fromName);
                    //else
                    objMailMsg.From = new MailAddress(fromAddress);
                }

                objMailMsg.BodyEncoding = Encoding.UTF8;
                objMailMsg.Subject = subject;
                objMailMsg.Body = messageBody;
                objMailMsg.IsBodyHtml = isBodyHtml;

                if (attachments != null)
                {
                    foreach (string fileName in attachments)
                    {
                        if (fileName.Trim().Length > 0 && File.Exists(fileName))
                            objMailMsg.Attachments.Add(new Attachment(fileName));
                    }
                }

                //prepare to send mail via SMTP transport
                SmtpClient objSMTPClient = new SmtpClient();

                if (objSMTPClient.Credentials != null)
                {

                }
                else
                {
                    objSMTPClient.UseDefaultCredentials = false;
                    NetworkCredential SMTPUserInfo = new NetworkCredential(username, pwd);
                    objSMTPClient.Host = host;
                    objSMTPClient.Port = Int16.Parse(port);
                    //objSMTPClient.UseDefaultCredentials = false;
                    objSMTPClient.Credentials = SMTPUserInfo;
                    //objSMTPClient.EnableSsl = true;
                    //objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                }
                //objSMTPClient.Host = stmpservername;
                //objSMTPClient.Credentials
                //System.Net.Configuration.MailSettingsSectionGroup mMailsettings = null;
                //string mailHost = mMailsettings.Smtp.Network.Host;
                try
                {
                    objSMTPClient.Send(objMailMsg);
                }
                catch (SmtpException smtpEx)
                {
                    if (smtpEx.Message.Contains("secure connection"))
                    {
                        objSMTPClient.EnableSsl = true;
                        objSMTPClient.Send(objMailMsg);
                    }
                }
            }
        }
    }

如果有任何疑问请告诉我

谢谢!

【问题讨论】:

  • 仅供参考,当我收到此错误时,Sample.pdf 未打开。
  • 您的 SMTP 服务是否打开了文件以供读取(在您删除的同时发送邮件)?
  • 能否提供邮件发送代码?您是动态创建 PDF 吗?
  • 如果使用 SmtpClient 发送文件,则必须 Dispose 对象(using/Dispose)。
  • @qor72: 我不知道 SMTP 服务设置是否像打开文件一样完成,但我猜它没有被删除,因为邮件仍在发送中。

标签: c# asp.net email file-io visual-studio-2005


【解决方案1】:

如果对象实现了 IDisposable 就更好了,就这样写

((IDisposable)objSMTPClient).Dispose();

【讨论】:

    【解决方案2】:

    我们可以看到负责通过电子邮件发送 PDF 文件的代码吗?您的问题可能是由未释放的内存流引起的。如果您使用的是 Attachment 类,那么您应该执行以下操作:

    using (Attachment data = new Attachment("document.pdf",  MediaTypeNames.Application.Octet))
    {
        // 1. Adding attachment to the e-mail message
        // 2. Sending out the e-mail message
    }
    

    using 语句将确保在对象超出范围时调用Dispose 方法。

    更新

    调用Send 方法后,调用邮件消息对象的Dispose

    objSMTPClient.Send(objMailMsg);
    objMailMsg.Dispose();
    

    希望这会对你有所帮助。

    【讨论】:

    • 你可以看到一个代码,但正如我之前所说,它超过了任何 cmets 的最大限制
    • @Romil:不要将其作为评论发布。改为编辑您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多