zoukankan      html  css  js  c++  java
  • using SSIS script task to send email result

    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;
            }
        }

  • 相关阅读:
    ssh加密访问
    protobuf java学习
    Errors occurred while updating the change sets for SVNStatusSubscriber org.apache.subversion.javahl.
    eclipse导入svn中的maven工程项目
    Archive for required library xx cannot be read or is not a valid ZIP file
    oracle9i 精简版客户端界面没有显示实例名
    mybatis3 sqlsession
    RabbitMQ学习之:(一)初识、概念及心得
    mybatis expected at least 1 bean which qualifies as autowire candidate for this dependency
    mysql 5.6.20 数据库中文乱码解决方法
  • 原文地址:https://www.cnblogs.com/hopecho/p/4167109.html
Copyright © 2011-2022 走看看