Email attachment and then delete on disk, access problem

Hi there all, would be great full if you have a good idea on how to deal with this problem, thanks! 

I am looping through a list of email addresses and sending them all the same email with an attachment.

This attachment (a .pdf file) only needs to exist while this is going on, and then needs to be deleted directly after all the emails have been sent.

The problem I am encountering is that once I have sent all the emails and try and delete the temporary file I get an access error, because something is still holding on to the file.

How can I solve this problem?

My code is something like the following summary.


try {

    string pathToAttachment = createTempPdf();

    foreach(string email in emails)
         sendEmail(email, pathToAttachment); // this method attaches the tempFile

} finally {

    // if file exists - then delete


Read More


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s