发送电子邮件后删除文件时出现“进程无法访问文件”错误

时间:2011-02-07 13:05:40

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

我收到的错误如下所述:

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

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

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

这里的directorypath是E:\ TempPDFs,filename是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);
                    }
                }
            }
        }
    }

如果有任何查询,请告诉我

谢谢!

2 个答案:

答案 0 :(得分:11)

我们能否看到负责通过电子邮件发送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();

希望这会对你有所帮助。

答案 1 :(得分:0)

或更好,如果对象实现IDisposable只是像这样写

((IDisposable)objSMTPClient).Dispose();