Convert PST to MSG
Converts a message stored in a PST file to an individual MSG file.
Code
using DRIT.Mail;
using DRIT.Mail.Conversion;
using DRIT.Mail.Pst.PhysicalLayout;
// Open the PST file
using var pst = new PstFile("archive.pst");
// Enumerate folders and messages
foreach (var folder in pst.Folders.GetAllFoldersFlatDepthFirst())
{
foreach (var mapiMessage in folder.GetMapiMessages())
{
// Convert the PST-backed message to an MSG file
string outputName = $"{mapiMessage.Subject ?? "message"}.msg";
MailMessageConverter.PstMessageToMsg(mapiMessage, outputName);
}
}
Remarks
MailMessageConverter.PstMessageToMsg writes a PST-backed MapiMessage to an
MSG compound file. An overload accepting a Stream is also available for
in-memory conversion.