Releasing file locks on email attachments?

Discussion in 'ASP .Net' started by David Veeneman, Feb 15, 2007.

  1. 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;
    client.Send(email);

    // Clear attachments
    if (email.Attachments.Count > 0)
    {
    email.Attachments.Clear();
    email.Attachments.Dispose();
    }

    // Dispose email
    email.Dispose();

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

    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
    David Veeneman, Feb 15, 2007
    #1
    1. Advertising

  2. 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
    occurs.

    David Veeneman
    Foresight Systems
    David Veeneman, Feb 15, 2007
    #2
    1. Advertising

  3. David Veeneman

    Guest

    Thanks, it saved a lot of my time.
    , Mar 5, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Veeneman

    Releasing file locks

    David Veeneman, Feb 14, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    339
    David Veeneman
    Feb 14, 2007
  2. David Veeneman

    Releasing file locks

    David Veeneman, Feb 14, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    576
    David Veeneman
    Feb 14, 2007
  3. codewarrior
    Replies:
    0
    Views:
    512
    codewarrior
    May 4, 2009
  4. Peter Green
    Replies:
    0
    Views:
    627
    Peter Green
    Nov 5, 2009
  5. DG

    Web service is not releasing SQL05 locks

    DG, Nov 20, 2007, in forum: ASP .Net Web Services
    Replies:
    5
    Views:
    322
Loading...

Share This Page