Home | 2.0 Content | Code Samples | Tips and Tricks | -- Tutorials -- | ASP.Net Books | Resources | Hosting
The Best Place Yet for ASP.Net - ASPNet101.com
Power Search


Don't Miss any New Content!
Sign up for Newsletter!

  Menu  
  Code Samples
Tips and Tricks
Tutorials
ASP.Net FAQ
Training
Resources
Hosting
Recommended Books
Home
 
     
  Our Sponsors :  
 
ASP Express WebHost4Life DotNetSlackers
 
     

69
If you find this site helpful, please donate to help keep it online


     
 
   

Sending Mass Emails Part 2
(Sending Separate Emails to each Recipient)

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!

   
Choose From Tutorials:
    Creating a Method to Return a DataSet in C#    
    Defining Error Displays Globally    
    Beginners Guide to Comparing Strings    
    Creating a Feedback Form    
    Beginners Guide: Scoping of Methods    
    Introduction to Parsing Strings    
    A Beginners Guide to the HyperinkField    
    Adding 'Last Updated' To Footer    
    Page Level Impersonation    
    Error Trapping in ASP.Net    
    Inserting and Updating with a SQLDataSource    
    Creating a Hit Counter    
    Using PreviousPage with a Master Page    
    Sorting/Filtering Fixed DataSet Results    
    Helper Functions    
    The Basics of Using SQL    
    A Beginners Guide to the Connection String    
    Querystring Results and SQL Statements    
    Membership/Roles with Remote DB    
    Beginners Guide to Com in ASP.Net    
    Multiple DropdownLists with AppendDataBoundItems    
    Creating a Login/Email/Activation Page    
    Creating a Popup Details Page    
    The Beginner's Guide to an ArrayList    
    Beginners Guide to the BulletedList    
    Configuring a Trusted SQL Srvr Connection    
    Emailing Form Results with ASP.Net 2.0    
    Adding Dynamic Content to Your Pages    
    Using TemplateFields    
    Menu User Control with Rollovers    
    Buttons and LinkButtons    
    Transferring Form Results to 2nd Page    
    Two Page Data Retrieval    
    Using the AdRotator Control    
    Using the QueryStringParameter    
    The 3 'Execute' Command Methods    
    Beginner's Guide to Master Pages    
    Displaying DataBase Information    
    Beginner's Guide to Themes    
    Sessions/Visitors Online    
    Sending Emails With ASP.Net 2.0    
    Sending Multiple Emails At Once    
    A Beginner's Guide to the GridView    
    Emailing Form Results    
    A Beginner's Guide to the DataSource Control    
    Inserting Data Into Two Tables    
    Coolest New v2.0 Additions    
    File System List With System.IO    
    Remote SQL Server/Web Matrix Server/WinXP Home    
    Understanding Regular Expressions    
    Creating a 'States' User Control    
    Parameterized Queries - Part 2    
    Iterating Through a List-Type Server Control    
    Creating an Online Bible    
    Beginner's Guide - Installing MSDE    
    Nesting Server Controls    
    Function Libraries    
    String Properties/Methods - Part 2    
    An Introduction to Validation Controls - Part I    
    String Properties/Methods - Part 1    
    Sending Mass Emails Part 2    
    Examining List Controls    
    Beginners Guide to Forms Authentication    
    If Not Page.IsPostBack    
    Using MySQL with ASP.NET    
    Single & Double Quotes in SQL    
    Matching Regular Expressions    
    Parameterized Queries in ASP.Net    
 
     
Home | 2.0 Content | Code Samples | Tips and Tricks | -- Tutorials -- | ASP.Net Books | Resources | Hosting
Send us your comments, questions or suggestions : Suggestions
Copyright © 2010 ASPNet101.com All Rights Reserved