1

I need help with sending PDFs via ASP.NET. I have a web screen where the user enters some info and can, if needed, also navigate through their folders and grab a PDF to send in an email. The PDF is not dynamically created, it will already be on the user's machine (usually on the desktop or in my documents).

I can get to the PDF using the FileUpload command and it attaches and sends just fine. When someone tries to open the PDF, however, they get the message that it could not be opened because it is not a supported file type or is damaged and suggesting that it wasn't correctly decoded.

I know it's not an unsupported file type or damaged PDF issue--it's the decoding--but I can't seem to find anywhere that talks about grabbing an existing PDF and sending it via email. I'm sure it's an issue with setting the header info for the PDF or encoding--but I can't find any clear examples of how to grab an existing PDF and send it and get all the headers or content type or byte stuff correct (I'm not addressing any of these at the moment--I know my problem is there somewhere, but I don't know where to start so I have no code that deals with any of these things).

What I have is:

Dim email As New MailMessage

......set up body and subject and sender/receiver info

If FileUpload1.HasFile Then

Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim memStream As New MemoryStream(UTF32Encoding.Default.GetBytes(FileName))
memStream.Position = 0
Dim attachFile As New Attachment(memStream, FileName)
email.Attachments.Add(attachFile)
End If

I'm not even sure this is the right starting approach, but it took me forever just to get to here, before all this it was never able to find the attachment in FileUpload1. Now I can at least get it and send it, but it can't be opened by anyone. If there's a better approach from start to finish I'm game for it.

This is an intranet/internal web page, using our internal network email (outlook) to send to and from internal recipients only.

Any thoughts would be appreciated

1 Answer 1

0

Yes, it looks as though too many cooks contributing to my original code made it messier than it had to be. Throwing out all the stuff between the if/end if and replacing with

Dim strFileName as String = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim attachment as New Attachment(FileUpload1.PostedFile.InputStream, strFileName)
email.Attachments.Add(attachment)

took care of everything--and was much simpler than all the sites made it look like it could be Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.