Bulk Report Generation in MSG Format via C#
Generate MSG email messages in bulk without requiring Outlook or Thunderbird.
How to Generate MSG based Reports Using C#
In order to create MSG reports, we’ll use
API which is a feature-rich, powerful and easy to use report generation API for C# platform. Open
package manager, search for Aspose.Email and install. You may also use the following command from the Package Manager Console.
Package Manager Console Command
PM> Install-Package Aspose.Email
Steps to Assemble MSG via C#
- Create a template from MailMessage
- Add dynamic fields for Subject, To, From & HtmlBody fields
- Create a TemplateEngine using the MailMessage object
- Create data source and mapping to the template fields
- Create messages in bulk using TemplateEngine.Instantiate method
- Save messages in MSG format
System Requirements
Aspose.Email for .NET is supported on all major operating systems. Just make sure that you have the following prerequisites.
- Microsoft Windows or a compatible OS with .NET Framework, .NET Core, and Xamarin Platforms
- Development environment like Microsoft Visual Studio
- Aspose.Email for .NET referenced in your project
C# example code for MSG message report generation
// create a template from MailMessage
MailMessage template = new MailMessage();
// add template field to subject
template.Subject = "Hello, #FirstName#";
template.From = new MailAddress("This email address is being protected from spambots. You need JavaScript enabled to view it.", "This email address is being protected from spambots. You need JavaScript enabled to view it.");
// add template field to receipt
template.To.Add(new MailAddress("#Receipt#", true));
// add template field to html body
template.HtmlBody = "Dear #FirstName# #LastName# Sent Date: #Date#";
// create a new TemplateEngine with the template message.
var engine = new Email.Tools.Merging.TemplateEngine(template);
// fill a DataTable
var dt = new System.Data.DataTable();
dt.Columns.Add("Receipt", typeof(string));
dt.Columns.Add("First Name", typeof(string));
dt.Columns.Add("Last Name", typeof(string));
dt.Columns.Add("Date", typeof(DateTime));
System.Data.DataRow dr;
dr = dt.NewRow();
dr["Receipt"] = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
dr["First Name"] = "Nancy";
dr["Last Name:"] = "Davolio";
dr["Date"] = System.DateTime.Now;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Receipt"] = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
dr["First Name"] = "Andrew";
dr["Last Name"] = "Fuller";
dr["Date"] = System.DateTime.Now;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Receipt"] = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
dr["First Name"] = "Janet";
dr["Last Name"] = "Leverling";
dr["Date"] = System.DateTime.Now;
dt.Rows.Add(dr);
// map columns
var mappings = new System.Data.Common.DataColumnMappingCollection();
mappings.Add(new System.Data.Common.DataColumnMapping("Receipt", "Receipt"));
mappings.Add(new System.Data.Common.DataColumnMapping("First Name", "FirstName"));
mappings.Add(new System.Data.Common.DataColumnMapping("Last Name", "LastName"));
mappings.Add(new System.Data.Common.DataColumnMapping("Date", "Date"));
Aspose.Email.MailMessageCollection messages;
// create messages from engine
messages = engine.Instantiate(dt, mappings);
for (int i = 0; i < messages.Count; i++)
{
// save messages in MSG format
messages[i].Save(i + ".msg");
}
About Aspose.Email for .NET API
Aspose.Email is a Microsoft Outlook and Thunderbird formats parsing solution. One can easily create, manipulate, convert email and storage formats such as MSG, EMLX, EML and MHT. Handling of email attachments, customization of message headers and implementation of different network protocols like POP3, IMAP & SMTP to send & receive emails is much easier. Its a standalone API and does not require Microsoft Outlook or any other software installation.Free App to Assemble MSG
Check our live demos to create MSG files with following benefits.
MSG What is MSG File Format
MSG is a file format used by Microsoft Outlook and Exchange to store email messages, contact, appointment, or other tasks. Such messages may contain one or more email fields, with the sender, recipient, subject, date, and message body, or contact information, appointment particulars, and one or more task specifications. The properties that constitute the Message object, including are also a part of the MSG file. MSG file has headers, main message body, and hyperlinks as plain ASCII text. MSG files are also suitable with the programs that need Microsoft's Messaging Applications Programming Interface (MAPI).
Read More