การทำงานกับไฟล์ PST ขนาดใหญ่

การประมวลผลไฟล์ Personal Storage Table (PST) ขนาดใหญ่สามารถลดประสิทธิภาพและเพิ่มการใช้หน่วยความจำได้ Aspose.Email สำหรับ Java มีเทคนิคและ API หลายอย่างที่ช่วยให้นักพัฒนาสามารถเข้าถึง, ประมวลผล, และจัดการข้อมูลเมลบ็อกซ์อย่างมีประสิทธิภาพโดยไม่ทำให้ระบบทำงานหนักเกินไป

ใช้เมธอด Iterable สำหรับการสำรวจโฟลเดอร์และข้อความ

เมื่อทำการวนซ้ำผ่านโฟลเดอร์และข้อความ, ควรเลือกใช้วิธีการที่คืนค่า Iterable. นี้ช่วยลดการใช้หน่วยความจำและปรับปรุงประสิทธิภาพ.

try (PersonalStorage pst = PersonalStorage.fromFile("storage.pst")) {
    for (FolderInfo folder : pst.getRootFolder().enumerateFolders()) {
        for (MessageInfo messageInfo : folder.enumerateMessages()) {
            // Process message
        }
    }
}

ใช้ MessageInfo สำหรับคุณสมบัติข้อความพื้นฐาน

หากคุณต้องการเพียงรายละเอียดข้อความพื้นฐานเท่านั้น, ให้ใช้ MessageInfo แทนที่จะโหลดข้อความเต็ม วิธีนี้เร็วกว่าและใช้หน่วยความจำน้อยกว่า

for (MessageInfo messageInfo : folder.enumerateMessages()) {
    System.out.println("Subject: " + messageInfo.getSubject());
    System.out.println("To: " + messageInfo.getDisplayTo());
    System.out.println("Importance: " + messageInfo.getImportance());
    System.out.println("Message Class: " + messageInfo.getMessageClass());
}

หลีกเลี่ยงการดึงข้อความเต็มหากไม่จำเป็น

อย่าใช้ ExtractMessage หรือ EnumerateMapiMessages วิธีการสำหรับข้อความทั้งหมด เว้นแต่คุณต้องการคุณสมบัติทั้งหมด

ในทางกลับกัน ใหพิจารณา:

a) ดึง ID ข้อความ

ใช้ enumerateMessagesEntryId เพื่อดึง ID ของข้อความทั้งหมดในโฟลเดอร์ จากนั้นคุณสามารถเลือกดึงคุณสมบัติ, ข้อความ หรือไฟล์แนบตามต้องการ

for (String id : folder.enumerateMessagesEntryId()) {
    // Retrieve a property using ID (extractProperty),
    // Extract a MapiMessage (extractMessage),
    // Extract message attachments (extractAttachments),
    // Save message to a stream (saveMessageToStream).
}

b) ดึงคุณสมบัติเดียว

ใช้ ExtractProperty ถ้าคุณต้องการเฉพาะคุณสมบัติเฉพาะที่หายไปใน MessageInfo.

for (String msgId : folder.enumerateMessagesEntryId()) {
    String transportMessageHeaders =
        pst.extractProperty(
            org.apache.commons.codec.binary.Base64.decodeBase64(msgId),
            KnownPropertyList.TRANSPORT_MESSAGE_HEADERS.getTag()
        ).getString();
}

c) ดึงเฉพาะไฟล์แนบ

หากต้องการเฉพาะไฟล์แนบ ให้ใช้ ExtractAttachments.

for (String msgId : folder.enumerateMessagesEntryId()) {
    MapiAttachmentCollection attachments = pst.extractAttachments(msgId);
}

ใช้เงื่อนไขการค้นหาเพื่อกรองข้อความ

กรองข้อความด้วย เงื่อนไขการค้นหา ลดจำนวนข้อความที่โหลดและปรับปรุงประสิทธิภาพ

try (PersonalStorage pst = PersonalStorage.fromFile("storage.pst")) {

    PersonalStorageQueryBuilder builder = new PersonalStorageQueryBuilder();
    // Unread messages
    builder.hasNoFlags(MapiMessageFlags.MSGFLAG_READ);

    for (FolderInfo folder : pst.getRootFolder().enumerateFolders()) {
        MessageInfoCollection unread = folder.getContents(builder.getQuery());
    }
}

บันทึกข้อความลงสตรีม

แทนที่จะดึงและบันทึกข้อความทีละรายการ ให้ใช้ SaveMessageToStream เพื่อประสิทธิภาพที่ดีกว่า.

for (String id : folder.enumerateMessagesEntryId()) {
    try (OutputStream fos  = new FileOutputStream("message.msg")) {
        pst.saveMessageToStream(id, fos);
    }
}

ใช้วิธีการแบบจำนวนมากเมื่อเป็นไปได้

เมื่อใดก็ตามที่คุณต้องการ เพิ่ม หรือ ลบ หลายรายการ แนะนำให้ใช้วิธีการแบบจำนวนมากแทนการดำเนินการแต่ละรายการ ซึ่งลดภาระและปรับปรุงประสิทธิภาพ