Home All Groups Group Topic Archive Search About

File busy after sent via email

Author
3 May 2007 8:14 PM
John
Hi

I am emailing a text file as attachment via the below code. The problem is
that after the file has been emailed, it becomes locked and can not be
accessed by further code such as IO.File.AppendAllText(FileName, Content)
which gives the error 'file is in use by another process' or something
similar. How can I release the file after it has been emailed or email it in
a way that it does not get locked?

Many Thanks

Regards


= Code Below ===========================

Dim Msg As String
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New
MailAddress(strTo))

MailMsg.BodyEncoding = System.Text.Encoding.Default
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True
MailMsg.Attachments.Add(New System.Net.Mail.Attachment(Attachment))

Dim SmtpMail As New SmtpClient

SmtpMail.Host = My.Settings.SMTPServer
SmtpMail.Port = 25
SmtpMail.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
SmtpMail.Send(MailMsg)
SmtpMail = Nothing
MailMsg = Nothing

Author
3 May 2007 8:33 PM
Smokey Grindle
freaky look at the post right above yours, i just asked the same thing!

Show quoteHide quote
"John" <John@nospam.infovis.co.uk> wrote in message
news:u1ROc%23bjHHA.1624@TK2MSFTNGP02.phx.gbl...
> Hi
>
> I am emailing a text file as attachment via the below code. The problem is
> that after the file has been emailed, it becomes locked and can not be
> accessed by further code such as IO.File.AppendAllText(FileName, Content)
> which gives the error 'file is in use by another process' or something
> similar. How can I release the file after it has been emailed or email it
> in a way that it does not get locked?
>
> Many Thanks
>
> Regards
>
>
> = Code Below ===========================
>
> Dim Msg As String
> Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New
> MailAddress(strTo))
>
> MailMsg.BodyEncoding = System.Text.Encoding.Default
> MailMsg.Subject = strSubject.Trim()
> MailMsg.Body = strMessage.Trim() & vbCrLf
> MailMsg.Priority = MailPriority.High
> MailMsg.IsBodyHtml = True
> MailMsg.Attachments.Add(New System.Net.Mail.Attachment(Attachment))
>
> Dim SmtpMail As New SmtpClient
>
> SmtpMail.Host = My.Settings.SMTPServer
> SmtpMail.Port = 25
> SmtpMail.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
> SmtpMail.Send(MailMsg)
> SmtpMail = Nothing
> MailMsg = Nothing
>
Author
4 May 2007 9:01 AM
Andrew Morton
John wrote:
> I am emailing a text file as attachment via the below code. The
> problem is that after the file has been emailed, it becomes locked
> and can not be accessed by further code such as
> IO.File.AppendAllText(FileName, Content) which gives the error 'file
> is in use by another process' or something similar. How can I release
> the file after it has been emailed or email it in a way that it does
> not get locked?

If it's any help, you can find which process has the lock on the file by
using unlocker from
http://ccollomb.free.fr/unlocker/

Andrew
Author
5 May 2007 2:50 AM
Smokey Grindel
that really doesnt answer or question on why the attachment process in
net.mail locks files and doesnt release the lock after attaching and sending

Show quoteHide quote
"Andrew Morton" <a**@in-press.co.uk.invalid> wrote in message
news:u0oo2rijHHA.1388@TK2MSFTNGP05.phx.gbl...
> John wrote:
>> I am emailing a text file as attachment via the below code. The
>> problem is that after the file has been emailed, it becomes locked
>> and can not be accessed by further code such as
>> IO.File.AppendAllText(FileName, Content) which gives the error 'file
>> is in use by another process' or something similar. How can I release
>> the file after it has been emailed or email it in a way that it does
>> not get locked?
>
> If it's any help, you can find which process has the lock on the file by
> using unlocker from
> http://ccollomb.free.fr/unlocker/
>
> Andrew
>
Author
8 May 2007 7:29 AM
Andrew Morton
Smokey Grindel wrote:
> that really doesnt answer or question on why the attachment process in
> net.mail locks files and doesnt release the lock after attaching and
> sending

Ah, but is it the OP's program or the OS which has the lock on the file?

Andrew
Author
8 May 2007 8:52 AM
John
Appears that its the program. As it only happens after program sends the
email and not before.

Regards

Show quoteHide quote
"Andrew Morton" <a**@in-press.co.uk.invalid> wrote in message
news:O6cq7LUkHHA.3816@TK2MSFTNGP02.phx.gbl...
> Smokey Grindel wrote:
>> that really doesnt answer or question on why the attachment process in
>> net.mail locks files and doesnt release the lock after attaching and
>> sending
>
> Ah, but is it the OP's program or the OS which has the lock on the file?
>
> Andrew
>
Author
9 May 2007 1:58 AM
jacky kwok
John wrote:
Show quoteHide quote
> Appears that its the program. As it only happens after program sends the
> email and not before.
>
> Regards
>
> "Andrew Morton" <a**@in-press.co.uk.invalid> wrote in message
> news:O6cq7LUkHHA.3816@TK2MSFTNGP02.phx.gbl...
>> Smokey Grindel wrote:
>>> that really doesnt answer or question on why the attachment process in
>>> net.mail locks files and doesnt release the lock after attaching and
>>> sending
>> Ah, but is it the OP's program or the OS which has the lock on the file?
>>
>> Andrew
>>
>
>


Call "System.Net.Mail.Attachment.Dispose()" after send mail.
The "Attachment" object will lock the file until "Dispose".
If you do not call "Dispose()" explicitly, the file just will be
unlocked after garbage collection.

--
Jacky Kwok
jacky@alumni_DOT_cuhk_DOT_edu_DOT_hk