BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
Posted by Andy Brown on 30 January 2015
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Using SSIS to send emails to a table of people
Another blog arising from a question from a recent course (it's how I learn): how do you send emails to people whose addresses are held in SQL Server?

Suppose that you have created a table or view to hold a number of email addresses, and you want to send the same email to each.
The rest of this blog assumes that you have configured SSIS to allow outgoing emails.
Step 1 - Creating the variables needed
The first thing to do is to create two variables:

The two variables we'll need.
Here's what these will be used for:
Variable | Use |
---|---|
ViewRows | This will hold the rows from the table or view |
EmailAddress | This will hold each individual email address |
Step 2 - read the rows into the object variable
The next step is to read the rows into your ViewRows variable. First add an Execute SQL task:

Add a task to read in the rows.
Now edit this task to store the results:

The task selects one column from our view, and puts the data returned into a results set.
Now use the Result Set tab to specify that you'll store the first (and only) results set in our object variable:

You'll need first to click on the Add button to add a results set into the grid.
Step 3 - looping over each row
You now need to add a FOREACH loop to loop over each row:

This task will run once for each row in the results set.
Edit this task to loop over the ADO rows stored in the ViewRows variable:

These settings are in the Collection tab for the FOREACH loop.
On the Variable Mappings tab, choose to store the first (and as it happens, only) bit of information for each row in the EmailAddress variable:

The SQL statement returned only one column - we capture the value of this in the string variable each time round the loop.
Step 4 - sending the email
Having done the hard part, we can now send each email. First add a Send Mail task within the loop:

This task will send an email for each pass through the loop.
What we'd like to do now is put the variable name in as the To field for the email, but SSIS won't let us do this:

The To property can't take an expression.
What you can do though is to add an expression:

Use this tab to add an expression.
Choose to add an expression:

Click on this button to the right of the dialog box to create a new expression.
You can now assign the variable to the To field:

Success! We've assigned each email address to the To line of each email.
Run your package - but note that Wise Owl do not condone using this knowledge to send out spam ... !
My send mail task keeps telling me "No recipient is specified". Any advice please?
No idea, I'm afraid. What I'd probably do is to right-click on the Send Email task and choose to edit its breakpoints. Put ones on for pre- and post-execute, and then use the Debug menu to display the Locals window. You can then check that the value of your variables are being correctly set.