MS SQL Report Export as PDF to e-mail attachment

sibleypark
sibleypark used Ask the Experts™
on
I am using the following code to try and export a pdf from SQL reports and attach to an e-mail:

                    Dim remoteUrl As String = cAttachment
                    Dim myWebClient As New WebClient()
                    'myWebClient.Credentials = CredentialCache.DefaultCredentials

                    'myWebClient.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
                    myWebClient.Headers.Add("user-agent", "Only a test!")

                    Dim myDatabuffer As Byte() = myWebClient.DownloadData(remoteUrl)

                    Dim ms As New MemoryStream(myDatabuffer)

                    message.Attachments.Add(New Attachment(ms, cAttachmentName))

When I use a physical file it is fine but when I point to
Dim mailReport As String = "http://localhost/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2fprecep_signature_req&Preceptorid=2758&rotation_id=17074&user_id=66&rs:Format=PDF"

I get a 403 security error form the server.  I can type the url in manually and it works fine and I get the pdf save file dialogue option.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Try using My.Computer.Network.DownloadFile method to download the file first and then attach to email.

Author

Commented:
I am not sure what this means
Most Valuable Expert 2012
Top Expert 2014
Commented:
It means

My.Computer.Network.DownloadFile("http://localhost/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2fprecep_signature_req&Preceptorid=2758&rotation_id=17074&user_id=66&rs:Format=PDF", "C:\MyFile.PDF")

message.Attachments.Add(New Attachment("C:\MyFile.PDF"))
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial