MailMerge Class
Provides properties and methods for creating customized messages based on a template.
Inheritance Hierarchy
SystemObject
  MailBee.MimeMailMerge

Namespace: MailBee.Mime
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax
public class MailMerge

The MailMerge type exposes the following members.

Methods
  NameDescription
Public methodCode exampleAddAttachmentPattern
Adds the attachment pattern to the collection of attachment patterns.
Public methodClearAttachmentPatterns
Clears the collection of the attachments patterns.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodCode exampleReplace(String, String)
Replaces all occurrences of the specified pattern in the message template with the specified actual value.
Public methodCode exampleReplace(String, String, MailMergeTargets)
Replaces all occurrences of the specified case-sensitive pattern in the specified parts of the message with the actual value.
Public methodReplaceAsync(String, String)
async/await version of Replace(String, String).
Public methodReplaceAsync(String, String, MailMergeTargets)
Public methodCode exampleReset
Resets the merged message.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Properties
  NameDescription
Public propertyCode exampleMergedMessage
Contains the completed mail message which is a result of mail merge operation.
Top
Remarks
MailMerge class is very flexible and can be used for creating of e-mail messages based on a template and data from any source.

However, if such flexibility is not needed and the source of the data is a database, then it's easier to use Smtp.SendMailMerge method.

Examples
This sample defines a template and generates 3 e-mail messages based on it. The template includes both static fields (for instance, From field remains the same for all e-mails in the series) and dynamic patterns which are replaced with actual values for every e-mail generated. Two attachments are added to each message: one static and one pattern-based.
// To use the code below, import these namespaces at the top of your code.
using System.Collections;
using System.Collections.Specialized;
using MailBee;
using MailBee.Mime;

// The actual code (put it into a method of your class)

// Create an object to store the list of e-mail addresses and friendly names...
StringDictionary sd = new StringDictionary();

// ...add populate it with some values.
sd.Add("jdoe@domain.com", "John Doe");
sd.Add("kathy@example.com", "Kathy");
sd.Add("mark@company.com", "Mark");

// Set up an e-mail template.
MailMessage msg = new MailMessage();
msg.To.AsString = @"""%%NAME%%"" <%%EMAIL%%>";
msg.From.Email = "john.smith@site.com";
msg.Subject = "Hello, %%NAME%%.";
msg.BodyPlainText = @"Hello, %%NAME%%. Your e-mail is %%EMAIL%%.";
msg.BodyHtmlText = @"<html>
<body>
Hello, <b>%%NAME%%</b>. Your e-mail is <i>%%EMAIL%%</i>.
Attached File: <font color=""red"">%%FILENAME%%</font>
<br>
<img src=""cid:123456"" alt="".NET"">
</body>
</html>";

// The template also includes the attachment which is common for all e-mails in the series...
msg.Attachments.Add(@"C:\Docs\image17b.gif", "logo.gif", "123456");

// ... and the attachment which is unique for each e-mail.
msg.Merge.AddAttachmentPattern(@"C:\Docs\%%FILENAME%%");

// Create array of actual filenames which will substitute %%FILENAME%% pattern in merged e-mails.
string[] strArrays = {"image1.jpg", "image2.gif", "image3.gif"};

// Perform actual mail merge and save the resulting e-mails to disk.
int i = 1;
foreach (DictionaryEntry de in sd)
{
    msg.Merge.Replace(@"%%NAME%%", (string)de.Value);
    msg.Merge.Replace(@"%%FILENAME%%", strArrays[i-1], MailMergeTargets.BodyHtmlText);
    msg.Merge.Replace(@"%%EMAIL%%", (string)de.Key, MailMergeTargets.Recipients |
        MailMergeTargets.Subject | MailMergeTargets.BodyHtmlText | MailMergeTargets.BodyPlainText);

    msg.Merge.MergedMessage.SaveMessage(string.Format(@"C:\Temp\merge{0}.eml", i));

    msg.Merge.Reset();
    i++;
}
See Also