Mail Merge with Object in C# and VB.NET

Besides DataTable, GemBox.Email's mail merge process supports using custom types as data sources through its MailMerge.Merge overload methods. In that case, the reflection will be used to retrieve values for variables replacement.

If you want to avoid the reflection usage, then you can wrap your custom type with IMailMergeDataSource interface implementation and use a wrapper object as a mail merge data source instead of your original object. In that case, the mail merge engine will only use members from your IMailMergeDataSource implementation to retrieve the required values.

The following example demonstrates how to perform a mail merge using a custom object as a data source.

Screenshot of unique emails created with mail merge
Generating unique emails with mail merge in C# and VB.NET
using System.Collections.Generic;
using GemBox.Email;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create template message.
        MailMessage template = new MailMessage("sender@example.com");
        template.Subject = "%FirstName%, check our new offer!";
        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/>
Sincerely 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>";

        // Create data source.
        List<DataObject> data = new List<DataObject>()
        {
            new DataObject("jonh@example.com", "Mr.", "John", "Doe", 10, "https://example.com/check.html?name=John"),
            new DataObject("jane@example.com", "Mrs.", "Jane", "Doe", 20, "https://example.com/check.html?name=Jane"),
            new DataObject("joe@example.com", "Mr.", "Joe", "Unknown", 15, "https://example.com/check.html?name=Joe")
        };

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

        // Save mail messages.
        for (int i = 0; i < messages.Count; i++)
            messages[i].Save($"Message_{i}.eml");
    }
}

// Custom data source type.
class DataObject
{
    public string To { get; }
    public string Title { get; }
    public string FirstName { get; }
    public string LastName { get; }
    public int Discount { get; }
    public string Link { get; }

    public DataObject(string to, string title, string firstName, string lastName, int discount, string link)
    {
        this.To = to;
        this.Title = title;
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Discount = discount;
        this.Link = link;
    }
}
Imports System.Collections.Generic
Imports GemBox.Email

Module Program

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

        ' Create template message.
        Dim template As New MailMessage("sender@example.com")
        template.Subject = "%FirstName%, check our new offer!"
        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/>
Sincerely 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>"

        ' Create data source.
        Dim data As New List(Of DataObject) From
        {
            New DataObject("jonh@example.com", "Mr.", "John", "Doe", 10, "https://example.com/check.html?name=John"),
            New DataObject("jane@example.com", "Mrs.", "Jane", "Doe", 20, "https://example.com/check.html?name=Jane"),
            New DataObject("joe@example.com", "Mr.", "Joe", "Unknown", 15, "https://example.com/check.html?name=Joe")
        }

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

        ' Save mail messages.
        For i As Integer = 0 To messages.Count - 1
            messages(i).Save($"Message_{i}.eml")
        Next

    End Sub

End Module

Class DataObject

    Public ReadOnly Property [To] As String
    Public ReadOnly Property Title As String
    Public ReadOnly Property FirstName As String
    Public ReadOnly Property LastName As String
    Public ReadOnly Property Discount As Integer
    Public ReadOnly Property Link As String

    Public Sub New([to] As String, title As String, firstName As String, lastName As String, discount As Integer, link As String)
        Me.To = [to]
        Me.Title = title
        Me.FirstName = firstName
        Me.LastName = lastName
        Me.Discount = discount
        Me.Link = link
    End Sub
End Class

Check next example or download examples from GitHub.