การสร้างและจัดการไฟล์ PST

นอกจากการวิเคราะห์ไฟล์ PST ที่มีอยู่แล้ว Aspose.Email ยังให้วิธีการสร้างไฟล์ PST ตั้งแต่ต้น บทความนี้แสดงวิธีสร้างไฟล์ Outlook PST และเพิ่มโฟลเดอร์ย่อยหรือข้อความเข้าไป

สร้างไฟล์ PST

เพื่อสร้างไฟล์ PST ใหม่บนดิสก์ท้องถิ่น คุณจำเป็นต้องใช้ PersonalStorage class. ด้วยคลาสนี้ คุณสามารถสร้าง อ่าน และจัดการไฟล์ PST ในแอปพลิเคชัน .NET ของคุณ สร้างไฟล์จัดเก็บจากศูนย์ด้วยบรรทัดโค้ดเดียว:

// Create new PST
using var pst = PersonalStorage.Create(path, FileFormatVersion.Unicode);

เพิ่มโฟลเดอร์ย่อยลงใน PST

เพิ่มโฟลเดอร์ย่อยที่รากของไฟล์ PST โดยเข้าถึงโฟลเดอร์ Root แล้วเรียก AddSubFolder เมธอด.

โค้ดสแนปต่อไปนี้แสดงวิธีเพิ่มโฟลเดอร์ย่อยชื่อ Inbox:

// Add new folder "Test"
pst.RootFolder.AddSubFolder("Inbox");

ตรวจสอบคลาสคอนเทนเนอร์ของโฟลเดอร์

เมื่อสร้างโฟลเดอร์ใหม่หรือเพิ่มรายการลงในโฟลเดอร์ที่มีอยู่ จำเป็นต้องตรวจสอบให้แน่ใจว่าคลาสคอนเทนเนอร์ของรายการหรือโฟลเดอร์ใหม่สอดคล้องกับคลาสคอนเทนเนอร์ของโฟลเดอร์แม่ เพื่อรักษาโครงสร้างลำดับองค์กรภายในไฟล์จัดเก็บ PST เพื่อวัตถุประสงค์นี้ Aspose.Email มี EnforceContainerClassMatching คุณสมบัติของ FolderCreationOptions class. คุณสมบัตินี้ระบุว่าจะบังคับตรวจสอบคลาสคอนเทนเนอร์ของโฟลเดอร์ที่กำลังเพิ่มเทียบกับคลาสคอนเทนเนอร์ของโฟลเดอร์แม่หรือไม่ หากตั้งค่าเป็น ’true’ จะเกิดข้อยกเว้นหากคลาสคอนเทนเนอร์ไม่ตรงกัน ค่าเริ่มต้นคือ ‘false’.

ตัวอย่างโค้ดต่อไปนี้สาธิตการใช้ EnforceContainerClassMatching คุณสมบัติเพื่อควบคุมว่าควรโยนข้อยกเว้นหรือไม่เมื่อเพิ่มโฟลเดอร์ที่มีคลาสคอนเทนเนอร์ไม่ตรงกัน:

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"});
}

หมายเหตุ: ตรวจสอบการจัดการข้อยกเว้นอย่างเหมาะสมเมื่อบังคับให้คลาสคอนเทนเนอร์ตรงกันเพื่อป้องกันพฤติกรรมที่ไม่คาดคิดระหว่างการสร้างโฟลเดอร์ใน PST.

เปลี่ยนคลาสคอนเทนเนอร์ของโฟลเดอร์

บางครั้งอาจจำเป็นต้องเปลี่ยนคลาสคอนเทนเนอร์ของโฟลเดอร์ ตัวอย่างทั่วไปคือการเพิ่มข้อความประเภทต่าง ๆ (นัดหมาย, ข้อความ ฯลฯ) ลงในโฟลเดอร์เดียวกัน ในกรณีเช่นนี้ จำเป็นต้องเปลี่ยนคลาสของโฟลเดอร์สำหรับทุกองค์ประกอบเพื่อให้แสดงผลอย่างถูกต้อง โค้ดตัวอย่างต่อไปนี้แสดงวิธีเปลี่ยนคลาสคอนเทนเนอร์ของโฟลเดอร์ใน PST เพื่อวัตถุประสงค์นี้.

using var pst = PersonalStorage.FromFile("PersonalStorage1.pst);
var folder = pst.RootFolder.GetSubFolder("Inbox");

folder.ChangeContainerClass("IPF.Note");

การเพิ่มไฟล์ไปยัง PST

ฟังก์ชันหลักของ Microsoft Outlook คือการจัดการอีเมล, ปฏิทิน, งาน, รายชื่อผู้ติดต่อและบันทึกบันทึก อีกทั้งไฟล์ยังสามารถเพิ่มลงในโฟลเดอร์ PST ได้และ PST ที่ได้จะบันทึกเอกสารที่เพิ่มไว้ Aspose.Email มีความสามารถในการเพิ่มไฟล์ลงในโฟลเดอร์ในลักษณะเดียวกันพร้อมกับการเพิ่มข้อความ, รายชื่อผู้ติดต่อ, งานและบันทึกบันทึก โค้ดตัวอย่างต่อไปนี้แสดงวิธีเพิ่มเอกสารไปยังโฟลเดอร์ PST ด้วย Aspose.Email.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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);
}

การเพิ่มข้อความไปยังไฟล์ PST

ด้วย Aspose.Email คุณสามารถเพิ่มข้อความไปยังโฟลเดอร์ย่อยของไฟล์ PST ที่คุณสร้างหรือโหลด บทความนี้เพิ่มสองข้อความจากดิสก์ไปยังโฟลเดอร์ย่อย Inbox ของ PST ใช้ PersonalStorage และ FolderInfo คลาสสำหรับเพิ่มข้อความไปยังไฟล์ PST. เพื่อเพิ่มข้อความไปยังโฟลเดอร์ Inbox ของไฟล์ PST:

  1. สร้างอินสแตนซ์ของคลาส FolderInfo และโหลดด้วยเนื้อหาของโฟลเดอร์ Inbox.
  2. เพิ่มข้อความจากดิสก์ไปยังโฟลเดอร์ Inbox โดยเรียก FolderInfo.AddMessage() เมธอด. The FolderInfo คลาสเปิดเผย AddMessages เมธอดที่ช่วยให้เพิ่มข้อความจำนวนมากไปยังโฟลเดอร์ ลดการดำเนินการ I/O ไปยังดิสก์และปรับปรุงประสิทธิภาพ.

โค้ดตัวอย่างด้านล่างแสดงวิธีเพิ่มข้อความไปยังโฟลเดอร์ย่อยของ PST ที่ชื่อ Inbox.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// 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"));

เพิ่มข้อความเป็นกลุ่มด้วยประสิทธิภาพที่ดีกว่า

การเพิ่มข้อความแต่ละรายการลงใน PST หมายถึงการทำ I/O เพิ่มขึ้นบนดิสก์และอาจทำให้ประสิทธิภาพช้าลง เพื่อปรับปรุงประสิทธิภาพ สามารถเพิ่มข้อความลงใน PST แบบกลุ่มเพื่อให้การดำเนินการ I/O น้อยที่สุด AddMessages เมธอดนี้อนุญาตให้คุณเพิ่มข้อความเป็นกลุ่มและสามารถใช้ในสถานการณ์ต่อไปนี้ นอกจากนี้, MessageAdded เหตุการณ์เกิดขึ้นเมื่อมีข้อความถูกเพิ่มเข้าไปในโฟลเดอร์.

เพิ่มข้อความจาก PST อื่น

เพื่อเพิ่มข้อความจาก PST อื่น ให้ใช้ FolderInfo.EnumerateMapiMessages เมธอดที่คืนค่า 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}");
}

เพิ่มข้อความจากไดเรกทอรี

เพื่อเพิ่มข้อความจากไดเรกทอรี ให้สร้าง GetMessages(string pathToDir) เมธอด iterator ชื่อที่คืนค่า 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}");
}

โหลดข้อความจากดิสก์

โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดข้อความจากดิสก์.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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);
}

IEnumerable Implementation

โค้ดตัวอย่างต่อไปนี้แสดงวิธีใช้ IEnumerable Implementation.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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()
    {
    }
}