1

What I want to do is render a SSRS 2008 report as HTML (actually MHTML) and send that as the body in an email.

Has anyone done this? I've almost got it (I think) but I seem to have a encoding problem. The email shows up as what looks like some sort of base64 encoding.

Like this: MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_NextPart_01C35DB7.4B204430" X-MSSQLRS-ProducerVersion: V10.0.2531.0 This is a multi-part message in MIME format. ------=_NextPart_01C35DB7.4B204430 Content-ID: Content-Disposition: inline; filename="FollowUpNotification" Content-Type: text/html; name="FollowUpNotification"; charset="utf-8" Content-Transfer-Encoding: base64 PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMDEgVHJhbnNp...

Any ideas on what I'm missing?

Here is a code snippet:

myMail.BodyEncoding = System.Text.Encoding.UTF8;

myMail.IsBodyHtml = true;

WebClient client = new WebClient();

System.Net.NetworkCredential repAuthenticationInfo = 
    new System.Net.NetworkCredential(@"username", @"password");
client.Credentials = repAuthenticationInfo;

client.Encoding = System.Text.Encoding.UTF8;

string messageBody = client.DownloadString(
    "http://<<reportserver>>&rs%3aFormat=MHTML&Parameter=" + Parameter);

myMail.Body = messageBody;
Mozy
  • 1,326
  • 1
  • 10
  • 10
  • Check the SQL Server version - you could use the email mechanism in Subscriptions rather than code the emailing yourself. – OMG Ponies May 07 '10 at 17:57
  • I need the report to be the body of the email. As far as I know that can't be done with subscriptions. – Mozy May 07 '10 at 21:13
  • Just a little update... My problem is MHTML (M for MIME) I need a way to convert MHTML to HTML or force the email to handle MHTML. – Mozy May 07 '10 at 21:19

2 Answers2

3

System.Net.Mail doesn't support sending MHTML email where the MHTML appears as the message (see this page where someone is attempting to send MHTML as email).

The reason you are seeing the code is that messageBody is expecting HTML, while the code is feeding it an MHTML document. The string is not automatically unencoded.

Here are some options:

  1. Change the format parameter from MHTML to HTML3.2 or HTML4.0. SQL Reporting Services will then give you RAW HTML.
  2. Send the MHTML as an attachment rather than inline.
  3. Use SSRS's built in email Subscriptions feature as OMG Ponies suggested in the above comment.
  4. Put the MHTML inline as discussed in this question. You'll likely want to buy a library to help with this.
Community
  • 1
  • 1
Nate
  • 693
  • 6
  • 8
  • You're answer pointed me in the right direction. Turns out I needed to use ReportExecution2005.asmx instead of the simple client I was using to get the HTML parameters to work. – Mozy May 11 '10 at 00:09
1

I like Nate's answer and I was able to get things working for HTML 4.0 using the advice below from the link in point # 4 above so here it is as a short cut for anyone who wanders here. A quick note though, I was using SMTP to send the email and the images are dropped since they couldn't link back to my SSRS server.

I don't have a solution for sending .mht content as the body of an email message, but I did figure out how to export a SQL Server 2005 report to plain old HTML format. I too was originally thrown off by the poor documentation of the Render method's "format" parameter. It turns out if you pass in "HTML4.0", SSRS will generate HTML for you.

Credit belongs to Voot quoted above.

Daver
  • 333
  • 2
  • 5
  • 14