471,311 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,311 software developers and data experts.

Releasing file locks on email attachments?

My web app creates several temporary files, then attaches them to an email
that it sends out. When the app attches the documents to the email, .NET
locks the files. My problem is that they aren't getting unlocked.

When the app runns, the first thing it does is delete the files left from
its last run. So, on the second run of the app, I'm getting this error
message: " The process cannot access the file 'D:\ ... \MyFile.txt' because
it is being used by another process." In other words, the server can't
delete the temporary files because the locks placed on them when they were
attached to the email in the previous run were never released.

I tried adding some code immediately after the email send in an attempt to
get the locks released:

************************************************** *********

// Send server error email
client.DeliveryMethod = SmtpDeliveryMethod.Network;

// Clear attachments
if (email.Attachments.Count 0)

// Dispose email

************************************************** ************

But that didn't do the trick.

Can anyone tell me how to release the locks placed on files when they are
attached to an email? Thanks.

David Veeneman
Foresight Systems
Feb 15 '07 #1
2 1927
I found my answer.

Apparently, you have to call Dispose() on each attachment--calling Dispose()
on the attachments collection isn't enough.

So, do a foreach iteration of the attachments collection, calling Dispose()
on each item in turn. That releases the file locks.

Then call Dispose() on the attachments collection, and on the email message.
These latter two steps aren't necessary to release the file locks, but they
are recommended to free up server resources the next time garbage collection

David Veeneman
Foresight Systems
Feb 15 '07 #2
Thanks, it saved a lot of my time.

Mar 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Paul Lamonby | last post: by
1 post views Thread by David Veeneman | last post: by
3 posts views Thread by olafmol | last post: by
reply views Thread by =?Utf-8?B?amVhbm9mbWlubmVhcG9saXM=?= | last post: by
1 post views Thread by rottmanj | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.