gpt4 book ai didi

c# - 发送电子邮件后删除文件时出现 "process cannot access file"错误

转载 作者:太空狗 更新时间:2023-10-29 21:10:50 24 4
gpt4 key购买 nike

我收到如下所述的错误:

进程无法访问文件“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);
}
}
}
}
}

如果有任何问题请告诉我

谢谢!

最佳答案

我们可以看到负责通过电子邮件发送 PDF 文件的代码吗?您的问题可能是由未释放的内存流引起的。如果您使用的是 Attachment class 然后你应该像下面这样:

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

希望对您有所帮助。

关于c# - 发送电子邮件后删除文件时出现 "process cannot access file"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4921641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com