現在問題をデバッグしていますが、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
行 174 はFile.Delete(tempFile);
、呼び出し元コードの行です。
SmtpClient は、呼び出された後、添付ファイルの非同期ロックを維持しますSmtpClient.Send
か? 他に目立った問題はありますか?