Adding Attachement to MailMessage leaves attached file open

G

Guest

I have a simple program that picks up a file from a folder and emails it to
an account on our exchange server using SMTP. Below is a copy of my code for
attaching the file to the message and sending it. The email message gets
delivered but I can't delete the file afterwards because it is being used by
another process. I used Sysinternals File Monitor to watch the file and see
where the problem. When I add the file as an attachement to the mail
message, it opens the file and reads the contents but doesn't close the file.
After about 30-40 seconds it will close file, I am assuming the gc took care
of this. Am I doing something wrong or do I just need to work around this?


Public Sub SendEmail(ByVal Filename As String)
Dim smtp As SmtpClient
Dim msg As New MailMessage(Me.EMAILFROM, Me.EMAILTO, "Fax Received -
" & DateTime.Now.ToShortDateString & " " & DateTime.Now.ToShortTimeString, "")
Try
msg.Body = "Fax Received - " & DateTime.Now.ToShortDateString &
" " & DateTime.Now.ToShortTimeString & vbCrLf & Filename & vbCrLf
msg.BodyEncoding = System.Text.Encoding.UTF8
msg.Attachments.Add(New Attachment(Filename))
smtp = New SmtpClient(Me.SMTPSERVER)
smtp.Send(msg)
Finally
msg = Nothing
smtp = Nothing
End Try
End Sub
 

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