471,336 Members | 938 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 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 2700
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michele Belloli | last post: by
1 post views Thread by Jonathan | last post: by
5 posts views Thread by IkBenHet | last post: by
15 posts views Thread by =?ISO-8859-1?Q?J=F8rn?= Dahl-Stamnes | last post: by
4 posts views Thread by Tony B | last post: by
3 posts views Thread by kujtim | 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.