Embedding Excel Chart with Outlook message


Embedding Excel Chart with Outlook message.

 

In this article I will explain How to send excel chart with the outlook message.

 

ð  Step 1(Convert Excel Chart as an Image).

Please refer my previous article to "Convert Excel Chart as an Image"

 ( http://www.c-sharpcorner.com/UploadFile/vivekbritish/4553/?ArticleID=8b2e0376-884c-4b14-9b48-7c61d911af55 ).  

 

ð  Step 2 (Function for sending Excel Chart with message.)

 

   public void SendXLChart(string strFilePath)

        {

            xl.Application xlApp = null;

            xl.Workbook xlWorkBook = null;

            xl.Worksheet xlWorkSheet;

            try

            {

                //Extracting excel chart from the Excel Sheet.

                object misValue = System.Reflection.Missing.Value;

                xlApp = new xl.ApplicationClass();

                xlWorkBook = xlApp.Workbooks.Open(strFilePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

                xlWorkSheet = (xl.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                xl.ChartObjects xlCharts = (xl.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);

                xl.ChartObject myChart = (xl.ChartObject)xlCharts.Item(1);

                xl.Chart chartPage = myChart.Chart;

                chartPage.Export(@"c:\chartImage\chart.jpeg", "JPEG", misValue);

                releaseObject(xlWorkSheet); // Releasing Excel Object.

 

                // Creating message body

                string strMsgBody;

                strMsgBody = @"<html><head></head><body><div>My Message</div>";

                strMsgBody += "<div><img src='cid:xlChart'/></div>";//ccid:xlChart is the id of content resource.

                strMsgBody += "</body></html>";

 

                // Creating mail message.

                MailMessage msg = new MailMessage();

                msg.From = new MailAddress("[email protected]", "ABC");

                msg.To.Add(new MailAddress("[email protected]", "ABC"));

                msg.Subject = "Sending Chart ";

                msg.Body = strMsgBody;

 

                // Embedding Excel Chart with message.

                System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/html");

                AlternateView av1 = AlternateView.CreateAlternateViewFromString(msg.Body.ToString(), ct);

                LinkedResource imgChart = new LinkedResource(@"c:\chartImage\Chart.jpeg", System.Net.Mime.MediaTypeNames.Image.Jpeg);

                imgChart.ContentId = "xlChart";

                av1.LinkedResources.Add(imgChart);

                msg.AlternateViews.Add(av1);

 

                // Sending Message

                System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();

                sc.Host = "smtp server";

                sc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

                sc.Send(msg);

                av1.Dispose();

                msg = null;

            }

            catch (Exception ex)

            {

                throw new Exception("SendxlChart:- " + ex.Message);

            }

            finally

            {

                if (xlWorkBook != null)

                {

                    xlWorkBook.Close(true, null, null);

                    xlApp.Quit();

                }

                releaseObject(xlWorkBook);

                releaseObject(xlApp);

            }

        }

 

ð  Function to relaese com object

 

        private void releaseObject(object obj)

        {

            try

            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);

                obj = null;

            }

            catch (Exception ex)

            {

                obj = null;

            }

            finally

            {

                GC.Collect();

            }

        }

 

Thanks for reading article..
 

Up Next
    Ebook Download
    View all
    Learn
    View all