mike55 Posted July 6, 2007 Posted July 6, 2007 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. Quote 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 PlausiblyDamp Posted July 6, 2007 Administrators Posted July 6, 2007 Just a quick suggestion but have you tried sending the mail before disposing of the attachment? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mike55 Posted July 6, 2007 Author Posted July 6, 2007 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. Quote 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 PlausiblyDamp Posted July 6, 2007 Administrators Posted July 6, 2007 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mike55 Posted July 6, 2007 Author Posted July 6, 2007 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. Quote 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 PlausiblyDamp Posted July 6, 2007 Administrators Posted July 6, 2007 http://support.microsoft.com/kb/286358 sounds like it could address your problem, should be worth a read. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
wassif Posted November 12, 2007 Posted November 12, 2007 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 Quote
Administrators PlausiblyDamp Posted November 12, 2007 Administrators Posted November 12, 2007 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.