public static void Run() { // The path to the File directory. // ExStart:ExtractAttachmentsFromPSTMessages string dataDir = RunExamples.GetDataDir_Outlook(); using (PersonalStorage personalstorage = PersonalStorage.FromFile(dataDir + "Outlook.pst")) { FolderInfo folder = personalstorage.RootFolder.GetSubFolder("Inbox"); foreach (var messageInfo in folder.EnumerateMessagesEntryId()) { MapiAttachmentCollection attachments = personalstorage.ExtractAttachments(messageInfo); if (attachments.Count != 0) { foreach (var attachment in attachments) { if (!string.IsNullOrEmpty(attachment.LongFileName)) { if (attachment.LongFileName.Contains(".msg")) { continue; } else { attachment.Save(dataDir + @"\Attachments\" + attachment.LongFileName); } } } } } } // ExEnd:ExtractAttachmentsFromPSTMessages }
public static void Run() { // ExStart:SaveMessagesDirectlyFromPSTToStream // The path to the file directory. string dataDir = RunExamples.GetDataDir_Outlook(); // Load the Outlook file string path = dataDir + "PersonalStorage.pst"; // Save message to MemoryStream using (PersonalStorage personalStorage = PersonalStorage.FromFile(path)) { FolderInfo inbox = personalStorage.RootFolder.GetSubFolder("Inbox"); foreach (MessageInfo messageInfo in inbox.EnumerateMessages()) { using (MemoryStream memeorystream = new MemoryStream()) { personalStorage.SaveMessageToStream(messageInfo.EntryIdString, memeorystream); } } } // Save message to file using (PersonalStorage pst = PersonalStorage.FromFile(path)) { FolderInfo inbox = pst.RootFolder.GetSubFolder("Inbox"); foreach (MessageInfo messageInfo in inbox.EnumerateMessages()) { using (FileStream fs = File.OpenWrite(messageInfo.Subject + ".msg")) { pst.SaveMessageToStream(messageInfo.EntryIdString, fs); } } } using (PersonalStorage pst = PersonalStorage.FromFile(path)) { FolderInfo inbox = pst.RootFolder.GetSubFolder("Inbox"); // To enumerate entryId of messages you may use FolderInfo.EnumerateMessagesEntryId() method: foreach (string entryId in inbox.EnumerateMessagesEntryId()) { using (MemoryStream ms = new MemoryStream()) { pst.SaveMessageToStream(entryId, ms); } } } // ExEnd:SaveMessagesDirectlyFromPSTToStream }
// ExStart:UpdatePSTCustomProperites public static void Run() { // Load the Outlook file string dataDir = RunExamples.GetDataDir_Outlook() + "Outlook.pst"; using (PersonalStorage personalStorage = PersonalStorage.FromFile(dataDir)) { FolderInfo testFolder = personalStorage.RootFolder.GetSubFolder("Inbox"); // Create the collection of message properties for adding or updating MapiPropertyCollection newProperties = new MapiPropertyCollection(); // Normal, Custom and PidLidLogFlags named property MapiProperty property = new MapiProperty(MapiPropertyTag.PR_ORG_EMAIL_ADDR_W, Encoding.Unicode.GetBytes("*****@*****.**")); MapiProperty namedProperty1 = new MapiNamedProperty(GenerateNamedPropertyTag(0, MapiPropertyType.PT_LONG), "ITEM_ID", Guid.NewGuid(), BitConverter.GetBytes(123)); MapiProperty namedProperty2 = new MapiNamedProperty(GenerateNamedPropertyTag(1, MapiPropertyType.PT_LONG), 0x0000870C, new Guid("0006200A-0000-0000-C000-000000000046"), BitConverter.GetBytes(0)); newProperties.Add(namedProperty1.Tag, namedProperty1); newProperties.Add(namedProperty2.Tag, namedProperty2); newProperties.Add(property.Tag, property); testFolder.ChangeMessages(testFolder.EnumerateMessagesEntryId(), newProperties); } }