473,320 Members | 2,052 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,320 software developers and data experts.

Problem accessing uploaded file.

When using SmtpClient and sending a message with an attached file using
SendAsync(...), if I add File.Delete(pathToAttachedFile) in the
SendCompleted event handler I get a file in use exception. Is there a
way to overcome this problem?

Thanks,
Nick Z.
Dec 5 '05 #1
5 2783
Nick,

It seems you are generating a temporary file which is attached to the
email, and then mailed.

What I would do here is that when creating the file, make a call to
CreateFile through the P/Invoke layer. When you call this, pass
FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE.
FILE_ATTRIBUTE_TEMPORARY will prevent the file from being written to the
disk if sufficient cache memory is available. FILE_FLAG_DELETE_ON_CLOSE
will cause the file to be deleted when it is closed.

Once you have that, create your FileStream (passing the handle returned
from CreateFile), and pass that to the constructor for the Attachment that
you add to the email.

That way, once the file is sent, and you call dispose on the Message,
the file will be deleted automatically.

The reason this happens is for one of two reasons. First could be that
you are using the constructor for Attachment which is taking the filename as
a string. When this happens, it opens the file for read access, and only
allows read operations to be performed on the file while the stream is open.
The stream is still open when the event is fired.

The second could be that you are not setting the share mode on your
FileStream to the appropriate value when creating it. You could fix it on
this level, and then manually delete the file, but I think letting the OS do
it is better.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nick Z." <pa*****@gmail.com> wrote in message
news:Z2****************@fe09.lga...
When using SmtpClient and sending a message with an attached file using
SendAsync(...), if I add File.Delete(pathToAttachedFile) in the
SendCompleted event handler I get a file in use exception. Is there a way
to overcome this problem?

Thanks,
Nick Z.

Dec 5 '05 #2
Thanks for the info. The problem is I only used File.Delete() as an
example, I actually want to write to the file. Sorry for not clarifying
that. In other words, write to file -> send it as attachment -> write to
the same file again once the attachment is sent. It seems the file is
never released by the SmtpClient or MailMessage classes.

Thanks again,
Nick Z.

Nicholas Paldino [.NET/C# MVP] wrote:
Nick,

It seems you are generating a temporary file which is attached to the
email, and then mailed.

What I would do here is that when creating the file, make a call to
CreateFile through the P/Invoke layer. When you call this, pass
FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE.
FILE_ATTRIBUTE_TEMPORARY will prevent the file from being written to the
disk if sufficient cache memory is available. FILE_FLAG_DELETE_ON_CLOSE
will cause the file to be deleted when it is closed.

Once you have that, create your FileStream (passing the handle returned
from CreateFile), and pass that to the constructor for the Attachment that
you add to the email.

That way, once the file is sent, and you call dispose on the Message,
the file will be deleted automatically.

The reason this happens is for one of two reasons. First could be that
you are using the constructor for Attachment which is taking the filename as
a string. When this happens, it opens the file for read access, and only
allows read operations to be performed on the file while the stream is open.
The stream is still open when the event is fired.

The second could be that you are not setting the share mode on your
FileStream to the appropriate value when creating it. You could fix it on
this level, and then manually delete the file, but I think letting the OS do
it is better.

Hope this helps.

Dec 5 '05 #3
Nick,

It should be released when you call the Dispose method on the
MailMessage class. MailMessage implements IDiposable, and you should make
sure you dispose of it correctly. The best way to do this, since you are
sending asychronously, is to send the MailMessage as the state when you send
it.

Then, in your event handler, call Dispose on the MailMessage instance
that you sent. When that is done, you will have to re-open the file for
writing, since the stream will have been closed down by calling Dispose on
the MailMessage instance.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nick Z." <pa*****@gmail.com> wrote in message
news:sO****************@fe12.lga...
Thanks for the info. The problem is I only used File.Delete() as an
example, I actually want to write to the file. Sorry for not clarifying
that. In other words, write to file -> send it as attachment -> write to
the same file again once the attachment is sent. It seems the file is
never released by the SmtpClient or MailMessage classes.

Thanks again,
Nick Z.

Nicholas Paldino [.NET/C# MVP] wrote:
Nick,

It seems you are generating a temporary file which is attached to the
email, and then mailed.

What I would do here is that when creating the file, make a call to
CreateFile through the P/Invoke layer. When you call this, pass
FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE.
FILE_ATTRIBUTE_TEMPORARY will prevent the file from being written to the
disk if sufficient cache memory is available. FILE_FLAG_DELETE_ON_CLOSE
will cause the file to be deleted when it is closed.

Once you have that, create your FileStream (passing the handle
returned from CreateFile), and pass that to the constructor for the
Attachment that you add to the email.

That way, once the file is sent, and you call dispose on the Message,
the file will be deleted automatically.

The reason this happens is for one of two reasons. First could be
that you are using the constructor for Attachment which is taking the
filename as a string. When this happens, it opens the file for read
access, and only allows read operations to be performed on the file while
the stream is open. The stream is still open when the event is fired.

The second could be that you are not setting the share mode on your
FileStream to the appropriate value when creating it. You could fix it
on this level, and then manually delete the file, but I think letting the
OS do it is better.

Hope this helps.


Dec 5 '05 #4
That did it!

Thank you,
Nick Z.

Nick Z. wrote:
Thanks for the info. The problem is I only used File.Delete() as an
example, I actually want to write to the file. Sorry for not clarifying
that. In other words, write to file -> send it as attachment -> write to
the same file again once the attachment is sent. It seems the file is
never released by the SmtpClient or MailMessage classes.

Thanks again,
Nick Z.

Nicholas Paldino [.NET/C# MVP] wrote:
Nick,

It seems you are generating a temporary file which is attached to
the email, and then mailed.

What I would do here is that when creating the file, make a call
to CreateFile through the P/Invoke layer. When you call this, pass
FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE.
FILE_ATTRIBUTE_TEMPORARY will prevent the file from being written to
the disk if sufficient cache memory is available.
FILE_FLAG_DELETE_ON_CLOSE will cause the file to be deleted when it is
closed.

Once you have that, create your FileStream (passing the handle
returned from CreateFile), and pass that to the constructor for the
Attachment that you add to the email.

That way, once the file is sent, and you call dispose on the
Message, the file will be deleted automatically.

The reason this happens is for one of two reasons. First could be
that you are using the constructor for Attachment which is taking the
filename as a string. When this happens, it opens the file for read
access, and only allows read operations to be performed on the file
while the stream is open. The stream is still open when the event is
fired.

The second could be that you are not setting the share mode on
your FileStream to the appropriate value when creating it. You could
fix it on this level, and then manually delete the file, but I think
letting the OS do it is better.

Hope this helps.

Dec 5 '05 #5
Nick,

Keep in mind that it may take time from the moment the API for closing a
file is called at the moment a file is actually closed. This is because file
closing API just put a request in a queue. When the request is going to be
served by the kernel... well no one can say for sure. Experience for me says
that it is almost impossible to have
[close file]
[delete file]

adjacent in your code. At least you need to separate them with some idle or
sleep time
--

Stoitcho Goutsev (100) [C# MVP]

"Nick Z." <pa*****@gmail.com> wrote in message
news:Z2****************@fe09.lga...
When using SmtpClient and sending a message with an attached file using
SendAsync(...), if I add File.Delete(pathToAttachedFile) in the
SendCompleted event handler I get a file in use exception. Is there a way
to overcome this problem?

Thanks,
Nick Z.

Dec 6 '05 #6

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

Similar topics

2
by: Michele Belloli | last post by:
Hi folks... I have a problem uploading a file to a web server. I created an html page with a multipart/form-data form and used cgi.parse_multipart function to obtain form data (and obviuosly...
1
by: Jonathan | last post by:
Hi everyone, I have a problem with the file uploading in Asp.Net and I have read a lot on forums on this but never found an answer. Here is the problem: I know Asp.Net maximum Length for...
3
by: prodirect | last post by:
Hi all, I hope someone can help me. I've recently created a database and wanted to put it up on an ftp sight so that multiple people could access the same tables at the same time from different...
5
by: IkBenHet | last post by:
Hello, I use this script to upload image files to a folder on a IIS6 server: ******************* START UPLOAD.ASPX FILE ********************** <%@ Page Language="VB" Debug="true" %>
8
by: supasnail | last post by:
I have a happily working set of asp pages which read from the database via include file "./_private/include/database.mdb". However, when I try to gain access to this database on pages one folder...
15
by: =?ISO-8859-1?Q?J=F8rn?= Dahl-Stamnes | last post by:
Hello folks, I need some help/advice FAST. I have problems with addslashes on my web-servers. After uploading a file, I read the uploaded file, use addslashes on the read data and then insert...
4
by: Tony B | last post by:
I've moved an existing site (which I didn't write) from a apache/php/mysql host under windows to a linux apache/php/mysql host. I've sorted out most problems except one. There is an upload...
3
by: kujtim | last post by:
i got html code file name html <html> <head> <title></title> </head> <body>
11
by: agarwalsrushti | last post by:
Hi, Ive created a registration page in which at the last it asks the user to upload the resume file. When the user clicks on the upload button to upload the file it automativcally submits all the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.