【发布时间】: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