Bulk Report Generation in EML Format via C#
Generate EML email messages in bulk without requiring Outlook or Thunderbird.
How to Generate EML based Reports Using C#
In order to create EML 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 EML 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 EML 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 EML 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 EML format
messages[i].Save(i + ".eml");
}
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 EML
Check our live demos to create EML files with following benefits.
EML What is EML File Format
EML file format represents email messages saved using Outlook and other relevant applications. Almost all emailing clients support this file format for its compliance with RFC-822 Internet Message Format Standard. Microsoft Outlook is the default software for opening EML message types. EML files can be used for saving to disc as well as sending out to recipients using communication protocols.
Read More