อ่านไฟล์ PST และดึงข้อมูล
อ่านไฟล์ PST และดึงข้อมูล
Aspose.Email สำหรับ .NET มี API สำหรับอ่านไฟล์ Microsoft Outlook PST. คุณสามารถโหลดไฟล์ PST จากดิสก์หรือสตรีมเข้าไปยังอินสแตนซ์ของ PersonalStorage คลาสและดึงข้อมูลเกี่ยวกับเนื้อหาเช่น โฟลเดอร์, โฟลเดอร์ย่อย, และข้อความ API ยังให้ความสามารถในการรวมโฟลเดอร์ค้นหาเมื่อตรวจสอบข้อความจากโฟลเดอร์ PST.
โหลดไฟล์ PST
ไฟล์ Outlook PST สามารถโหลดในอินสแตนซ์ของ PersonalStorage คลาส. โค้ดตัวอย่างต่อไปนี้จะแสดงวิธีโหลดไฟล์ PST.
// Load the Outlook PST file
PersonalStorage personalStorage = PersonalStorage.FromFile(dataDir + @"PersonalStorage.pst");
แสดงข้อมูลโฟลเดอร์
หลังจากโหลดไฟล์ PST ใน PersonalStorage คลาส, คุณสามารถดึงข้อมูลเกี่ยวกับชื่อไฟล์ที่แสดง, โฟลเดอร์ราก, โฟลเดอร์ย่อยและจำนวนข้อความได้ โค้ดสแนปต่อไปนี้แสดงวิธีแสดงชื่อไฟล์ PST, โฟลเดอร์และจำนวนข้อความในโฟลเดอร์.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
string dst = dataDir + "PersonalStorage.pst";
// load PST file
PersonalStorage personalStorage = PersonalStorage.FromFile(dst);
// Get the folders information
FolderInfoCollection folderInfoCollection = personalStorage.RootFolder.GetSubFolders();
// Browse through each folder to display folder name and number of messages
foreach (FolderInfo folderInfo in folderInfoCollection)
{
Console.WriteLine("Folder: " + folderInfo.DisplayName);
Console.WriteLine("Total items: " + folderInfo.ContentCount);
Console.WriteLine("Total unread items: " + folderInfo.ContentUnreadCount);
Console.WriteLine("-----------------------------------");
}
ดึงเฉพาะโฟลเดอร์ที่ผู้กำหนดเอง
ไฟล์ PST/OST อาจมีโฟลเดอร์ที่สร้างโดยผู้ใช้ Aspose.Email ให้ความสามารถในการเข้าถึงเฉพาะโฟลเดอร์ที่ผู้กำหนดโดยใช้ PersonalStorageQueryBuilder.OnlyFoldersCreatedByUser คุณสมบัติ คุณสามารถตั้งค่า PersonalStorageQueryBuilder.OnlyFoldersCreatedByUser ตั้งค่าคุณสมบัติเป็น true เพื่อรับเฉพาะโฟลเดอร์ที่ผู้ใช้กำหนดเอง โค้ดสแนปชอตต่อไปนี้แสดงการใช้ PersonalStorageQueryBuilder.OnlyFoldersCreatedByUser เพื่อรับโฟลเดอร์ที่ผู้ใช้กำหนดเอง.
using (PersonalStorage pst = PersonalStorage.FromFile(dataDir + "Outlook.pst"))
{
PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder();
queryBuilder.OnlyFoldersCreatedByUser.Equals(true);
FolderInfoCollection subfolders = pst.RootFolder.GetSubFolders(queryBuilder.GetQuery());
foreach (FolderInfo folder in subfolders)
{
Console.WriteLine(folder.DisplayName);
}
}
ตรวจสอบว่าโฟลเดอร์เป็นแบบกำหนดล่วงหน้าหรือไม่
เมื่อเปิดและตรวจสอบโฟลเดอร์ภายในไฟล์ PST (Personal Storage Table) คุณสามารถตรวจสอบว่าแต่ละโฟลเดอร์เป็นประเภทโฟลเดอร์ที่กำหนดไว้ล่วงหน้าหรือเป็นโฟลเดอร์ย่อยของประเภทโฟลเดอร์ที่กำหนดไว้ล่วงหน้า และรับข้อมูลเกี่ยวกับแต่ละโฟลเดอร์ได้.
นี้ FolderInfo.GetPredefinedTypeเมธอด (bool getForTopLevelParent) ช่วยตรวจสอบว่าโฟลเดอร์มาจาก StandardIpmFolder. หากพารามิเตอร์ getForTopLevelParent เป็น true เมธอดจะคืนค่า StandardIpmFolder ค่าของ enum สำหรับโฟลเดอร์แม่ระดับบนสุด ซึ่งกำหนดว่าโฟลเดอร์ปัจจุบันเป็นโฟลเดอร์ย่อยของโฟลเดอร์ที่กำหนดล่วงหน้าหรือไม่ หากพารามิเตอร์ getForTopLevelParent เป็น false จะคืนค่า StandardIpmFolder ค่าของ enum สำหรับโฟลเดอร์ปัจจุบัน.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีนำคุณลักษณะนี้ไปใช้ในโปรเจกต์ของคุณ:
using (PersonalStorage pst = PersonalStorage.FromFile("my.pst"))
{
FolderInfoCollection folders = pst.RootFolder.GetSubFolders();
foreach (FolderInfo folder in folders)
{
Console.WriteLine($"Folder: {folder.DisplayName}");
Console.WriteLine($"Is predefined: {folder.GetPredefinedType(false) != StandardIpmFolder.Unspecified}");
Console.WriteLine("-----------------------------------");
}
}
ดึงข้อมูลโฟลเดอร์แม่
โค้ดสแนปชอตต่อไปนี้แสดงวิธีดึงข้อมูลโฟลเดอร์แม่จาก MessageInfo.
using (PersonalStorage pst = PersonalStorage.FromFile(fileName))
{
foreach (FolderInfo folder in pst.RootFolder.GetSubFolders())
{
foreach (MessageInfo msg in folder.EnumerateMessages())
{
FolderInfo folderInfo = pst.GetParentFolder(msg.EntryId);
}
}
}
ดึงโฟลเดอร์ย่อยตามเส้นทาง
นี้ FolderInfo.GetSubFolder(string name, bool ignoreCase, bool handlePathSeparator) การโอเวอร์โหลดเมธอดจะทำให้คุณสามารถดึงโฟลเดอร์ย่อยที่มีชื่อที่ระบุจากโฟลเดอร์ PST ปัจจุบันได้
เมธอดรับพารามิเตอร์ต่อไปนี้:
- name - ระบุชื่อของโฟลเดอร์ย่อยที่ต้องการดึงข้อมูล
- ignoreCase - กำหนดว่าการค้นหาชื่อโฟลเดอร์ย่อยจะต้องแยกแยะตัวพิมพ์ใหญ่-เล็กหรือไม่ หากตั้งค่าเป็น true การค้นหาจะไม่แยกแยะตัวพิมพ์ แต่ถ้าเป็น false จะต้องแยกแยะตัวพิมพ์
- handlePathSeparator - ระบุว่าจะต้องปฏิบัติชื่อโฟลเดอร์ที่ระบุว่าเป็นเส้นทางหรือไม่หากมีเครื่องหมาย backslash หากตั้งค่าเป็น true เมธอดจะตีความชื่อโฟลเดอร์เป็นเส้นทางและพยายามไปยังโฟลเดอร์ย่อยโดยใช้เส้นทาง หากตั้งค่าเป็น false เมธอดจะถือชื่อโฟลเดอร์เป็นชื่อธรรมดาและค้นหาโฟลเดอร์ย่อยที่ตรงกับชื่ออย่างแม่นยำ
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการนำเมธอดนี้ไปใช้ในโครงการของคุณ:
var folder = pst.RootFolder.GetSubFolder(@"Inbox\Reports\Jan", true, true);
ในตัวอย่างนี้ วิธีจะคืนค่าโฟลเดอร์ชื่อ ‘Jan’ ที่อยู่ที่ Inbox\Reports\ เส้นทางสัมพันธ์กับโฟลเดอร์ราก.
ฟีด RSS และโฟลเดอร์ที่สามารถค้นหาได้
โฟลเดอร์ RSS Feeds มาตรฐานใน PersonalStorage
Aspose.Email ทำให้สามารถดึงอ้างอิงไปยังโฟลเดอร์ที่กำหนดล่วงหน้าที่เก็บ RSS feeds ได้ ซึ่งอาจมีประโยชน์หากคุณต้องการเข้าถึงและจัดการ RSS feeds ที่จัดเก็บในไฟล์ Outlook PST อย่างอัตโนมัติ ให้ค่าของ RssFeeds แก่ StandardIpmFolder enum.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับโฟลเดอร์ RSS Feeds.
using (var pst = PersonalStorage.FromFile("my.pst", false))
{
var rssFolder = pst.GetPredefinedFolder(StandardIpmFolder.RssFeeds);
}
เพื่อเพิ่มโฟลเดอร์ RSS Feeds ให้ใช้โค้ดสแนปต่อไปนี้:
using (var pst = PersonalStorage.Create("my.pst", FileFormatVersion.Unicode))
{
var rssFolder = pst.CreatePredefinedFolder("RSS Feeds", StandardIpmFolder.RssFeeds);
}
การแยกวิเคราะห์โฟลเดอร์ที่ค้นหาได้
ไฟล์ PST/OST อาจมีโฟลเดอร์ที่สามารถค้นหาได้เพิ่มเติมจากประเภทโฟลเดอร์ปกติ Aspose.Email ให้ FolderKind enumerator สำหรับระบุข้อความจากโฟลเดอร์การค้นหาเหล่านี้ด้วย EnumerateFolders และ GetSubFolders เมธอด. โค้ดสแนปต่อไปนี้แสดงวิธีแยกโฟลเดอร์ที่สามารถค้นหาได้.
using (PersonalStorage pst = PersonalStorage.FromFile("my.pst"))
{
FolderInfoCollection folders = pst.RootFolder.GetSubFolders(FolderKind.Search | FolderKind.Normal);
// Browse through each folder to display folder name and number of messages
foreach (FolderInfo folder in folders)
{
Console.WriteLine($"Folder: {folder.DisplayName}");
FolderInfoCollection subFolders = folder.GetSubFolders(FolderKind.Search | FolderKind.Normal);
foreach (FolderInfo subFolder in subFolders)
{
Console.WriteLine($"Sub-folder: {subFolder.DisplayName}");
}
}
}
API การสำรวจไฟล์ PST
API การเดินสำรวจช่วยให้ดึงรายการ PST ทั้งหมดให้ได้มากที่สุดโดยไม่ทำให้เกิดข้อยกเว้น แม้ข้อมูลบางส่วนของไฟล์ต้นฉบับจะเสียหาย ขั้นตอนต่อไปนี้แสดงวิธีใช้ API นี้.
ใช้ PersonalStorage คอนสตรัคเตอร์และ Load เมธอดแทน FromFile method.
คอนสตรัคเตอร์อนุญาตให้กำหนดเมธอด callback.
using (var currentPst = new PersonalStorage((exception, itemId) => { /* Exception handling code. */ }))
ข้อยกเว้นที่เกิดจากการโหลดและการเดินสำรวจจะพร้อมใช้งานผ่านเมธอด callback.
นี้ Load เมธอดคืนค่า ’true’ หากไฟล์โหลดสำเร็จและสามารถเดินสำรวจต่อได้ หากไฟล์เสียหายและไม่สามารถเดินสำรวจได้ จะคืนค่า ‘false’.
if (currentPst.Load(inputStream))
นี่ทำให้สามารถเปิดและสำรวจไฟล์ PST ที่เสียหายได้โดยไม่โยนข้อยกเว้นออกมา และข้อยกเว้นและรายการที่เสียหายจะถูกจัดการโดยเมธอด callback.
using (PersonalStorage pst = new PersonalStorage((exception, itemId) => { /* Exception handling code. */ }))
{
if (pst.Load(@"test.pst"))
{
GetAllMessages(pst, pst.RootFolder);
}
}
private static void GetAllMessages(PersonalStorage pst, FolderInfo folder)
{
foreach (var messageEntryId in folder.EnumerateMessagesEntryId())
{
MapiMessage message = pst.ExtractMessage(messageEntryId);
}
foreach (var subFolder in folder.GetSubFolders())
{
GetAllMessages(pst, subFolder);
}
}
ดึงสีของหมวดหมู่รายการจากไฟล์ PST
Aspose.Email อนุญาตให้ผู้ใช้ดึงข้อมูลหมวดหมู่ รวมถึงชื่อและสี จากไฟล์ PST ข้อมูลหมวดหมู่สามารถดึงและจับคู่กับรายการ PST แต่ละรายการได้ ใช้สมาชิก API ต่อไปนี้:
- OutlookCategoryColor enum - แสดงสีที่เชื่อมกับหมวดหมู่.
- PstItemCategory คลาส - ให้พร็อพเพอร์ตี้สำหรับชื่อและสีของหมวดหมู่.
- GetCategories เมธอด - ดึงรายการหมวดหมู่พร้อมชื่อและสีของมัน.
ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีดึงหมวดหมู่ทั้งหมดที่มีจากไฟล์ PST และจับคู่ที่เชื่อมกับข้อความอีเมลเฉพาะ.
ดึงหมวดหมู่ที่มีจาก PST
using (var pst = PersonalStorage.FromFile("mailbox.pst"))
{
var categories = pst.GetCategories();
foreach(var category in categories)
{
Console.WriteLine(category);
}
}
จับคู่ชื่อหมวดหมู่กับสีของมัน
using (var pst = PersonalStorage.FromFile("mailbox.pst"))
{
// Get all categories from the PST
var availableCategories = pst.GetCategories();
// Extract a message from the PST and retrieve the list of category names for the message
var messageCategoryList = FollowUpManager.GetCategories(pst.ExtractMessage(messageInfo));
// Iterate through each category in the message and match it with the PST category list
foreach (var messageCategory in messageCategoryList)
{
var category = availableCategories.Find(c => c.Name.Equals(messageCategory, StringComparison.OrdinalIgnoreCase));
if (category != null)
{
// Print the category name and its associated color
Console.WriteLine(category);
}
else
{
Console.WriteLine($"Category: {messageCategory}, Color: Not found");
}
}
}