I have a result set from an Execute SQL Task
query saved on a System.Object variable and i would like to send this results using a Send Mail Task
using the following expression on the MessageSource.
"Please find attached data summary\n\n" + SUBSTRING( @[User::myVariable] ,1,3990)
+ "\n\n"
After many attempts this method actually works. However, if you have Visual Studio 2010 or higher the C# script needs to be changed ever so slightly.
Replace:
with this:
Still trying to sort out the spacing in the email I am getting but quite happy with the results.
Probably, you have found an answer to your question by now. This answer is for others who might stumble upon this question. I don't think you can use object variable in expression. You need to loop through the query result object and format it to a string so that you can send the query output in an e-mail message. You can also export the data to a file and send the file as an attachment. That is another possible option. This example shows how to loop through the query result set to form the message body that will then be emailed using Send Email task.
Step-by-step process:
Create a table named
dbo.EmailData
using the script provided under SQL Scripts section.Screenshot #1 shows sample data that
Execute SQL
task will query and send it in an e-mail in this example.On the SSIS package, create 5 variables as shown in screenshot #2.
On the SSIS package, place the following tasks:
Execute SQL task
,Foreach loop container
,Script task
within the Foreach loop container andSend Email task
.Configure the
Execute SQL task
as shown in screenshots #3 and #4.Configure the
Foreach loop container
as shown in screenshots #5 and #6. Variable mappings section shows the order in which the query result columns appear and how they are assigned to SSIS variables. These variables will be used to form the email message inside theScript task
.In the
Script task
, replace the code with the one shown under the Script task code section. The script task has very simple plain text email message formatting.Configure the Send Email task as shown in screenshot #7. You need to configure it with valid email address in From and To fields.
After configuring the Control flow tasks, your package should look like as shown in screenshot #8.
Sample package execution is shown in screenshot #9.
E-mail sent by the package is shown in screenshot #10. Some information have been removed from the screenshot. You can compare the table data shown in screenshot #1 with this email output and they should same.
Hope that helps.
SQL Scripts: .
Script task code:
C# code that can be used only in
SSIS 2008 and above
. .Screenshot #1:
Screenshot #2:
Screenshot #3:
Screenshot #4:
Screenshot #5:
Screenshot #6:
Screenshot #7:
Screenshot #8:
Screenshot #9:
Screenshot #10: