File busy after sent via email

J

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
 
A

Andrew Morton

John said:
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
 
S

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
 
A

Andrew Morton

Smokey said:
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
 
J

John

Appears that its the program. As it only happens after program sends the
email and not before.

Regards
 
J

jacky kwok

John said:
Appears that its the program. As it only happens after program sends the
email and not before.

Regards


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top