Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying PDF in email body

Status
Not open for further replies.

brage98

IS-IT--Management
Sep 8, 2010
27
CA
I have a code that kinda does what ssrs-subscription can do. Except, users are asking to view the actual attached-report in the body of their emails. So say I'm attaching a .pdf or .mht file to the email. Users want to be able to see these files right in their email.

I've tried the "AlternativeView" option, and setting contentType to "application/pdf" or "multipart/related". Nothing's working. I get a buncha binary data displayed in the body of my email

Your help is much appreciated!

Here's a sample of the code (due to some internal policies it had to be written in powershell... but powershell, C#, tomayto, tomahto... just different syntax i guess:


Code:
    $smtp = new-object Net.Mail.SmtpClient("MailSerer")
    $msg = new-object Net.Mail.MailMessage

    $msg.From = "fromAddress"
    $msg.To.Add($toAddress)    
    $msg.subject = "test"
    $msg.IsBodyHtml = $true
    $att = new-object Net.Mail.Attachment($fileName)
    $msg.Attachments.Add($att)
    $vu = New-Object System.Net.Mail.AlternateView($fileName)
    $vu.ContentType = New-Object System.Net.Mime.ContentType("multipart/related");
    $lr = New-Object System.Net.Mail.LinkedResource($fileName, "application/pdf")
    $vu.LinkedResources.Add($lr)
    $msg.AlternateViews.Add($vu);
    
    $smtp.Send($msg)
 
Have you seen this feature before, in other emails? I'm not sure it's even possible.

As an alternative you could create an html or text email and attach the PDF.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Hi there,
Thanks for the reply

I'm sure there's a way of doing it.
SSRS does it some how:
When you subscribe to a report, and ask for mhtml format, the email that is sent out to you displays the report right in the content.

Also, in outlook, when inserting a file, you have the option of inserting the file (attaching the file) as text through "InsertAsText" option... and that displays the content of the file (whatever it may be, pdf, excel, etc...) right in the body of the message.

So I'm sure there's a way of doing it.

since my last post, I'm playing around with the header:
$msg.Headers.add("Content-Type", "application/pdf")
$msg.Headers.add("Content-Transfer-Encoding", "binary")

I'm sure I'm missing something here... must be something simple that I'm overlooking


 
then your right, there probably is a way however it may be specific to Outlook/Exchange, not smtp. I've never seen this feature before.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
k, thanks again for the follow up.

As an FYI,
with a "system.Net.Mail.MailMessage" object you can
1. specify the body of the message to be of html type
2. set "headers"

From 1 and 2, and given that system.Net.Mail.MailMessage is used by System.Net.Mail.SmtpClient, I'm almost certain displaying binary data such as .pdf contents in the body of an email-message is not specific to outlook

Thanks again, and feel free to drop a line if you do find a way of doing this.
 
please do the same. I would like to see this in action.

you could try to reverse engineer an existing email message that does this. take a look at the file in a text editor to see how the file is formatted. maybe they are using an iframe and linking to the document somehow?

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I have no solution for this except the following work-around:
The file I was planning to displayin the body of the email is the output of an SSRS report in MHTML format (.mht file).

Since I couldn't find an easy way of translating .mht to .html, I've decided to simply call the ssrs twice to generate two files, one "HTML4.0" and another "PDF" format.

I display the HTML4.0 in the body, and attach the pdf to the email. It's not ideal, but it is what it is i guess :)

 
What you can try is associating a file extension with the program that open it. i.e. an attached .xls file is openned by Excel, etc.

We use a program called Fortis (it's basically a back end to our imaging system). Normally .tif images are stored, but so can most anything else, including .pdf's. Once a pdf 'image" is opened, the user presses a launch button and adobe reader starts and the file is displayed., Same with excel files, word docs, etc.

you might try something like the above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top