473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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;
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
Feb 15 '07 #1
2 2016
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
Feb 15 '07 #2
Thanks, it saved a lot of my time.

Mar 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Lamonby | last post by:
Hi, I am sending a file from the server as an email attachment. The file is being attached no problem and sending the email, but I get an error when I try to open it saying it is corrupt....
6
by: ErwinF | last post by:
Hi there, I would like to know the following: How to send send email attachments using WebDAV in VB .NET? Sample code please................... Thanks for your help.
2
by: Joe George | last post by:
Hi there, How to save email attachments, from exchange, using WebDAV in C#.NET? Sample code please................... Thanks for your help. -- Joe George
1
by: David Veeneman | last post by:
I have a maddening problem that I hope someone can help with. I have some code that writes temporary documents to the data folder of my web site. On one document, I keep getting an error "The...
0
by: jackiewkc | last post by:
Hi, Does anyone know how to use VB to write a simple MAPI to save email attachments from a specified email address to my hard drive? What I basically want is that everytime when I receive an...
3
by: olafmol | last post by:
Hello, i want to write a PHP4 script that reads POP3 or IMAP mail and can decode the possible attachments in the email. I've looked around for some classes to do this, and found a few, but most...
1
by: shil | last post by:
Is there any possible way to create an email attachments from an URL in windows vb.net application? I'm using frame work 2.0, System.Net.Mail component. If I attach a file from my file system,...
0
by: =?Utf-8?B?amVhbm9mbWlubmVhcG9saXM=?= | last post by:
I am having problems opening email attachments when I receive as a Notepad. I have this problem in AOL or Outlook. When I open I get a box with letters, symbols and numbers. I have been told as...
1
by: rottmanj | last post by:
So after a few hours of playing with my config file, I think I have it to a point where I am ready to start adding in the rest of the components to my perl application. One thing that I know I need...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.