1

I need to send email report from c# code. This email contain graphic like the image. is there any way to make that in c#? dygraph graphic must be included in the email

2 Answers2

1

You can use the MailMessage class to construct the email, and the SmtpClient class to send the message. You'll need to set up the SmtpClient appropriately*.

In order to embed an image, you need to set the email to be HTML, and include an alternate view with the embedded image:

var mail = new MailMessage();
mail.IsBodyHtml = true;

var inline = new LinkedResource(@"C:\path\to\your\image.png");
inline.ContentId = Guid.NewGuid().ToString();
var htmlBody = @"<img src='cid:" + inline.ContentId + @"'/>"; // Include whatever other content in the html body here.
var alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(inline);

mail.AlternateViews.Add(alternateView);

Then set the to/from/subject fields and use the SmtpClient.Send Method (or SendAsync) to send the email.

*From the MSDN docs:

To construct and send an e-mail message by using SmtpClient, you must specify the following information:

  • The SMTP host server that you use to send e-mail. See the Host and Port properties.

  • Credentials for authentication, if required by the SMTP server. See the Credentials property.

  • The e-mail address of the sender. See the Send and SendAsync methods that take a from parameter. Also see the MailMessage.From property.

  • The e-mail address or addresses of the recipients. See the Send and SendAsync methods that take a recipient parameter. Also see the MailMessage.To property.

  • The message content. See the Send and SendAsync methods that take a body parameter. Also see the MailMessage.Body property.

merthsoft
  • 878
  • 1
  • 7
  • 10
  • I need to generate the image(graphics, chart), i mean. the problem is how generate the charts and sent by email. i understan that i can send images. but how generate the chart and make it an image.. I have a background worker that runs 24/7 on a number of servers and send a email; before send the email i must generate a chart with DB data and this must be included in the email. – user3046993 May 10 '16 at 16:51
  • Oh, I get it. Do you have something that's generating these charts already? If so, you can use an HttpClient or something to get the image, and then do a Base64 encoding or something like in Ollie's answer. – merthsoft May 10 '16 at 17:58
0

Dotnet contains a MailMessage class with an IsBodyHtml property. You can construct your email message using a text string. Into the text string, write HTML with an <img ..> tag pointing to the graphic.

You can insert the datastream for the image directly into the image tag using the data: URI scheme, with tags that don't refer to external images. That looks something like this.

img src="data:image/png;base64,iVBORw0KG==" />

You can do this by following the advice here. Converting image into data:image/png;base64 for web page disaplay

Community
  • 1
  • 1
O. Jones
  • 103,626
  • 17
  • 118
  • 172