Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am attempting to include a .pdf document as an email attachment. The following is the code that I am using:

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.

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Posted

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?

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.

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

  • 4 months later...
Posted

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

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...