Mengekstrak Pesan dari Outlook PST dan Menyimpannya ke MSG
Tips migrasi ini menunjukkan cara mengekstrak pesan dari file Outlook PST dan menyimpannya ke disk sebagai file MSG. Ini melibatkan beberapa langkah:
- Membaca file Outlook PST,
- mengekstrak pesan dan, akhirnya,
- menyimpan pesan yang diekstrak.
Ada berbagai cara untuk mencapai hasil yang sama: artikel ini membandingkan penggunaan VSTO dan Aspose.Email. Pertama, adalah contoh kode untuk menggunakan Microsoft Office Interop untuk mengekstrak pesan dari PST. Setelah contoh itu, contoh kode menunjukkan cara menggunakan Aspose.Email Outlook, dalam Java, untuk melakukan tugas yang sama.
Menggunakan Microsoft Office Interop
Untuk menggunakan objek Office Automation untuk Microsoft Outlook, tambahkan referensi ke pustaka Microsoft Office Interop untuk Outlook ke dalam proyek. Microsoft Office Outlook juga harus terinstal pada mesin tempat kode dijalankan. Namespace yang digunakan dalam contoh kode berikut adalah Microsoft.Office.Interop.Outlook.
Contoh Pemrograman
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);
Menggunakan Aspose.Email
Potongan kode berikut melakukan hal yang sama dengan kode di atas tetapi menggunakan Aspose.Email. Dengan Aspose.Email untuk Java terpasang, Microsoft Outlook tidak lagi diperlukan pada mesin. Cukup referensikan Aspose.Email untuk membangun dan menjalankan proyek dengan sukses.
Contoh Pemrograman
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");
}
}
}