我目前正在调试一个问题,但在MSDN 文档中找不到这个问题的答案
我有以下代码:
if(attachmentFileName != null && File.Exists(attachmentFileName))
{
mail.Attachments.Add(new Attachment(attachmentFileName, MediaTypeNames.Application.Octet));
}
using(SmtpClient smtp = new SmtpClient { UseDefaultCredentials = true })
{
try
{
smtp.Send(mail);
}
catch(SmtpException ex)
{
if(attachmentFileName != null && ex.StatusCode == SmtpStatusCode.ExceededStorageAllocation)
{
//Need to still send the mail. Just strip out the attachment & add footer saying that attachment has been stripped out.
mail.Attachments.Clear();
mail.Body += "\n\nNote: Please note that due to outbound size limitations, attachments to this email have been stripped out.\n";
smtp.Send(mail);
}
else
{
throw;
}
}
}
在更高级别(此方法的调用者),我有以下内容:
try
{
SendEmail(recipients, alertTitle, body, alertID, subjectPrefix, merchantIDValue, attachmentFilePath);
}
finally
{
if(tempFile != null)
{
File.Delete(tempFile);
}
}
我将代码部署到我们的测试环境中,现在我的错误日志中出现以下异常:
System.IO.IOException: The process cannot access the file 'C:\fileName.zip' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Delete(String path)
at AlertDelivery.CreateAttachmentFile(String attachmentData, String merchantID, String reportTitle) in d:\svn\trunk\Solution\Alerting\AlertDelivery.cs:line 143
at AlertDelivery.TrySendAlert(String merchantID, String reportTitle, Int32 alertID, String alertTitle, String attachmentData, String attachmentFilePath, String body, Boolean isReport, String subjectPrefix, List`1 recipients) in d:\svn\trunk\Solution\Alerting\AlertDelivery.cs:line 110
at AlertingService.ProcessAlertEvents(Object parameters) in d:\svn\trunk\Solution\Alerting\AlertingService.cs:line 174
第 174File.Delete(tempFile);
行是调用者代码中的行。
SmtpClient.Send
SmtpClient 是否在调用后对附件保持异步锁定?还有什么明显的问题吗?