The SSIS Script Task provides an option to perform functions that are not available in the SSIS toolbox (both in built-in Tasks and transformations). The SSIS script task uses the VSTA (Microsoft Visual Studio Tools for Applications) as the code environment in which you can write the C# or VB Script.
TIP: VSTA provides all the standard futures available in general Visual Studio Environment.
SSIS Script Task Send Email
Drag and drop the Script Task from SSIS toolbox into the Control Flow region
Before we start configuring the SSIS Script task, let us create four variables. In order to create a variable, right-click on the control flow region, and it will open the context menu to select the variable option. Once you select the variable option, it will open the new window called Variables to create the new variable.
Here you can create n variables
- EmailFrom: Please specify the Email ID from where you want to send the Email. Here, we want to send from [email protected]
- EmailTo: Please specify the Email ID to whom you want to send the Email. Here, we want to send to [email protected]
- EmailSubject: Please specify the Subject you want to include
- EmailBody: Please specify the Message. This can be Plain Text or HTML Message
Double click on the Script task will open the following editor to configure the SSIS Script task components
- EntryPoint: Please specify the Method name that the SSIS run-time calls as the entry point. Method name you specify here must be in ScriptMain class. Remember, you can change the name as per your requirement, and when you change it you have to change it in ScriptMain class
SSIS Script Task General tab
Please click on the General tab to change the default Name and Description.
- Name: Please provide the Unique Name
- Description: Briefly describe the Task Functionality. It is always a good practice to provide the valid description.
ScriptLanguage: Microsoft provides two of its famous languages: Visual Basic (VB), and C# to use as a scripting language. I am very much familiar with C# so, I am selecting C# as my script language.
- ReadOnlyVariables: Please select the variables that you want to use in Script Task, and they may be user-defined variables or System default variables. Remember, variables that are selected as ReadOnlyVariables are used for the Read-only purpose (we can’t alter them)
- ReadWriteVariables: Please select the variables that you want to use in Script Task, and they may be user-defined variables or System default variables. Remember, variables that are selected as ReadWriteVariables can be altered according to our requirement
In order to select the variables, please click on the Eclipse (…) button ReadOnlyVariables option as shown below
Once you click on the Eclipse (…) button, Select the Variables window will be opened as shown below. Please select the previously created variables
Add C# code in SSIS Script Task
Once you selected the required variable, please click on the Edit Script.. button to write the actual C# Script
Once you click on the Edit Script, it will open the ScriptMain.cs class file to write the C# code.
In order to send an email from C# we need to add two references or import references called: using System.Net; and using System.Net.Mail;
TIP: If you are VB developer then it should be something like ScriptMain.vb
Next, within the Main() function add your Code. Remember, if your code is long or robust, try to divide the code by creating methods
C# code we used for this SSIS Script Task in the above screenshot is:
// C# Script for Script Task in SSIS String SendMailFrom = Dts.Variables["EmailFrom"].Value.ToString(); String SendMailTo = Dts.Variables["EmailTo"].Value.ToString(); String SendMailSubject = Dts.Variables["EmailSubject"].Value.ToString(); String SendMailBody = Dts.Variables["EmailBody"].Value.ToString(); try { MailMessage email = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); // START email.From = new MailAddress(SendMailFrom); email.To.Add(SendMailTo); email.Subject = SendMailSubject; email.Body = SendMailBody; //END SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential(SendMailFrom, "your password"); SmtpServer.EnableSsl = true; SmtpServer.Send(email); MessageBox.Show("Email was Successfully Sent "); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } Dts.TaskResult = (int)ScriptResults.Success;
If you forgot to create Variables (or if you find difficult to create variables) then, you can remove the first four lines of code, and replace the code between //Start… END with the below-shown code
// SSIS Script Task Example email.From = new MailAddress("[email protected]"); email.To.Add("[email protected]"); email.Subject = "Test Mail"; email.Body = "This Email is coming from SSIS Script Task";
Once you finished editing the Script, Please close the ScriptMain.cs file and Script Task Editor. Let us run the SSIS Script Task package
From the above screenshot, you can observe that Task is successful and we got the Message box saying that, Email was Successfully sent. Let me open my Gmail
From the above screenshot, you can see that, we got the Email with the message we specified.
Points to Remember for this SSIS Script Task :
- If you want to send an Email to your Manager (or to yourself) after every Data Load then, you can use this SSIS Script task along with Data Flow Task.
- Always use the Variables for storing Emails, Password, Subject, or SMTP credentials.
- If there is an Error stating Secure Connection then, Go To Less Secure Apps in your Gmail account settings and Turn on the Access for Less Secure Apps option.
Thank You for Visiting Our Blog.