MailMerge Class |
Namespace: MailBee.Mime
The MailMerge type exposes the following members.
Name | Description | |
---|---|---|
AddAttachmentPattern |
Adds the attachment pattern to the collection of attachment patterns.
| |
ClearAttachmentPatterns |
Clears the collection of the attachments patterns.
| |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
Replace(String, String) |
Replaces all occurrences of the specified pattern in the message template with the specified actual value.
| |
Replace(String, String, MailMergeTargets) |
Replaces all occurrences of the specified case-sensitive pattern in the specified parts of the message with the actual value.
| |
ReplaceAsync(String, String) |
async/await version of Replace(String, String).
| |
ReplaceAsync(String, String, MailMergeTargets) |
async/await version of Replace(String, String, MailMergeTargets).
| |
Reset |
Resets the merged message.
| |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Name | Description | |
---|---|---|
MergedMessage |
Contains the completed mail message which is a result of mail merge operation.
|
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.
// 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++; }