Provides properties and methods for creating customized messages based on a template.
For a list of all members of this type, see MailMerge Members.
System.Object
MailBee.Mime.MailMerge
Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.
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.
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.
[C#] // 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++; }
[Visual Basic] ' To use the code below, import these namespaces at the top of your code. Imports System.Collections.Specialized Imports MailBee Imports 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... Dim 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. Dim msg As 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%%. Visit us (www.afterlogic.com)" msg.BodyHtmlText = "<html>" & VbCrLf & _ "<body>" & VbCrLf & _ "Hello, <b>%%NAME%%</b>. Your e-mail is <i>%%EMAIL%%</i>. Visit us <a href=""http://www.afterlogic.com/"">Afterlogic</a>" & VbCrLf & _ "Attached File: <font color=""red"">%%FILENAME%%</font>" & VbCrLf & _ "<br>" & VbCrLf & _ "<img src=""cid:123456"" alt="".NET"">" & VbCrLf & _ "</body>" & VbCrLf & _ "</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"); msg.Merge.AddAttachmentPattern("%%FILENAME%%") Dim i As Integer = 1 Dim strArrays As String() = {"C:\Docs\image1.jpg", "C:\Docs\image2.gif", "C:\Docs\image3.gif"} For Each de As DictionaryEntry In sd msg.Merge.Replace("%%NAME%%", de.Value) msg.Merge.Replace("%%FILENAME%%", strArrays(i - 1), MailMergeTargets.BodyHtmlText) msg.Merge.Replace("%%EMAIL%%", de.Key, MailMergeTargets.Recipients Or _ MailMergeTargets.Subject Or MailMergeTargets.BodyHtmlText Or MailMergeTargets.BodyPlainText) msg.Merge.MergedMessage.SaveMessage(String.Format("C:\Temp\merge{0}.eml", i)) msg.Merge.Reset() i = i + 1 Next
Namespace: MailBee.Mime
Assembly: MailBee.NET (in MailBee.NET.dll)
MailMerge Members | MailBee.Mime Namespace | MailMessage | MailMergeTargets