Does SmtpClient.Send with attachments lock the attachment?

I am currently debugging a problem and cannot find the answer to this question in the MSDN documentation

I have the following code:

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; } } } 

At a higher level (calling this method), I have the following:

 try { SendEmail(recipients, alertTitle, body, alertID, subjectPrefix, merchantIDValue, attachmentFilePath); } finally { if(tempFile != null) { File.Delete(tempFile); } } 

I deployed the code in our test environment, and now I get the following exception in our error logs:

 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 

Line 174 is the line File.Delete(tempFile); in the caller code.

Does SmtpClient support asynchronous attachment blocking after SmtpClient.Send been called? Any other noticeable issues?

+4
source share
2 answers

Try encapsulating the mail variable with the using statement. Something like this

 public void SendEmail(...) { using(MailMessage mail = new MailMessage()) { .... your code above } } 

this will force a dispose call on the MailMessage object, and this call also provides any attachments.

+10
source

This certainly does until you get rid of the MailMessage object.

+7
source

All Articles