Smtp.NET 3 Documentation

SmtpServer.MailMerge Method 

Combines data from the DBDataTable with various properties in the message.

[Visual Basic]
Public Function MailMerge( _
   ByVal dataTable As DataTable, _
   ByVal emailColumn As String, _
   ByVal recipientType As AddressTypes, _
   ByVal replacementIds As Hashtable _
) As ReturnCodes
[C#]
public ReturnCodes MailMerge(
   DataTable dataTable,
   string emailColumn,
   AddressTypes recipientType,
   Hashtable replacementIds
);

Parameters

dataTable
A datatable that contains the data to use in the mail merge.
emailColumn
The name of the column in the data table that holds email addresses.
recipientType
The recipient type for the addresses in the DBDataTable of enumerated type AddressTypes
replacementIds
A hashtable of replacement tags to use with mail merge functionality. Each key of the array should a string that Smtp.NET will search for and replace in your message body, subject, etc.. For example, if the key is "$email$" - Smtp.NET will search for the string $email$ in the message body, subject, and addressing. The value of the hashtable represents the name(s) of columns to pull data from for replacement. You can pull data from multiple columns by seperating values with a comma. Example: key="$name$", value="first_name,last_name".

NOTE: The search that is performed is case-sensitive. If you specify $email$ as a replacement id, your message should contain $email$, not $Email$.

Return Value

An integer of enumerated type ReturnCodes.

Remarks

After this method is called, all recipients in the "to", "cc", "bcc" fields are cleared.

Events

Event TypeReason
After each email message is sent, this event will be raised with the current DataRow in the specified DataTable and the percentage complete of the entire mail merge.

See: ProgressEventHandler for a complete example.

Example

For example: In the message, "Dear $A$: Thank you for ordering $B$ from our $C$ store...", $A$, $B$ and $C$ are merge points into which data is inserted from the list.

Letter

Database

Dear $A$:

Thank you for ordering $B$ at our $C$ store.

Sincerely,

The store owners

$A$

$B$

$C$

Mr. Smith

1 Widget

New York

Ms. Gomer

2 Dingits

Chicago

Mr. Jones

1 Frabbits

Los Angeles

Mr. Silver

3 Widgets

Tucson

Ms. Change

2 Gobbits

Philadelphia

Mr. Russion

5 Wibbles

Washington D.C

Example Output (without formatting):

Dear Mr. Smith: Thank you for ordering 1 Widget at our New York store. Sincerely, The store owners

Dear Ms. Gomer: Thank you for ordering 2 Dingits at our Chicago store. Sincerely, The store owners

Dear Mr. Jones: Thank you for ordering 1 Frabbits at our Los Angeles store. Sincerely, The store owners

Dear Mr. Silver: Thank you for ordering 3 Widgets at our Tucson store. Sincerely, The store owners

Dear Ms. Change: Thank you for ordering 2 Gobbits at our Philadelphia store. Sincerely, The store owners

Dear Mr. Russion: Thank you for ordering 3 Wibbles at our Washington D.C. store. Sincerely, The store owners

[C#]
public void Send()
{
     SmtpServer oSmtpDotNet = new SmtpServer();
 
     // Set the remote server name.  If left blank, "localhost" is used.
     oSmtpDotNet.ServerAddress = "mail.domain.com"
     
     // Message Addressing
     oSmtpDotNet.FromAddress = "from@domain.com";
 
   // Set the message subject and body
   oSmtpDotNet.Body = "Dear $A$:  Thank you for ordering $B$ at our $C$ store.  Sincerely, The store owners";
   oSmtpDotNet.Subject = "Your order of $B$";
      
   // Set up the hashtable for the mail merge replacements. 
   Hashtable oHashtable = new Hashtable();
   oHashtable.Add("$A$", "customerSalutation,customerLastName");
   oHashtable.Add("$B$", "itemCount,itemName"); 
   oHashtable.Add("$C$", "itemStoreSoldName"); 
   ReturnCodes nRC = oSmtpDotNet.MailMerge(dataTable, "customerEmail", SmtpDotNet.AddressTypes.TO, oHashtable);

     if ( nRC != ReturnCodes.SUCCESS )
     {
          Console.WriteLine("Error #"+nRC+" occurred.");
          Console.WriteLine(oSmtpDotNet.LastError);
          Console.Write(oSmtpDotNet.LogFileToString(false));
          return;
     }
 
     // Success!
     Console.WriteLine("Success!");
}
[VB]
Public Function Send() As Integer
     Dim oSmtpDotNet As New SmtpServer()
     Dim nRC As ReturnCodes

     ' Set the remote server name.  If left blank, "localhost" is used.
     oSmtpDotNet.ServerAddress = "mail.domain.com"
     
     ' Message Addressing
     oSmtpDotNet.FromAddress = "from@domain.com"

 
   ' Set the message subject and body
   oSmtpDotNet.Body = "Dear $A$:  Thank you for ordering $B$ at our $C$ store.  Sincerely, The store owners"
   oSmtpDotNet.Subject = "Your order of $B$"
      
   ' Set up the hashtable for the mail merge replacements. 
   Hashtable oHashtable = new Hashtable()
   oHashtable.Add("$A$", "customerSalutation,customerLastName")
   oHashtable.Add("$B$", "itemCount,itemName") 
   oHashtable.Add("$C$", "itemStoreSoldName") 
   ReturnCodes nRC = oSmtpDotNet.MailMerge(dataTable, "customerEmail", SmtpDotNet.AddressTypes.TO, oHashtable)

     If (nRC <> ReturnCodes.SUCCESS) Then
         ' A problem occurred.
         ' Write out the last error encountered
         Console.WriteLine(oSmtpDotNet.LastError)
         ' Write out the log file contents
         Console.WriteLine(oSmtpDotNet.LogFileToString(False))
         Exit Function
     End If

     ' Success!
     Console.WriteLine("Message Sent!")
End Function

See Also

SmtpServer Class | SmtpServer Members | SmtpDotNet Namespace | ReturnCodes