Sending Mass Emails Part 2

If you haven’t read Part 1 in this series, you can do so now, by clicking here:

Sending Multiple Emails At Once

One of the major drawbacks of creating a mass emailing like we did in Part 1, was personalization and customization were practically non-existent. All email addresses were kept in a string, which populated the BCC section. Each recipient could easily see that this was a mass email. Naturally, that’s normally OK, but by being able to personalize an email gives the recipient a better feeling when he things that the ‘Sender’ of the email took the time to send the email to him, personally. By building your mass email this way, practically any level of personalization and customization is all up to you. For instance, you could create a link in the email itself, that, when clicked, can run a script on your web site to delete that user from the email list automatically.

For this example, all you’ll really need is:

  1. Import the System.Web.Mail namespace
  2. Import the Database namespaces necessary
  3. A table of Data, in the Database (for this sample, we only have two fields (Name and Email)
  4. One subroutine for the code (for this example, we’ll use Page_Load)
  5. A label with an ID of ‘lblEmails’ (only to show the list of emails for the test)

First, we’ll Import the namespaces. For emailing, we must add:

<%@ Import Namespace="System.Web.Mail" %>

For the database (we’re using SQL Server for this tutorial), we must add:

<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SQLClient" %>

Now, for the real ‘meat’ of this tutorial, first, we’ll add two Global variables:

Dim sEmail as String Dim sBody as String

Next, in a Subroutine of your choice, add this code:

Dim objEmail as New MailMessage sBody = "This is our test email to you" objEmail.Subject="This is my Subject" Dim MySQL as string = "Select Name, email from TestMail" Dim MyConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("YourConfigSettings")) Dim objDR as SQLDataReader Dim Cmd as New SQLCommand(MySQL, MyConn) MyConn.Open() objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection) While objDR.Read() sEmail = objDR("email") objEmail.To =sEmail objEmail.FROM="fromYou@YourDomain.com" objEmail.Body=sBody & vbcrlf & "Name: " & objDR("Name") & vbcrlf & "Email: " & sEmail objEmail.BodyFormat = MailFormat.Text SmtpMail.SmtpServer ="mail.YourDomain.com" SmtpMail.Send(objEmail) lblEmails.text+=sEmail & "<br>" End While MyConn.Close

Here, in the While/End While loop, we do all the real work. We iterate through each record of the table, sending a separate email, personalized, to each email address in the table. For the sake of this tutorial, we’re also adding each email address to a label to show how many emails were sent, and to whom. Naturally, this is not a necessary step – it’s there only for visualizing what actually has happened.

Remarkably, this is all you really need to get started. The customization and/or personalization is up to you!

Related Posts:

  • No Related Posts
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!