Bulk Report Generation in PST Format via C#
Generate email messages in bulk and add to PST file via .NET API.
How to Generate PST based Reports Using C#
In order to create PST 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 PST via C#
- Create a template as MailMessage and add dynamic fields
- Create data source and mapping
- Initialize TemplateEngine using the MailMessage object
- Call TemplateEngine.Instantiate method to generate messages in bulk
- Create a new PST with PersonalStorage.Create method
- Add folder in PST
- Add messages from TemplateEngine to folder using FolderInfo.Add method
- Save in PST 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 PST 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 new PST
var pst = Email.Storage.Pst.PersonalStorage.Create("storage.pst", Email.Storage.Pst.FileFormatVersion.Unicode);
// add folder to PST
var inboxFolder = pst.RootFolder.AddSubFolder("Inbox");
// create messages from engine
messages = engine.Instantiate(dt, mappings);
for (int i = 0; i < messages.Count; i++)
{
// save messages in PST format
messages[i].Save(i + ".pst");
inboxFolder.AddMessage(MapiMessage.FromMailMessage(messages[i]));
}
// save in PST format
pst.SaveAs("output.pst", Email.Storage.Pst.FileFormat.Pst);
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 PST
Check our live demos to create PST files with following benefits.
PST What is PST File Format
Files with .PST extension represent Outlook Personal Storage Files (also called Personal Storage Table) that store variety of user information. User information is stored in folders of different types that include emails, calendar items, notes, contacts, and several other file formats. PST files are used for archiving emailing data offline that can be later loaded and viewed in various applications.
Read More