Tạo và Quản lý Tệp PST
Bên cạnh việc phân tích một tệp PST hiện có, Aspose.Email còn cung cấp cách tạo một tệp PST từ đầu. Bài viết này trình bày cách tạo các tệp Outlook PST và thêm các thư mục con hoặc tin nhắn vào chúng.
Creating PST Files
Để tạo một tệp PST mới trên ổ đĩa cục bộ, bạn sẽ cần sử dụng PersonalStorage class. Với lớp này, bạn có thể tạo, đọc và thao tác các tệp PST trong các ứng dụng .NET của mình. Tạo một tệp lưu trữ mới từ đầu chỉ bằng một dòng lệnh:
// Create new PST
using var pst = PersonalStorage.Create(path, FileFormatVersion.Unicode);
Add Sub-folders to PST
Thêm một thư mục con ở gốc tệp PST bằng cách truy cập thư mục Root và sau đó gọi AddSubFolder phương thức.
Đoạn mã sau cho bạn thấy cách thêm một thư mục con có tên Inbox:
// Add new folder "Test"
pst.RootFolder.AddSubFolder("Inbox");
Check the Folder Container Class
Khi tạo các thư mục mới hoặc thêm mục vào các thư mục hiện có, việc đảm bảo rằng lớp container của mục hoặc thư mục mới phù hợp với lớp container của thư mục cha là quan trọng để duy trì cấu trúc tổ chức trong tệp lưu trữ PST. Vì mục đích này, Aspose.Email cung cấp EnforceContainerClassMatching thuộc tính của FolderCreationOptions class. Thuộc tính này xác định liệu có thực thi việc kiểm tra lớp container của thư mục được thêm so với lớp container của thư mục cha hay không. Nếu được đặt thành ’true’, sẽ ném ra một ngoại lệ nếu các lớp container không khớp. Mặc định là ‘false’.
Đoạn mã mẫu dưới đây minh họa việc sử dụng EnforceContainerClassMatching thuộc tính để kiểm soát việc có ném ngoại lệ khi thêm thư mục có lớp container không khớp hay không:
using (var pst = PersonalStorage.Create("storage.pst", FileFormatVersion.Unicode))
{
// Create a standard Contacts folder with the IPF.Contacts container class.
var contacts = pst.CreatePredefinedFolder("Contacts", StandardIpmFolder.Contacts);
// An exception will not arise. EnforceContainerClassMatching is false by default.
contacts.AddSubFolder("Subfolder1", "IPF.Note");
// An exception will occur as the container class of the subfolder being added (IPF.Note)
// does not match the container class of the parent folder (IPF.Contact).
contacts.AddSubFolder("Subfolder3", new FolderCreationOptions {EnforceContainerClassMatching = true, ContainerClass = "IPF.Note"});
}
Lưu ý: Đảm bảo xử lý ngoại lệ đúng cách khi áp dụng việc khớp lớp container để tránh hành vi không mong muốn khi tạo thư mục trong PST.
Thay đổi Lớp Container của Thư mục
Đôi khi cần thay đổi lớp container của thư mục. Một ví dụ phổ biến là khi các tin nhắn thuộc các loại khác nhau (cuộc hẹn, tin nhắn, v.v.) được thêm vào cùng một thư mục. Trong các trường hợp này, lớp thư mục cần được thay đổi cho tất cả các phần tử trong thư mục để hiển thị đúng. Đoạn mã dưới đây cho bạn thấy cách thay đổi lớp container của một thư mục trong PST cho mục đích này.
using var pst = PersonalStorage.FromFile("PersonalStorage1.pst");
var folder = pst.RootFolder.GetSubFolder("Inbox");
folder.ChangeContainerClass("IPF.Note");
Thêm Tệp vào PST
Chức năng chính của Microsoft Outlook là quản lý email, lịch, nhiệm vụ, danh bạ và mục nhập nhật ký. Ngoài ra, các tệp cũng có thể được thêm vào thư mục PST và PST kết quả sẽ ghi lại các tài liệu đã thêm. Aspose.Email cung cấp khả năng thêm tệp vào một thư mục theo cùng cách cùng với việc thêm tin nhắn, danh bạ, nhiệm vụ và mục nhập nhật ký vào PST. Đoạn mã dưới đây cho bạn thấy cách thêm tài liệu vào thư mục PST bằng Aspose.Email.
using (var personalStorage = PersonalStorage.Create(dataDir + "Ps1_out.pst", FileFormatVersion.Unicode))
{
var folder = personalStorage.RootFolder.AddSubFolder("Files");
// Add Document.doc file with the "IPM.Document" message class by default.
folder.AddFile(dataDir + "attachment_1.doc", null);
}
Thêm Tin Nhắn vào Tệp PST
Với Aspose.Email bạn có thể thêm tin nhắn vào các thư mục con của tệp PST mà bạn đã tạo hoặc tải. Bài viết này thêm hai tin nhắn từ đĩa vào thư mục con Inbox của một PST. Sử dụng PersonalStorage và FolderInfo các lớp để thêm tin nhắn vào tệp PST. Để thêm tin nhắn vào thư mục Inbox của tệp PST:
- Tạo một thể hiện của lớp FolderInfo và tải nó với nội dung của thư mục Inbox.
- Thêm tin nhắn từ đĩa vào thư mục Inbox bằng cách gọi FolderInfo.AddMessage() phương thức. Thuộc tính FolderInfo lớp này cung cấp AddMessages phương thức cho phép thêm số lượng lớn tin nhắn vào thư mục, giảm các thao tác I/O tới đĩa và cải thiện hiệu năng.
Đoạn mã dưới đây cho bạn thấy cách thêm tin nhắn vào thư mục con PST có tên Inbox.
// Create new PST
var personalStorage = PersonalStorage.Create(dataDir, FileFormatVersion.Unicode);
// Add new folder "Inbox"
personalStorage.RootFolder.AddSubFolder("Inbox");
// Select the "Inbox" folder
var inboxFolder = personalStorage.RootFolder.GetSubFolder("Inbox");
// Add some messages to "Inbox" folder
inboxFolder.AddMessage(MapiMessage.FromFile(RunExamples.GetDataDir_Outlook() + "MapiMsgWithPoll.msg"));
Thêm Tin nhắn Hàng Loạt với Hiệu Suất Cải Thiện
Thêm từng tin nhắn riêng lẻ vào PST đồng nghĩa với việc thực hiện nhiều thao tác I/O tới đĩa hơn và có thể làm chậm hiệu năng. Để cải thiện hiệu năng, các tin nhắn có thể được thêm vào PST ở chế độ bulk để giảm thiểu các thao tác I/O. The AddMessages phương thức cho phép bạn thêm tin nhắn hàng loạt và có thể được sử dụng trong các kịch bản sau. Ngoài ra, MessageAdded sự kiện xảy ra khi một tin nhắn được thêm vào thư mục.
Thêm Tin nhắn từ PST Khác
Để thêm tin nhắn từ một PST khác, sử dụng FolderInfo.EnumerateMapiMessages phương thức trả về IEnumerable<MapiMessage>:
using var srcPst = PersonalStorage.FromFile(@"source.pst", false);
using var destPst = PersonalStorage.FromFile(@"destination.pst");
// Get the folder by name
var srcFolder = srcPst.RootFolder.GetSubFolder("SomeFolder");
var destFolder = destPst.RootFolder.GetSubFolder("SomeFolder");
destFolder.MessageAdded += new MessageAddedEventHandler(OnMessageAdded);
destFolder.AddMessages(srcFolder.EnumerateMapiMessages());
// Handles the MessageAdded event.
static void OnMessageAdded(object sender, MessageAddedEventArgs e)
{
Console.WriteLine($"Added: {e.EntryId}");
}
Thêm Tin nhắn từ Thư mục
Để thêm tin nhắn từ thư mục, tạo GetMessages(string pathToDir) phương thức iterator có tên trả về IEnumerable<MapiMessage>:
using var pst = PersonalStorage.FromFile(@"storage.pst");
var folder = pst.RootFolder.GetSubFolder("SomeFolder");
folder.MessageAdded += OnMessageAdded;
folder.AddMessages(GetMessages(@"MessageDirectory"));
// Named iterator method to read messages from directory.
static IEnumerable<MapiMessage> GetMessages(string pathToDir)
{
string[] files = Directory.GetFiles(pathToDir, "*.msg");
foreach (var file in files)
{
yield return MapiMessage.Load(file);
}
}
// Handles the MessageAdded event.
static void OnMessageAdded(object sender, MessageAddedEventArgs e)
{
Console.WriteLine($"Added: {e.EntryId}");
}
Tải Tin nhắn từ Đĩa
Đoạn mã dưới đây cho bạn thấy cách tải tin nhắn từ đĩa.
private static void AddMessagesInBulkMode(string fileName, string msgFolderName)
{
using (PersonalStorage personalStorage = PersonalStorage.FromFile(fileName))
{
FolderInfo folder = personalStorage.RootFolder.GetSubFolder("myInbox");
folder.MessageAdded += OnMessageAdded;
folder.AddMessages(new MapiMessageCollection(msgFolderName));
}
}
static void OnMessageAdded(object sender, MessageAddedEventArgs e)
{
Console.WriteLine(e.EntryId);
Console.WriteLine(e.Message.Subject);
}
Triển khai IEnumerable
Đoạn mã dưới đây cho bạn thấy cách sử dụng triển khai IEnumerable.
public class MapiMessageCollection : IEnumerable<MapiMessage>
{
private string path;
public MapiMessageCollection(string path)
{
this.path = path;
}
public IEnumerator<MapiMessage> GetEnumerator()
{
return new MapiMessageEnumerator(path);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class MapiMessageEnumerator : IEnumerator<MapiMessage>
{
private readonly string[] files;
private int position = -1;
public MapiMessageEnumerator(string path)
{
string path1 = RunExamples.GetDataDir_Outlook();
files = Directory.GetFiles(path1);
}
public bool MoveNext()
{
position++;
return (position < files.Length);
}
public void Reset()
{
position = -1;
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public MapiMessage Current
{
get
{
try
{
return MapiMessage.FromFile(files[position]);
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
public void Dispose()
{
}
}