Visual studio feeds

All Visual Studio blogs in one place


Enter your email address:

Delivered by FeedBurner

Increase your website traffic with



Anti-spam: How many eyes has a typical person?

Follow us on FB


Releasing Email File Attachment Resources (System.Net.Mail)

AddThis Social Bookmark Button
I’ve been looking through the temp directories on my production machines and had realized some overlooked design points in a number of programs; namely the deletion of certain temp files.  One situation that particularly caught my attention was some email attachments that should have been deleted.
Even though the code was actively calling File.Delete() on some email attachments, they were not being deleted.
An error log showed the program was throwing an exception when attempting to delete the files.
The mail was being delivered successfully with no visible flaws to the customer. I finally decided to write some test
code to reproduce the problem that I originally thought was caused by the ZIP compression step right before the mail send. The problem actually turned out to be I was not calling Dispose() on the attachment resources after the mail send.  Most Dot Net classes I use don’t require the explicit calling of a Dispose() method, so I completely bypassed this one until it became necessary.  Here’s an example of the solution:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Mail;
Home : Blog List : Tom Hines ( : Releasing Email File Attachment Resources (System.Net.Mail)