GemBox.Email is a .NET component that enables you to read, write, receive, and send emails from your .NET applications using one simple API.

Mail Merge with DataTable source in C# and VB.NET

A mail merge is a process for creating multiple personalized mail messages based on a single message template and data source. The template message contains fixed text, which will be the same in every output message, and variables, which act as placeholders that are replaced by values from the data source. Once the mail merge starts, the template message is cloned for every data element in the data source, but with the variables replaced by values from the current data element.

The following example shows how to perform a mail merge using the GemBox.Email component. A MailMessage instance is used as a template message, where Subject, BodyText and BodyHtml contain fixed text and variables, defined as %<value name>%.

Value name can contain letters or digits, but no white space, and must be surrounded with '%' characters. To, Cc, Bcc and ReplyTo are predefined value names that can be used to set the corresponding properties in the output message. All predefined names are optional except 'To', since it represents the recipient's mail address.

Screenshot
Data Table Source Screenshot

See the full code below.

using GemBox.Email;
using System.Collections.Generic;
using System.Data;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create template message
        MailMessage template = new MailMessage(new MailAddress("sender@example.com"));
        template.Subject = "%FirstName%, check our new offer ...";
        template.BodyText = "Dear %Title% %LastName%,\r\n" +
                            "\r\n" +
                            "Check out or new offer on %link%.\r\n" +
                            "Visit us today and get additional %discount%% discount!\r\n" +
                            "\r\n" +
                            "Your,\r\n" +
                            "Special Store\r\n" +
                            "\r\n" +
                            "This email was sent to %To%.";
        template.BodyHtml = "<div>" +
                                "Dear <strong>%Title% %LastName%</strong>,<br/>" +
                                "<br/>" +
                                "Check out our new offer on this <a href=\"%link%\">link</a>.<br/>" +
                                "<span style=\"color:red;font-size:90%\">Visit us today and get additional %discount%% discount!</span><br/>" +
                                "<br/>" +
                                "Sincerly Yours,<br/>" +
                                "Special Store<br/>" +
                                "<br/>" +
                                "<span style=\"color:gray;font-size:80%\">This email was sent to <a href=\"mailto:%To%\">%To%</a>.</span>" +
                            "</div>";

        // Define data table columns ...
        DataTable data = new DataTable();
        data.Columns.Add("To", typeof(string));
        data.Columns.Add("Title", typeof(string));
        data.Columns.Add("FirstName", typeof(string));
        data.Columns.Add("LastName", typeof(string));
        data.Columns.Add("Discount", typeof(int));
        data.Columns.Add("Link", typeof(string));

        // ... and add some data
        data.Rows.Add("jonh@example.com", "Mr.", "John", "Doe", 10, "http://example.com/check.html?name=John");
        data.Rows.Add("jane@example.com", "Mrs.", "Jane", "Doe", 20, "http://example.com/check.html?name=Jane");
        data.Rows.Add("joe@example.com", "Mr.", "Joe", "Unknown", 15, "http://example.com/check.html?name=Joe");

        // Perform mail merge
        IList<MailMessage> messages = MailMerge.Merge(template, data);

        // Save messages to disk
        for (int i = 0; i < messages.Count; i++)
            messages[i].Save("Message_" + i, MailMessageFormat.Eml);
    }
}
Imports GemBox.Email

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Create template message
        Dim template As New MailMessage(New MailAddress("sender@example.com"))
        template.Subject = "%FirstName%, check our new offer ..."
        template.BodyText = "Dear %Title% %LastName%,\r\n" &
                            "\r\n" &
                            "Check out or new offer on %link%.\r\n" &
                            "Visit us today and get additional %discount%% discount!\r\n" &
                            "\r\n" &
                            "Your,\r\n" &
                            "Special Store\r\n" &
                            "\r\n" &
                            "This email was sent to %To%."
        template.BodyHtml = "<div>" &
                                "Dear <strong>%Title% %LastName%</strong>,<br/>" &
                                "<br/>" &
                                "Check out our new offer on this <a href=""%link%"">link</a>.<br/>" &
                                "<span style=""color:red;font-size:90%"">Visit us today and get additional %discount%% discount!</span><br/>" &
                                "<br/>" &
                                "Sincerly Yours,<br/>" &
                                "Special Store<br/>" &
                                "<br/>" &
                                "<span style=""color:gray;font-size:80%"">This email was sent to <a href=""mailto:%To%"">%To%</a>.</span>" &
                            "</div>"

        ' Define data table columns ...
        Dim data As New DataTable()
        data.Columns.Add("To", GetType(String))
        data.Columns.Add("Title", GetType(String))
        data.Columns.Add("FirstName", GetType(String))
        data.Columns.Add("LastName", GetType(String))
        data.Columns.Add("Discount", GetType(Integer))
        data.Columns.Add("Link", GetType(String))

        ' ... And add some data
        data.Rows.Add("jonh@example.com", "Mr.", "John", "Doe", 10, "http://example.com/check.html?name=John")
        data.Rows.Add("jane@example.com", "Mrs.", "Jane", "Doe", 20, "http://example.com/check.html?name=Jane")
        data.Rows.Add("joe@example.com", "Mr.", "Joe", "Unknown", 15, "http://example.com/check.html?name=Joe")

        ' Perform mail merge
        Dim messages As IList(Of MailMessage) = MailMerge.Merge(template, data)

        ' Save messages to disk
        For i As Integer = 0 To messages.Count - 1
            messages(i).Save("Message_" & i, MailMessageFormat.Eml)
        Next

    End Sub

End Module

Check next sample.