ASP.NET chart not being embedded in email

David C
David C used Ask the Experts™
on
Hi Experts,

I am using the code below to capture a pages HTML and send it in an email. The issue is I have introduced a ASP.NET chart but its not being sent in the email? Is there anything I need to do to the chart?

 Dim renderedOutput As StringBuilder
        Dim strWriter As StringWriter
        Dim tWriter As HtmlTextWriter
        Dim html As String

        'create a HtmlTextWriter to use for rendering the page
        renderedOutput = New StringBuilder
        strWriter = New StringWriter(renderedOutput)
        tWriter = New HtmlTextWriter(strWriter)
        'render the page output
        Page.RenderControl(tWriter)
        html = renderedOutput.ToString

        mail.Body = html

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Are you using javascript (or jQuery) to create the chart? You can only send simple HTML in an e-mail.

You could try inserting the chart through an iFrame maybe.
David CIT Projects Manager

Author

Commented:
The chart is ASP.NET object that I just dragged from the toolbox and bound some data to it

Commented:
Can you check the source of the result in a webpage? It will probably show javascript code to generate the chart.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

David CIT Projects Manager

Author

Commented:
Thanks ingriT this is the URL
ChartImg.axd?i=chart_bb15068bc57f4be4b7e30d067ad0f705_0.png&g=eff92e323abd42879b4f7d1f7e618046

Open in new window


I am only assuming but I think this will be different everytime it runs. How would I access the generated path during runtime that way I can embedd that URL in the email and see if it opens

Commented:
You could also create a webpage (ie. Chart.aspx) that only shows this chart, and that way, you can always link it to this page, and not the chart inside the page.
David CIT Projects Manager

Author

Commented:
That sounds like a great idea. How would I link the page? iFrame? or is there a better way
IT Projects Manager
Commented:
Found a cleaner solution as follows;

Dim mail As New MailMessage()

mail.From = New MailAddress(from_address)
mail.To.Add(to_address)
mail.IsBodyHtml = True

Dim renderedOutput As StringBuilder
Dim strWriter As StringWriter
Dim tWriter As HtmlTextWriter
Dim html As String

'create a HtmlTextWriter to use for rendering the page
renderedOutput = New StringBuilder
strWriter = New StringWriter(renderedOutput)
tWriter = New HtmlTextWriter(strWriter)

'render the page output
Page.RenderControl(tWriter)
html = renderedOutput.ToString

Dim item As New Attachment(Server.MapPath("Chart1.png"))
item.ContentDisposition.Inline = True
item.ContentDisposition.DispositionType = DispositionTypeNames.Inline
item.ContentId = "IMAGE_ID"
item.ContentType.MediaType = "image/png"
item.ContentType.Name = "Chart1.png"

mail.Attachments.Add(item)

mail.Body = html

mail.Subject = mail_subject
Dim smtpClient As New SmtpClient(smtp_server)

smtpClient.Send(mail)

Open in new window


Had to change the ImageLocation to Chart1 and ImageStorageMode to UseImageLocation

I also used this to save the image as a file

 Dim imagestream As New System.IO.MemoryStream()
Chart1.SaveImage(imagestream, System.Web.UI.DataVisualization.Charting.ChartImageFormat.Png)
Dim imageByte As Byte() = imagestream.ToArray()

Open in new window

Commented:
Yes, an inline image is probably your best option, good luck!
David CIT Projects Manager

Author

Commented:
Found a cleaner solution to embed the chart image by saving the image first then embedding the image to the email.

Also added <td><img src="cid:IMAGE_ID"></td> to the source to location of chart.

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