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

时间: 2024-11-13 04:21:55

using SSIS script task to send email result的相关文章

转 Refresh Excel Pivot Tables Automatically Using SSIS Script Task

Refresh Excel Pivot Tables Automatically Using SSIS Script Task https://www.mssqltips.com/sqlservertip/5946/refresh-excel-pivot-tables-automatically-using-ssis-script-task/ 原文地址:https://www.cnblogs.com/Tonyyang/p/10468749.html

SSIS -&gt;&gt; Script Task

利用Script Task,我们可以做一些本身SSIS没能满足我们的,或者实现起来效果不够理想的.比如说我们想做一件这样的事情,去检查某个文件是否为空.如果我们通过Row Count组件来实现,性能上不理想,因为我又并不需要要知道究竟文件包含多少行数据.我们只需要简单地知道文件是否包含数据.我们可以通过写C#代码,去调用BinaryStream的方法来读取该文件的前几行就可以确定是否文件包含数据.总的来说,Script Task能做事情分几类:1) 读取和改变包的变量:2)读取包的属性:3)用C

微软BI 之SSIS 系列 - 使用 Script Task 访问非 Windows 验证下的 SMTP 服务器发送邮件

开篇介绍 大多数情况下我们的 SSIS 包都会配置在 SQL Agent Job 中周期性的按计划执行,比如每天晚上调用 SSIS 包刷新数据,处理 Cube 等.一旦 SSIS 包中出现任何异常,报错,那么配置在 SQL Agent Job 中的通知,邮件提醒就会把这些错误信息发邮件到指定的用户或者系统维护者,这样就起到了一个错误监控的作用. 但是在有的情况下,有一些自定义的 SSIS 调度框架的计划调度都不是通过 SQL Agent Job 配置来完成的.比如我以前在一个小项目中设计过一个

[转]SSIS Execute SQL Task : Mapping Parameters And Result Sets

本文转自:http://www.programmersedge.com/post/2013/03/05/ssis-execute-sql-task-mapping-parameters-and-result-sets.aspx#.U18_6PmSxBk A very common scenario in an ETL process is one in which you need to call out to some configuration tables to figure out so

微软 BI ssis 中的 script task中用代码直接调用 WCF / Webservice

背景: 在普通的C#项目中,可以直接调用 WCF / Webservice: 在微软BI的 ssis中,有 webservice任务组件,也可以直接调用简单的 WCF / Webservice; 偶这边的情况是,后端的 WCF中用的反射,所以在 ssis中的webservice任务组件中,死活不能识别 WCF对应的方法. 所以只能在ssis的 script task组件中,用纯代码的方式来调用WCF服务. 就这样,可能还是有问题:可能会碰到 不能识别 System.ServiceModel节点的

ssis的script task作业失败(调用外部dll)

原文 ssis的script task作业失败 我的ssis作业包里用了一个script task,会查询一个http的页面接口,获取json数据后解析然后做后续处理,其中解析json引用了本地目录下的第三方的类库:Newtonsoft.Json.dll 在vs环境中调试包的时候报错 ? 1 2 3 4 5  在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Bo

Try to write a script to send e-mail but failed

#-*-coding: utf-8 -*- '''使用Python去发送邮件但是不成功,运行后,等待一段时间, 返回[Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to res

Script Task 引用 package variable

Script Task 能够使用C#代码进行编程,许多复杂的逻辑都可以使用C# 脚本来实现,不仅灵活,而且强大. 1,能够传递package variable 给 Script Task ,并且Script Task 能够将Variable更新后返回给package. 示例中新建了两个Variables,VarCode和VarName,分别传递给Script Task. 2,在脚本中,SSIS提供两种方式访问变量,第一种方式比较简单,推荐使用. //读写变量 第一种方式 string VarNa

Check SMTP Server Availability for ORA-29278 or ORA-29279 errors using UTL_SMTP to Send Email

Check SMTP Server Availability for ORA-29278 or ORA-29279 errors using UTL_SMTP to Send Email. (文档 ID 604763.1) 转到底部 修改时间:2014-5-13类型:PROBLEM 为此文档评级 通过电子邮件发送此文档的链接 在新窗口中打开文档 In this Document   Symptoms   Cause   Solution   References APPLIES TO: PL/S