using SSIS script task to send email result

來源:互聯網
上載者:User

標籤:style   ar   io   color   os   sp   for   on   bs   

sometimes notification email is required to be sent so that receivers can know about the data load status. Following C# code in SSIS script task is composed to meet the requirement.

1. drag a SQL task to get data result and assigned full set to an object variable(e.g.oCompleteFileList)

2. drag a script task to compose HTML message body and send email

 

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Net.Mail;

 

 

 public void Main()
        {
            // TODO: Add your code here

            //Variable list
            //User::ccSeller,User::sEmailFrom,User::nFileCount,User::oCompleteFileList,User::sEmailToList,User::Environment
            string vSeller = Dts.Variables["ccSeller"].Value.ToString();
            string vEmailFrom = Dts.Variables["sEmailFrom"].Value.ToString();
            string vEmailTo = Dts.Variables["sEmailToList"].Value.ToString();
            object vFileList = Dts.Variables["oCompleteFileList"].Value;
            int vFileCount = (int)Dts.Variables["nFileCount"].Value;
            string vEnv = Dts.Variables["Environment"].Value.ToString();

            //setup smtp connection
            SmtpClient vSmtp = new SmtpClient();
            string vSmtpcm = Dts.Connections["SMTP Connection Manager"].ConnectionString.ToString();
            vSmtp.Host = (vSmtpcm.Split(‘;‘)[0]).Split(‘=‘)[1];
            vSmtp.UseDefaultCredentials = true;
            MailMessage vmsg = new MailMessage();
            //send from
            vmsg.From = new MailAddress(vEmailFrom);
            //send to
            Array vToList = vEmailTo.Split(‘;‘);
            foreach (string s in vToList)
            {
                vmsg.To.Add(new MailAddress(s));
            }
            //message subject and message body
            vmsg.IsBodyHtml = true;
            OleDbDataAdapter oleDA = new OleDbDataAdapter();
            DataTable dt = new DataTable();
            string rc = "", msgxml = "", hd = "";
            string newline = Environment.NewLine;
            string blank4 = "    ", blank1 = " ";

            if (vFileCount == 0)
            {
                msgxml = "No files were loaded";
                vmsg.Subject =vEnv + ":" + vSeller + " - No files were loaded.";
            }
            else
            {
                //read sql result
                oleDA.Fill(dt, vFileList);
                //compose table header
                foreach (DataColumn col in dt.Columns)
                {
                    hd = hd + "<th style=‘border:1px solid black‘>" + col.ColumnName + "</th>";
                }
                hd = "<tr style=‘background-color:#4F81BD;color:white‘>" + hd + "</tr>" + newline;
                //compose table content
                foreach (DataRow row in dt.Rows)
                {
                    rc = "";
                    foreach (DataColumn col in dt.Columns)
                    {
                        if (col.Ordinal != dt.Columns.Count - 1)
                        {
                            rc = rc + "<td style=‘border:1px solid‘>" + blank1 + row[col.Ordinal].ToString() + blank4 + "</td>";
                        }
                        else
                        {
                            if (row[col.Ordinal].ToString().ToUpper() == "SUCCESS")
                            {
                                rc = rc + "<td style=‘border:1px solid;background-color:green‘>" + blank1 + row[col.Ordinal].ToString() + blank4 + "</td>";
                            }
                            else
                            {
                                rc = rc + "<td style=‘border:1px solid;background-color:red‘>" + blank1 + row[col.Ordinal].ToString() + blank4 + "</td>";
                            }
                        }
                    }
                    msgxml = msgxml + "<tr>" + rc + "</tr>" + newline;
                }
                //compose final xml
                msgxml = "<table cellspacing=50 style=‘border:1px solid;border-collapse:collapse‘>" + newline + hd + msgxml + "</table>";
                vmsg.Subject = vEnv + ":" + vSeller + " - Detail loaded files list";
            }
            vmsg.Body = msgxml;
            //send email
            vSmtp.Send(vmsg);

            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }

using SSIS script task to send email result

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.