Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created September 26, 2025 15:32
Show Gist options
  • Save aspose-com-gists/bac69d1a5a8eda883cfc3a41d2e2a308 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/bac69d1a5a8eda883cfc3a41d2e2a308 to your computer and use it in GitHub Desktop.
Extract Properties, Attachments, and Recipients from PST Files in C#
// Load the PST file
using (var pst = PersonalStorage.FromFile("sample.pst"))
{
// Specify the EntryId of the target message
var targetEntryId = "..."; // Your message's EntryId here
// Extract the attachments collection for this message
var attachments = pst.ExtractAttachments(targetEntryId);
// Check if any attachments exist
if (attachments.Any())
{
Console.WriteLine($"Found {attachments.Count} attachment(s). Saving...");
// Save each attachment to disk
foreach (var attachment in attachments)
{
var filePath = Path.Combine("OutputDir", attachment.FileName);
attachment.Save(filePath);
Console.WriteLine("Saved: " + filePath);
}
}
else
{
Console.WriteLine("No attachments found for this message.");
}
}
// Load the PST file
using (var pst = PersonalStorage.FromFile("sample.pst"))
{
// Get the required folder (e.g., Inbox)
var inbox = pst.RootFolder.GetSubFolder("Inbox");
// Loop through all messages in the folder
foreach (var messageInfo in inbox.EnumerateMessages())
{
// Extract the Subject property (tag: PR_SUBJECT)
var subjectProperty = pst.ExtractProperty(
messageInfo.EntryId,
KnownPropertyList.TagSubject.Tag
);
// Extract the Sender Email property (tag: PR_SENDER_EMAIL_ADDRESS)
var senderProperty = pst.ExtractProperty(
messageInfo.EntryId,
KnownPropertyList.SenderEmailAddress.Tag
);
// Display the extracted information
Console.WriteLine("Subject: " + (subjectProperty?.GetString() ?? "N/A"));
Console.WriteLine("From: " + (senderProperty?.GetString() ?? "N/A"));
Console.WriteLine("----------------------------------------");
}
}
// Load the PST file
using (var pst = PersonalStorage.FromFile("sample.pst"))
{
// Specify the EntryId of the target message
var targetEntryId = "..."; // Your message's EntryId here
// Extract the recipients collection for this message
var recipients = pst.ExtractRecipients(targetEntryId);
// Check if any recipients exist
if (recipients.Any())
{
Console.WriteLine("Recipients:");
// Display details for each recipient
foreach (var recipient in recipients)
{
Console.WriteLine($"\tName: {recipient.DisplayName}");
Console.WriteLine($"\tEmail: {recipient.EmailAddress}");
Console.WriteLine($"\tType: {recipient.RecipientType}"); // To, Cc, Bcc
Console.WriteLine("\t---");
}
}
else
{
Console.WriteLine("No recipients found for this message.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment