I have the following code to generate emails & store in a pst file. When the total size of attachments in the folder is more than 170MB I get an Out of memory exception when adding the message to PST Folder…
public static void Main(string[] args)
{
if (File.Exists(DestinationPstFile))
File.Delete(DestinationPstFile);
using (var storage = PersonalStorage.Create(DestinationPstFile, FileFormatVersion.Unicode))
{
var pstName = Path.GetFileNameWithoutExtension(DestinationPstFile);
var folder = storage.RootFolder.AddSubFolder(pstName);
var attachments = Directory.GetFileSystemEntries(FolderWithAttachments);
var mailMessage = new MailMessage
{
HtmlBody = “\r\n\r\n \r\n Text \r\n”,
Date = DateTime.UtcNow,
MessageId = “test”
};
foreach (var attachmentFile in attachments)
{
var attachment = new Attachment(attachmentFile);
mailMessage.AddAttachment(attachment);
}
using (var mapiMessage = MapiMessage.FromMailMessage(mailMessage))
{
folder.AddMessage(mapiMessage);
}
}
}