Cannot access a closed file & email attachment.

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
I am attempting to include a .pdf document as an email attachment. The following is the code that I am using:
Code:
Dim smtpClient As New SmtpClient
            Dim message As New MailMessage

            SendEmail = True
            Try
                ' Gets the SMTP Email credentials
                If GetEmailAccountDetails(emailSmtp, emailUser, emailPass) = True Then
                    Dim fromAddress As MailAddress = New MailAddress(sender)
                    Dim smtpAuthentication As New System.Net.NetworkCredential(emailUser, emailPass)
                    'Sets the smptclient properties
                    smtpClient.Host = emailSmtp
                    smtpClient.UseDefaultCredentials = False
                    'Sets the smpt port and Authentication
                    smtpClient.Port = 25
                    smtpClient.Credentials = smtpAuthentication

                    'Set Message 
                    message.From = fromAddress
                    message.To.Add(receipient)
                    message.Subject = subject
                    message.IsBodyHtml = True
                    message.Body = messageBody
                    'Sets the Attachement if required
                    If Len(mailAttachment) <> 0 And System.IO.File.Exists(mailAttachment) = True Then
                        Dim fileAttached As New System.Net.Mail.Attachment(mailAttachment)
                        message.Attachments.Add(fileAttached)
                        fileAttached.Dispose()
                    End If
                    If Len(mailAttachment2) <> 0 And System.IO.File.Exists(mailAttachment2) = True Then
                        Dim fileAttached As New System.Net.Mail.Attachment(mailAttachment2)
                        message.Attachments.Add(fileAttached)
                        fileAttached.Dispose()
                    End If
                    'Sends the message
                    smtpClient.Send(message)
                Else
                    Return False
                End If

            Catch ex As Exception
                Return False
            Finally
            End Try

I am passing in the location of the file in the following format: C:\mylog\templates\supportdocument.pdf

However when I try to attach the file I get a System.Net.Mail.Smtpexception that tells me "Failure sending mail" with the inner message being: "Cannot access a closed file"

Any suggestions? I have checked and have the necessary permissions, I'm sure. Sometimes, the code also seems to corrupt the file as when I try to open the file I get the message: "Adobe Reader could not open supportDocument.pdf because it is either not a supported file type or because the file has been damaged (it was sent as an email attachment + wasn't correctly decoded)"

Michael.
 
Yea, I'm disposing the attachment just before exiting the if statement, the question that I need to ask is: am I disposing it correctly?
Code:
 If Len(mailAttachment) <> 0 And System.IO.File.Exists(mailAttachment) = True Then
                        Dim fileAttached As New System.Net.Mail.Attachment(mailAttachment)
                        message.Attachments.Add(fileAttached)
                        fileAttached.Dispose()
                    End If

I have been searching google, but have been unable to find any posting of a similar problem.

Mike55.
 
Visual Basic:
If Len(mailAttachment) <> 0 And System.IO.File.Exists(mailAttachment) = True Then
    Dim fileAttached As New System.Net.Mail.Attachment(mailAttachment)
    message.Attachments.Add(fileAttached)
    fileAttached.Dispose()
End If

If Len(mailAttachment2) <> 0 And System.IO.File.Exists(mailAttachment2) = True Then
    Dim fileAttached As New System.Net.Mail.Attachment(mailAttachment2)
    message.Attachments.Add(fileAttached)
    fileAttached.Dispose()
End If

'Sends the message
smtpClient.Send(message)    'being done AFTER a Dispose
You are attaching the file, disposing of it then sending it - try sending then disposing to see if that fixes the problem.
 
Ok, by relocating the location of the fileattached.dispose() as you suggested I got the code to run. However it resulted in the original file and the attachment becoming corrupted. When I try to open the file I get the following message: "Adobe Reader could not open supportDocument.pdf because it is either not a supported file type or because the file has been damaged (it was sent as an email attachment + wasn't correctly decoded)"

Mike55.
 
I fixed my problem by suppressing the Finalize on the base stream after I opened the file

Here is the Line I added after opening the file
GC.SuppressFinalize(myLogStream.BaseStream);

Then at the class destructor I could Flush and Close the file
 
If you are dealing with something like a streamwriter then it is documented behaviour that you should explicitly close the streamwriter when you have finished writing rather than leaving it for the garbage collector.

Suppressing finalization will simply result in the file remaining locked until the GC kicks in - this could result in an unacceptable wait before the handle is released.
 
Back
Top