Outlook PST से संदेश निकालना और उन्हें MSG में सहेजना
यह माइग्रेशन टिप दिखाती है कि Outlook PST फ़ाइल से संदेश कैसे निकाले और उन्हें डिस्क पर MSG फ़ाइलों के रूप में सहेजे। इसमें कई चरण शामिल हैं:
- Outlook PST फ़ाइल पढ़ें,
- संदेश निकालें और अंत में,
- निकाले गए संदेशों को सहेजें।
एक ही परिणाम प्राप्त करने के विभिन्न तरीके हैं: यह लेख VSTO और Aspose.Email के उपयोग की तुलना करता है। पहले, हैं Microsoft Office Interop के उपयोग के कोड नमूने PST से संदेश निकालने के लिए। उस उदाहरण के बाद, कोड नमूने दिखाते हैं कि Aspose.Email Outlook का उपयोग कैसे करें, Java में, उसी कार्य को करने के लिए।
Microsoft Office Interop का उपयोग करना
Microsoft Outlook के लिए Office Automation ऑब्जेक्ट्स का उपयोग करने के लिए, प्रोजेक्ट में Microsoft Office Interop for Outlook लाइब्रेरीज़ के रेफ़रेंस जोड़ें। कोड चलने वाली मशीन पर Microsoft Office Outlook स्थापित होना चाहिए। नीचे दिए गए कोड नमूने में उपयोग किया गया नेमस्पेस 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 for Java स्थापित होने पर 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");
}
}
}