Email charts attached and embedded from memory


QUESTION

Can a chart image be sent via email directly from memory by embedding in an HTML email or sending as an attachment?

ANSWER

Yes, using a third party email component such as www.aspnetemail.com this can be easily accomplished.  The following C# samples demonstrate both attaching and embedding charts directly from memory without saving the chart to disk. 

To use either of these samples save to an aspx page such as email.aspx.  You will also need to download AspNetEmail:

1) Download the aspNetEmail.msi from www.advancedintellect.com/download.aspx
as a zipped archive. The zip archive also contains a xml license file named
aspNetEmail.xml.lic.
2) Install the aspNetEmail.msi.
3) Locate the aspNetEmail.dll and copy it to the /bin with dotnetcharting.dll
4) Copy the aspNetEmail.xml.lic license file in your /bin directory. This
license file will expire in 21 days. If you need to extend your trial
evaluation, simply return to
www.advancedintellect.com/download.aspx and
download another license file.

[C#]  This sample shows attachment from memory

<%@ 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 = "Attached Chart";
   mail.To ="toaddress@domain.com";
   mail.FromAddress = "fromaddress@domain.com;
   mail.Body = "Please see the attached 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 random 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 the 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();

   //attach chart
   Attachment att = new Attachment( imageBytes , "chart1.png" );
   mail.AddAttachment( att );

   mail.Send();

   Response.Write( "Your message has been sent to "+ mail.To ); 
  
  }

</script>


<html>
  <head>
    <title>.netCHARTING Chart Email Sample</title>
  </head>
  <body MS_POSITIONING="GridLayout">
 
    <form id="dnChartEmail" method="post" runat="server">

     </form>
 
  </body>
</html>

 

[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>

 


Posted Monday, August 8, 2005 by Support
http://www.dotnetcharting.com/kb/KnowledgebaseArticle11834.aspx