Home All Groups Group Topic Archive Search About

Adding Attachement to MailMessage leaves attached file open

Author
29 Oct 2006 12:51 AM
Clay
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

Author
29 Oct 2006 1:00 AM
Bryan Phillips
You need to call Dispose on the MailMessage class so that the underlying
stream is closed.

Bryan Phillips
MCSD, MCDBA, MCSE
Blog:  http://bphillips76.spaces.live.com




Show quote
"Clay" <C***@discussions.microsoft.com> wrote in message
news:6ADE3B23-7E1C-452C-8368-CF6B54B07FCA@microsoft.com:

> 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

AddThis Social Bookmark Button