استخراج پیامها از Outlook PST و ذخیره آنها به صورت MSG
این نکته مهاجرت نشان میدهد چگونه پیامها را از فایل Outlook PST استخراج کرده و به صورت فایلهای MSG بر روی دیسک ذخیره کنید. این کار شامل چندین مرحله است:
- خواندن فایل Outlook PST،
- استخراج پیامها و در نهایت،
- ذخیره پیامهای استخراجشده.
راههای مختلفی برای دستیابی به همان نتیجه وجود دارد: این مقاله مقایسهای بین استفاده از VSTO و Aspose.Email ارائه میدهد. ابتدا، موارد زیر هستند نمونههای کد برای استفاده از Microsoft Office Interop برای استخراج پیامها از PST. پس از آن مثال، نمونههای کد نشان میدهند چگونه از Aspose.Email Outlook استفاده کنید, در جاوا، برای انجام همان کار.
استفاده از Microsoft Office Interop
برای استفاده از اشیاء Office Automation برای Microsoft Outlook، مراجع به کتابخانههای Microsoft Office Interop برای Outlook را به پروژه اضافه کنید. Microsoft Office Outlook نیز باید بر روی دستگاهی که کد در آن اجرا میشود نصب باشد. فضای نام (namespace) استفادهشده در نمونه کد زیر Microsoft.Office.Interop.Outlook است.
مثالهای برنامهنویسی
C#
string pstFilePath = @"C:\sample.pst";
Application app = new Application();
NameSpace outlookNs = app.GetNamespace("MAPI");
// Add PST file (Outlook Data File) to Default Profile
outlookNs.AddStore(pstFilePath);
MAPIFolder rootFolder = outlookNs.Stores["items"].GetRootFolder();
// Traverse through all folders in the PST file
// TODO: This is not recursive
Folders subFolders = rootFolder.Folders;
foreach (Folder folder in subFolders)
{
Items items = folder.Items;
foreach (object item in items)
{
if (item is MailItem)
{
// Retrieve the Object into MailItem
MailItem mailItem = item as MailItem;
Console.WriteLine("Saving message {0} ....", mailItem.Subject);
// Save the message to disk in MSG format
// TODO: File name may contain invalid characters [\ / : * ? " < > |]
mailItem.SaveAs(@"\extracted\" + mailItem.Subject + ".msg",OlSaveAsType.olMSG);
}
}
}
// Remove PST file from Default Profile
outlookNs.RemoveStore(rootFolder);
استفاده از Aspose.Email
کدهای زیر همان کاری را انجام میدهند که کد بالا اما از Aspose.Email استفاده میکند. با نصب Aspose.Email برای جاوا، دیگر نیازی به Microsoft Outlook بر روی دستگاه نیست. فقط کافی است Aspose.Email را برای ساخت و اجرای پروژه ارجاع دهید.
نمونههای برنامهنویسی
String pstFilePath = "C:\\sample.pst";
// Create an instance of PersonalStorage and load the PST from file
try (PersonalStorage personalStorage = PersonalStorage.fromFile(pstFilePath)) {
// Get the list of subfolders in PST file
FolderInfoCollection folderInfoCollection = personalStorage.getRootFolder().getSubFolders();
// Traverse through all folders in the PST file
// This is not recursive
for (FolderInfo folderInfo : folderInfoCollection) {
// Get all messages in this folder
MessageInfoCollection messageInfoCollection = folderInfo.getContents();
// Loop through all the messages in this folder
for (MessageInfo messageInfo : messageInfoCollection) {
// Extract the message in MapiMessage instance
MapiMessage message = personalStorage.extractMessage(messageInfo);
System.out.println("Saving message " + message.getSubject() + " ...");
// Save the message to disk in MSG format
// TODO: File name may contain invalid characters [\ / : * ? " < > |]
message.save("\\extracted\\" + message.getSubject() + ".msg");
}
}
}