Using SSIS Script Task to send email result

Source: Internet
Author: User
Tags mailmessage smtpclient ssis

Sometimes notification email is required to being sent so, receivers can know about the data load status. Following C # code in SSIS Script task was 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::enviro Nment
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 + "&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.