In the first part I showed you the basics of how to send one email, now we need to make it go through the list one by one emailing each person, organisation etc.
There's a bit more to it than just looping through a list!
First lets consider the list in more detail, the three critical pieces of information sent in an email are the To (the recipients email address), the Subject and the Body parts, see image
here.
In practice the list is most likely to be supplied to you in a delimited text file with one record per line.
The easiest option is to use the DTS (Data Transfer Services) utility such as the one with SQL Server Enterprise Manager, or an equivalent utility with your preferred database which allows you to import a new database table.
Now we're ready to automate the process of stepping through the list of suspects ( as they are called in marketing speak.
Once you've got the list into your database you're ready to build a more advanced verion of the code used in the Send buttons click event shown in the introduction.
One final step before we dive into some .NET code again is to add a new column to your database table to hold the body (all the html, xml, graphics, hyperlinks etc that you want to appear in the recipiants email).
In database design terms you could also add a new table which would hold the body text and do a join to that table in your query. This would mean you would not have to modify the table holding the list, either way it's best to make the database column that holds the Body of type ntext.
Of course you may have to experiement a little with the layout.
You'll need to make a few dry runs at this, all we need to do is to get it to work for 3 or more names in the list (table) and we will assume it will work for all, with a tiny bit of tunning not to overload the SMTP server.
The logic is fairly straight forward, we want to read a record from the database, pass it to a send method that accepts each column (To, Subject, Body) as input parameters which then sends one email. Even though the mail server will queue requests to send, we could be gental and wait, perhaps 1 second before we make the next call to the send method, and so on untill we have passed all the records in the list.
In this scenario the email's body does not contain a personlised greeting, I'll cover that later.