[C#] This sample shows embedding from memory in an HTML email.
<%@ Page language="c#" Description="Email dotnetChart" %> <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="aspNetEmail" %> <%@ Import Namespace="dotnetCHARTING" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<script runat=server> private void Page_Load(object sender, System.EventArgs e) { EmailMessage mail = new EmailMessage(); //set the mail server properties mail.Server = "mail.yourserver.com"; mail.Subject = "Embedded Chart"; mail.To = "toaddress@domain.com"; mail.FromAddress = "fromaddress@domain.com"; mail.Body = "Please see the embedded chart for more information."; //you may also need to set a username and password to send email //mail.Username ="username"; //mail.Password = "pass";
//create the chart dotnetCHARTING.Chart Chart = new dotnetCHARTING.Chart(); //Create some randon data to show in the chart SeriesCollection SC = new SeriesCollection(); Random myR = new Random(); for(int a = 0; a < 4; a++) { Series s = new Series(); s.Name = "Series " + a; for(int b = 0; b < 5; b++) { Element el = new Element(); el.Name = "E " + b; el.YValue = myR.Next(50); s.Elements.Add(el); } SC.Add(s); }
// Set Different Colors for our Series SC[0].DefaultElement.Color = Color.FromArgb(49,255,49); SC[1].DefaultElement.Color = Color.FromArgb(255,255,0); SC[2].DefaultElement.Color = Color.FromArgb(255,99,49); SC[3].DefaultElement.Color = Color.FromArgb(0,156,255);
// Set the title. Chart.Title="This chart was sent to " + mail.To; // Change the shading mode Chart.ShadingEffectMode = ShadingEffectMode.Two;
// Set the x axis label Chart.ChartArea.XAxis.Label.Text="X Axis Label";
// Set the y axis label Chart.ChartArea.YAxis.Label.Text="Y Axis Label";
// Set the directory where the images will be stored. Chart.TempDirectory="temp";
// Set he chart size. Chart.Width = 600; Chart.Height = 350;
// Add the random data. Chart.SeriesCollection.Add(SC);
//get the chart image bitmap Bitmap bmp = Chart.GetChartBitmap(); //Convert the bitmap image to an array of bytes suitable for attachment from memory MemoryStream imageStream = new MemoryStream(); bmp.Save(imageStream,System.Drawing.Imaging.ImageFormat.Png); long len = imageStream.Length; Byte [] imageBytes = new Byte[len+1]; imageStream.Position = 0; int totalRead = imageStream.Read(imageBytes,0,(int)len); imageStream.Close();
//Embed chart in email message
//assign the content-id value of 'chart1'. This can any alphanumeric string. string html = "Here is the embedded chart<br><img src=\"cid:chart1\">"; mail.HtmlBodyPart = html;
//embed chart1 EmbeddedObject eo = new EmbeddedObject( "chart1.png", imageBytes, null, "image/png" ); eo.ContentID = "chart1";
mail.EmbedObject( eo );
mail.Send();
Response.Write( "Your message has been sent to "+ mail.To ); }
</script>
<html> <head> <title>.netCHARTING Chart Embedded Email Sample</title> </head> <body MS_POSITIONING="GridLayout"> <form id="dnChartEmail" method="post" runat="server">
</form> </body> </html> |