การทำงานกับข้อความในไฟล์ PST

Contents
[ ]

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

สร้างไฟล์ PST ใหม่และเพิ่มโฟลเดอร์ย่อย แสดงวิธีสร้างไฟล์ 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-Java

// Create new PST
PersonalStorage personalStorage = PersonalStorage.create(dataDir, FileFormatVersion.Unicode);

// Add new folder "Inbox"
personalStorage.getRootFolder().addSubFolder("Inbox");

// Select the "Inbox" folder
FolderInfo inboxFolder = personalStorage.getRootFolder().getSubFolder("Inbox");

// Add some messages to "Inbox" folder
inboxFolder.addMessage(MapiMessage.fromFile(dataDir + "MapiMsgWithPoll.msg"));

การเพิ่มข้อความเป็นกลุ่ม

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
private static void addMessagesInBulkMode(String fileName, String msgFolderName) {
    try (PersonalStorage personalStorage = PersonalStorage.fromFile(fileName)) {
        FolderInfo folder = personalStorage.getRootFolder().getSubFolder("myInbox");
        folder.MessageAdded.add(new MessageAddedEventHandler() {
            public void invoke(Object sender, MessageAddedEventArgs e) {
                onMessageAdded(sender, e);
            }
        });
        folder.addMessages(new MapiMessageCollection(msgFolderName));
    }
}

static void onMessageAdded(Object sender, MessageAddedEventArgs e) {
    System.out.println(e.getEntryId());
    System.out.println(e.getMessage().getSubject());
}

การทำงานของ Iterable

โค้ดตัวอย่างต่อไปนี้แสดงวิธีสร้างการทำงานของ Iterable Implementation.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public class MapiMessageCollection implements Iterable<MapiMessage> {

    private final File folder;

    public MapiMessageCollection(String folder) {
        this.folder = new File(folder);
    }

    public Iterator<MapiMessage> iterator() {
        return new MapiMessageIterator(folder.listFiles());
    }
}

public class MapiMessageIterator implements Iterator<MapiMessage> {

    private Queue<String> queue = new LinkedList<String>();

    public MapiMessageIterator(File[] listOfFiles) {
        for (File file : listOfFiles) {
            queue.offer(file.getAbsolutePath());
        }
    }

    public boolean hasNext() {
        return !queue.isEmpty();
    }

    public MapiMessage next() {
        return MapiMessage.fromFile(queue.poll());
    }
}

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

สำหรับการเพิ่มข้อความจาก PST อื่น ใช้ FolderInfo.enumerateMapiMessages() เมธอดที่คืนค่า Iterable. ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเพิ่มข้อความจาก PST อื่น.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
private static void bulkAddFromAnotherPst(String source) {
    // The path to the File directory.
    String dataDir = "data/";

    try (PersonalStorage pst = PersonalStorage.fromFile(source, false)) {
        try (PersonalStorage pstDest = PersonalStorage.fromFile(dataDir + "PersonalStorageFile1.pst")) {
            // Get the folder by name
            FolderInfo folderInfo = pst.getRootFolder().getSubFolder("Contacts");
            MessageInfoCollection ms = folderInfo.getContents();

            // Get the folder by name
            FolderInfo f = pstDest.getRootFolder().getSubFolder("myInbox");
            f.MessageAdded.add(new MessageAddedEventHandler() {
                public void invoke(Object sender, MessageAddedEventArgs e) {
                    onMessageAdded(sender, e);
                }
            });
            f.addMessages(folderInfo.enumerateMapiMessages());
            FolderInfo fi = pstDest.getRootFolder().getSubFolder("myInbox");
            MessageInfoCollection msgs = fi.getContents();
        }
    }
}

// Handles the MessageAdded event.
static void onMessageAdded(Object sender, MessageAddedEventArgs e) {
    System.out.println(e.getEntryId());
    System.out.println(e.getMessage().getSubject());
}

รับข้อมูลข้อความจากไฟล์ Outlook PST

ใน อ่านไฟล์ Outlook PST และรับข้อมูลโฟลเดอร์และโฟลเดอร์ย่อย, เราได้กล่าวถึงการโหลดไฟล์ Outlook PST และท่องดูโฟลเดอร์เพื่อรับชื่อโฟลเดอร์และจำนวนข้อความในนั้น บทความนี้อธิบายวิธีการอ่านโฟลเดอร์และโฟลเดอร์ย่อยทั้งหมดในไฟล์ PST และแสดงข้อมูลเกี่ยวกับข้อความ เช่น หัวเรื่อง, ผู้ส่ง, และผู้รับ. The FolderInfo.getContents() เมธอดใช้เพื่อแสดง ข้อมูลข้อความสรุป เช่น หัวเรื่อง, ผู้ส่ง, ผู้รับ. ด้านประสิทธิภาพ นี่เป็นตัวเลือกที่เหมาะสมที่สุดสำหรับการรับข้อมูลหลักของข้อความ. เพื่อ สกัด ข้อมูลข้อความทั้งหมด, the PersonalStorage.extractMessage() มีเมธอดให้ใช้ไฟล์ Outlook PST อาจมีโฟลเดอร์ซ้อน เพื่อดึงข้อมูลข้อความจากโฟลเดอร์เหล่านี้และโฟลเดอร์ระดับบนสุด ใช้วิธีการแบบ recursive เพื่ออ่านทุกโฟลเดอร์ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีอ่านไฟล์ Outlook PST และแสดงเนื้อหาโฟลเดอร์และข้อความแบบเรียกซ้ำ.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public static void run() {
    // The path to the File directory.
    String dataDir = "data/";

    // Load the Outlook file
    String path = dataDir + "PersonalStorage.pst";

    try {

        // Load the Outlook PST file
        PersonalStorage personalStorage = PersonalStorage.fromFile(path);

        // Get the Display Format of the PST file
        System.out.println("Display Format: " + personalStorage.getFormat());

        // Get the folders and messages information
        FolderInfo folderInfo = personalStorage.getRootFolder();

        // Call the recursive method to display the folder contents
        displayFolderContents(folderInfo, personalStorage);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

// This is a recursive method to display contents of a folder
private static void displayFolderContents(FolderInfo folderInfo, PersonalStorage pst) {
    // Display the folder name
    System.out.println("Folder: " + folderInfo.getDisplayName());
    System.out.println("==================================");
    // Display information about messages inside this folder
    MessageInfoCollection messageInfoCollection = folderInfo.getContents();
    for (MessageInfo messageInfo : messageInfoCollection) {
        System.out.println("Subject: " + messageInfo.getSubject());
        System.out.println("Sender: " + messageInfo.getSenderRepresentativeName());
        System.out.println("Recipients: " + messageInfo.getDisplayTo());
        System.out.println("------------------------------");
    }

    // Call this method recursively for each subfolder
    if (folderInfo.hasSubFolders() == true) {
        for (FolderInfo subfolderInfo : folderInfo.getSubFolders()) {
            displayFolderContents(subfolderInfo, pst);
        }
    }
}

การสกัดข้อความจากไฟล์ PST

บทความนี้แสดงวิธีการอ่านไฟล์ Microsoft Outlook PST และ สกัดข้อความ. ข้อความจะถูกบันทึกลงดิสก์ในรูปแบบ MSG บทความยังแสดงวิธีการ สกัดข้อความจำนวนที่ระบุ จากไฟล์ PST. ใช้วิธีการแบบ recursive เพื่อท่องดูทุกโฟลเดอร์ (รวมถึงโฟลเดอร์ซ้อน) และเรียก PersonalStorage.extractMessage() เมธอดเพื่อดึงข้อความ Outlook ไปยังอินสแตนซ์ของ MapiMessage คลาส. หลังจากนั้นเรียก MapiMessage.save() เมธอดเพื่อบันทึกข้อความไปยังดิสก์หรือสตรีมในรูปแบบ MSG ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสกัดข้อความจากไฟล์ PST.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public static void run() {
    // The path to the File directory.
    String dataDir = "data/";

    // Load the Outlook file
    String path = dataDir + "PersonalStorage.pst";

    try {
        // load the Outlook PST file
        PersonalStorage pst = PersonalStorage.fromFile(path);

        // get the Display Format of the PST file
        System.out.println("Display Format: " + pst.getFormat());

        // get the folders and messages information
        FolderInfo folderInfo = pst.getRootFolder();

        // Call the recursive method to extract msg files from each folder
        extractMsgFiles(folderInfo, pst);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

// This is a recursive method to display contents of a folder
private static void extractMsgFiles(FolderInfo folderInfo, PersonalStorage pst) {
    // display the folder name
    System.out.println("Folder: " + folderInfo.getDisplayName());
    System.out.println("==================================");
    // loop through all the messages in this folder
    MessageInfoCollection messageInfoCollection = folderInfo.getContents();
    for (MessageInfo messageInfo : messageInfoCollection) {
        System.out.println("Saving message {0} ...." + messageInfo.getSubject());
        // get the message in MapiMessage instance
        MapiMessage message = pst.extractMessage(messageInfo);
        // save this message to disk in msg format
        message.save(message.getSubject().replace(":", " ") + ".msg");
        // save this message to stream in msg format
        ByteArrayOutputStream messageStream = new ByteArrayOutputStream();
        message.save(messageStream);
    }

    // Call this method recursively for each subfolder
    if (folderInfo.hasSubFolders() == true) {
        for (FolderInfo subfolderInfo : folderInfo.getSubFolders()) {
            extractMsgFiles(subfolderInfo, pst);
        }
    }
}

บันทึกข้อความโดยตรงจาก PST ไปยังสตรีม

เพื่อบันทึกข้อความจากไฟล์ PST ไปยังสตรีมโดยตรงโดยไม่ต้องดึง MsgInfo ของข้อความ ใช้ saveMessageToStream() เมธอด. ตัวอย่างโค้ดต่อไปนี้แสดงวิธีบันทึกข้อความโดยตรงจาก PST ไปยังสตรีม.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/";

// Load the Outlook file
String path = dataDir + "PersonalStorage.pst";

// Save message to MemoryStream
try (PersonalStorage personalStorage = PersonalStorage.fromFile(path)) {
    FolderInfo inbox = personalStorage.getRootFolder().getSubFolder("Inbox");
    for (MessageInfo messageInfo : inbox.enumerateMessages()) {
        try (ByteArrayOutputStream memeorystream = new ByteArrayOutputStream()) {
            personalStorage.saveMessageToStream(messageInfo.getEntryIdString(), memeorystream);
        }
    }
}

// Save message to file
try (PersonalStorage pst = PersonalStorage.fromFile(path)) {
    FolderInfo inbox = pst.getRootFolder().getSubFolder("Inbox");
    for (MessageInfo messageInfo : inbox.enumerateMessages()) {
        try (FileOutputStream fs = new FileOutputStream(new File(dataDir + messageInfo.getSubject() + ".msg"))) {
            pst.saveMessageToStream(messageInfo.getEntryIdString(), fs);
        }
    }
}

try (PersonalStorage pst = PersonalStorage.fromFile(path)) {
    FolderInfo inbox = pst.getRootFolder().getSubFolder("Inbox");

    // To enumerate entryId of messages you may use FolderInfo.EnumerateMessagesEntryId() method:
    for (String entryId : inbox.enumerateMessagesEntryId()) {
        try (ByteArrayOutputStream ms = new ByteArrayOutputStream()) {
            pst.saveMessageToStream(entryId, ms);
        }
    }
}

การสกัดข้อความจำนวน n จากไฟล์ PST

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
FolderInfo inbox = personalStorage.getRootFolder().getSubFolder("Inbox");

// Extracts messages starting from 10th index top and extract total 100 messages
MessageInfoCollection messages = inbox.getContents(10, 100);

รับจำนวนรวมของรายการจากไฟล์ PST

Aspose.Email ให้ GetTotalItemsCount() เมธอดของ PersonalStorage.Store property. มันคืนค่าจำนวนรวมของรายการข้อความที่อยู่ใน PST.

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

try (PersonalStorage pst = PersonalStorage.fromFile("my.pst", false)) {
    int count = pst.getStore().getTotalItemsCount();
}

ลบรายการจากไฟล์ PST

Add Messages to PST Files แสดงวิธีการเพิ่มข้อความไปยังไฟล์ PST แน่นอนว่าก็สามารถลบรายการ (เนื้อหา) จากไฟล์ PST ได้และอาจต้องการลบข้อความเป็นกลุ่ม รายการจากไฟล์ PST สามารถลบได้โดยใช้ FolderInfo.deleteChildItem() เมธอด. API ยังให้บริการ FolderInfo.deleteChildItems() เมธอดเพื่อลบรายการเป็นกลุ่มจากไฟล์ PST.

การลบข้อความจากไฟล์ PST

บทความนี้แสดงวิธีการใช้ FolderInfo คลาสเพื่อเข้าถึงโฟลเดอร์เฉพาะในไฟล์ PST. เพื่อลบข้อความจากโฟลเดอร์ย่อย Sent ของไฟล์ PST ที่โหลดหรือสร้างไว้ก่อนหน้า:

  1. สร้างอินสแตนซ์ของ FolderInfo คลาสและโหลดด้วยเนื้อหาของโฟลเดอร์ย่อย sent.
  2. ลบข้อความจากโฟลเดอร์ Sent โดยเรียก FolderInfo.deleteChildItem() เมธอดและส่งผ่าน MessageInfo.EntryId เป็นพารามิเตอร์ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีลบข้อความจากโฟลเดอร์ย่อย Sent ของไฟล์ PST.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/" + "Sub.pst";

// Load the Outlook PST file
PersonalStorage personalStorage = PersonalStorage.fromFile(dataDir);

// Get the Sent items folder
FolderInfo folderInfo = personalStorage.getPredefinedFolder(StandardIpmFolder.SentItems);

MessageInfoCollection msgInfoColl = folderInfo.getContents();
for (MessageInfo msgInfo : msgInfoColl) {
    System.out.println(msgInfo.getSubject() + ": " + msgInfo.getEntryIdString());
    if (msgInfo.getSubject().equals("some delete condition")) {
        // Delete this item
        folderInfo.deleteChildItem(msgInfo.getEntryId());
        System.out.println("Deleted this message");
    }
}

การลบโฟลเดอร์จากไฟล์ PST

คุณสามารถลบโฟลเดอร์ PST ได้โดยย้ายไปยังโฟลเดอร์ Deleted Items.

try (PersonalStorage pst = PersonalStorage.fromFile("test.pst")) {
    FolderInfo deletedItemsFolder = pst.getPredefinedFolder(StandardIpmFolder.DeletedItems);
    FolderInfo emptyFolder = pst.getRootFolder().getSubFolder("Empty folder");
    FolderInfo someFolder = pst.getRootFolder().getSubFolder("Some folder");
    pst.moveItem(emptyFolder, deletedItemsFolder);
    pst.moveItem(someFolder, deletedItemsFolder);
}

ข้อได้เปรียบของเมธอดนี้คือโฟลเดอร์ที่ถูกลบสามารถกู้คืนได้ง่าย.

FolderInfo someFolder = deletedItemsFolder.getSubFolder("Some folder");
pst.moveItem(someFolder, pst.getRootFolder());

คุณสามารถลบโฟลเดอร์ออกจากโฟลเดอร์ Deleted Items อย่างถาวร หากจำเป็น.

deletedItemsFolder.deleteChildItem(emptyFolder.getEntryId());

นี้ deleteChildItem() เมธอดนี้สามารถใช้กับโฟลเดอร์ใดก็ได้หากคุณต้องการลบโฟลเดอร์ย่อยโดยทันทีและถาวรโดยข้ามโฟลเดอร์ Deleted Items.

FolderInfo someFolder = pst.getRootFolder().getSubFolder("Some folder");
pst.getRootFolder().deleteChildItem(someFolder.getEntryId());

ลบรายการเป็นกลุ่มจากไฟล์ PST

API ของ Aspose.Email สามารถใช้ลบรายการเป็นกลุ่มจากไฟล์ PST ได้ ซึ่งทำได้โดยใช้ deleteChildItems() เมธอดที่รับรายการของรายการ Entry ID ที่อ้างถึงรายการที่ต้องลบ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการลบรายการเป็นกลุ่มจากไฟล์ PST.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/" + "Sub.pst";

try (PersonalStorage personalStorage = PersonalStorage.fromFile(dataDir)) {
    // Get Inbox SubFolder from Outlook file
    FolderInfo inbox = personalStorage.getRootFolder().getSubFolder("Inbox");

    // Create instance of PersonalStorageQueryBuilder
    PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder();

    queryBuilder.getFrom().contains("someuser@domain.com");
    MessageInfoCollection messages = inbox.getContents(queryBuilder.getQuery());
    List<String> deleteList = new ArrayList<String>();
    for (MessageInfo messageInfo : messages) {
        deleteList.add(messageInfo.getEntryIdString());
    }

    // delete messages having From = "someuser@domain.com"
    inbox.deleteChildItems(deleteList);
}

Enumerate และกู้คืนข้อความที่ถูกลบแบบ Soft-Deleted จาก PST

สามารถดึงข้อความที่ถูกลบอย่างถาวรด้วย Aspose.Email for Java ได้ API ของมันอนุญาตให้คุณ enumerate ข้อความที่ถูกลบแบบ soft-deleted ในไฟล์ PST. รายการที่ถูกลบแบบ soft-deleted คือข้อความที่ถูกลบสองครั้ง - ก่อนหน้านี้ย้ายไปยังโฟลเดอร์ Deleted Items แล้วจึงลบออกจากนั้น ข้อความเหล่านี้ยังสามารถกู้คืนได้ และ Aspose.Email มีวิธีที่สะดวกในการเข้าถึงพวกมัน. The PersonalStorage.findAndEnumerateSoftDeletedItems() เมธอดคืนคอลเลกชันของ RestoredItemEntry วัตถุ. แต่ละรายการประกอบด้วย:

  • MapiMessage item — ข้อความที่เรียกคืน.
  • String FolderId - ตัวระบุของโฟลเดอร์ที่ข้อความเดิมอยู่.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการ enumerate รายการอีเมลที่ถูกลบแบบ soft-deleted (เรียกคืนได้) ในไฟล์ PST:

// Load the PST file
try (PersonalStorage pst = PersonalStorage.fromFile(fileName)) {
    // Enumerate soft-deleted items
    for (RestoredItemEntry entry : pst.findAndEnumerateSoftDeletedItems()) {
        MapiMessage message = entry.getItem();
        String folderId = entry.getFolderId();
        
        System.out.println("Subject: " + message.getSubject());
        System.out.println("Deleted from Folder ID: " + folderId);
        System.out.println("-----------------------------------");
    }
}

ค้นหาข้อความและโฟลเดอร์ใน PST ตามเกณฑ์

ไฟล์ Personal Storage (PST) อาจมีข้อมูลจำนวนมหาศาลและการค้นหาข้อมูลที่ตรงกับเกณฑ์เฉพาะในไฟล์ใหญ่เช่นนี้ต้องมีจุดตรวจสอบหลายขั้นตอนในโค้ดเพื่อกรองข้อมูล. ด้วย PersonalStorageQueryBuilder คลาส, Aspose.Email ทำให้สามารถค้นหาบันทึกเฉพาะใน PST ตามเกณฑ์การค้นหาที่กำหนดได้ สามารถค้นหาข้อความใน PST ตามพารามิเตอร์การค้นหาเช่นผู้ส่ง, ผู้รับ, หัวเรื่อง, ความสำคัญของข้อความ, การมีไฟล์แนบ, ขนาดข้อความ, และแม้กระทั่ง ID ของข้อความ. The PersonalStorageQueryBuilder ยังสามารถใช้เพื่อค้นหาโฟลเดอร์ย่อยได้.

การค้นหาข้อความและโฟลเดอร์ใน PST

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการใช้ PersonalStorageQueryBuilder คลาสสำหรับค้นหาเนื้อหาใน PST ตามเกณฑ์การค้นหาที่แตกต่างกัน ตัวอย่างเช่น แสดงการค้นหา PST ตาม:

  • ความสำคัญของข้อความ.
  • คลาสของข้อความ.
  • การมีไฟล์แนบ.
  • ขนาดของข้อความ.
  • วันที่ของข้อความ.
  • ข้อความที่ยังไม่ได้อ่าน.
  • ข้อความที่ยังไม่ได้อ่านพร้อมไฟล์แนบ, และ
  • โฟลเดอร์ที่มีชื่อโฟลเดอร์ย่อยเฉพาะ.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/";

try (PersonalStorage personalStorage = PersonalStorage.fromFile(dataDir + "Outlook.pst")) {
    FolderInfo folder = personalStorage.getRootFolder().getSubFolder("Inbox");
    PersonalStorageQueryBuilder builder = new PersonalStorageQueryBuilder();

    // High importance messages
    builder.getImportance().equals((int) MapiImportance.High);
    MessageInfoCollection messages = folder.getContents(builder.getQuery());
    System.out.println("Messages with High Imp:" + messages.size());

    builder = new PersonalStorageQueryBuilder();
    builder.getMessageClass().equals("IPM.Note");
    messages = folder.getContents(builder.getQuery());
    System.out.println("Messages with IPM.Note:" + messages.size());

    builder = new PersonalStorageQueryBuilder();
    // Messages with attachments AND high importance
    builder.getImportance().equals((int) MapiImportance.High);
    builder.hasFlags(MapiMessageFlags.MSGFLAG_HASATTACH);
    messages = folder.getContents(builder.getQuery());
    System.out.println("Messages with atts: " + messages.size());

    builder = new PersonalStorageQueryBuilder();
    // Messages with size > 15 KB
    builder.getMessageSize().greater(15000);
    messages = folder.getContents(builder.getQuery());
    System.out.println("messags size > 15Kb:" + messages.size());

    java.util.Calendar c = java.util.Calendar.getInstance();

    builder = new PersonalStorageQueryBuilder();
    // Messages by Current Date
    // (Note that queries by date are not supported for Calendar Items in the Appointments folder)
    builder.getSentDate().on(c.getTime(), DateComparisonType.ByDate);
    messages = folder.getContents(builder.getQuery());
    System.out.println("Messages by Current Date: " + messages.size());

    builder = new PersonalStorageQueryBuilder();
    // Messages between Dates
    // (Note that queries by date are not supported for Calendar Items in the Appointments folder)
    c.set(2020,  0, 1, 0, 0, 0);
    builder.getSentDate().since(c.getTime());
    c.set(2021,  0, 1, 0, 0, 0);
    builder.getSentDate().before(c.getTime());
    messages = folder.getContents(builder.getQuery());
    System.out.println("Messages between Dates: " + messages.size());

    builder = new PersonalStorageQueryBuilder();
    // Unread messages
    builder.hasNoFlags(MapiMessageFlags.MSGFLAG_READ);
    messages = folder.getContents(builder.getQuery());
    System.out.println("Unread:" + messages.size());

    builder = new PersonalStorageQueryBuilder();
    // Unread messages with attachments
    builder.hasNoFlags(MapiMessageFlags.MSGFLAG_READ);
    builder.hasFlags(MapiMessageFlags.MSGFLAG_HASATTACH);
    messages = folder.getContents(builder.getQuery());
    System.out.println("Unread msgs with atts: " + messages.size());

    // Folder with name of 'SubInbox'
    builder = new PersonalStorageQueryBuilder();
    builder.getFolderName().equals("SubInbox");
    FolderInfoCollection folders = folder.getSubFolders(builder.getQuery());
    System.out.println("Folder having subfolder: " + folders.size());

    builder = new PersonalStorageQueryBuilder();
    // Folders with subfolders
    builder.hasSubfolders();
    folders = folder.getSubFolders(builder.getQuery());
    System.out.println(folders.size());
}

การค้นหาข้อความใน PST ด้วยพารามิเตอร์ Ignore Case

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการค้นหาข้อความใน PST ด้วยพารามิเตอร์ ignore case.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/";

try (PersonalStorage personalStorage = PersonalStorage.create(dataDir + "CaseSensitivity.pst", FileFormatVersion.Unicode)) {
    FolderInfo folderinfo = personalStorage.createPredefinedFolder("Inbox", StandardIpmFolder.Inbox);
    folderinfo.addMessage(MapiMessage.fromMailMessage(MailMessage.load("Sample.eml")));
    PersonalStorageQueryBuilder builder = new PersonalStorageQueryBuilder();
    // IgnoreCase is True
    builder.getFrom().contains("automated", true);
    MailQuery query = builder.getQuery();
    MessageInfoCollection coll = folderinfo.getContents(query);
    System.out.println(coll.size());
}

การค้นหาหัวเรื่องข้อความด้วยคำสำคัญหลายคำในไฟล์ PST

คุณสามารถใช้ MailQueryBuilder.or เมธอดเพื่อค้นหาข้อความที่หัวเรื่องมีคำอย่างน้อยหนึ่งคำที่กำหนดตามตัวอย่างด้านล่าง:

PersonalStorageQueryBuilder builder1 = new PersonalStorageQueryBuilder();
builder1.getSubject().contains("Review"); // 'Review' is key word for the search

PersonalStorageQueryBuilder builder2 = new PersonalStorageQueryBuilder();
builder2.getSubject().contains("Error"); // 'Error' is also key word for the search

PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder();
queryBuilder.or(builder1.getQuery(), builder2.getQuery()); // message subjects must contain 'Review' or 'Error' words

try (PersonalStorage storage = PersonalStorage.fromFile("example.pst"))
{
    FolderInfo folderInfo = storage.getRootFolder().getSubFolder("Inbox");
    MessageInfoCollection messageInfos = folderInfo.getContents(queryBuilder.getQuery());

    for (MessageInfo messageInfo : messageInfos)
    {
        System.out.println(messageInfo.getSubject());
    }
}

การดึงข้อมูลโฟลเดอร์ PST แบบแบ่งหน้า

ปรับปรุงประสิทธิภาพและการควบคุมของแอปพลิเคชันเมื่อนำทางในโฟลเดอร์ขนาดใหญ่ ดึงเนื้อหาโฟลเดอร์ในรูปแบบแบ่งหน้าโดยใช้โอเวอร์โหลดของ Aspose.Email ของ getContents เมธอดนี้ - FolderInfo.getContents(MailQuery query, int startIndex, int count) - ดึงชุดย่อยของข้อความที่ตรงกับคิวรีที่กำหนด เริ่มจากดัชนีที่ระบุและจำกัดด้วยจำนวน ตัวอย่างโค้ดต่อไปนี้แสดงการดึงข้อมูลแบบแบ่งหน้าและการประมวลผลข้อความที่กรองจากโฟลเดอร์ PST:

// Load the PST file
try (PersonalStorage pst = PersonalStorage.fromFile(fileName)) {
    // Access a specific subfolder
    FolderInfo folder = pst.getRootFolder().getSubFolder("Inbox");

    // Build a query to filter messages by sender address
    PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder();
    queryBuilder.getFrom().contains("report-service", true);

    // Define the page size
    int pageSize = 5;

    // Retrieve and process messages in pages
    for (int pageIndex = 0; pageIndex < 6; pageIndex++) {
        int startIndex = pageIndex * pageSize;

        // Get a page of messages
        MessageInfoCollection messages = folder.getContents(queryBuilder.getQuery(), startIndex, pageSize);

        for (MessageInfo messageInfo : messages) {
            // Output basic info about each message
            System.out.println("Subject: " + messageInfo.getSubject() + ", Sender: " + messageInfo.getSenderRepresentativeName());
        }
    }
}

ย้ายรายการไปยังโฟลเดอร์อื่นของไฟล์ PST

Aspose.Email ทำให้สามารถย้ายรายการจากโฟลเดอร์ต้นทางไปยังโฟลเดอร์อื่นในไฟล์ Personal Storage (PST) เดียวกันได้ สิ่งนี้รวมถึง:

  • การย้ายโฟลเดอร์ที่กำหนดไปยังโฟลเดอร์แม่ใหม่.
  • การย้ายข้อความที่กำหนดไปยังโฟลเดอร์ใหม่.
  • การย้ายเนื้อหาไปยังโฟลเดอร์ใหม่.
  • การย้ายโฟลเดอร์ย่อยไปยังโฟลเดอร์แม่ใหม่.

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
try (PersonalStorage personalStorage = PersonalStorage.fromFile("test.pst")) {
    FolderInfo inbox = personalStorage.getPredefinedFolder(StandardIpmFolder.Inbox);
    FolderInfo deleted = personalStorage.getPredefinedFolder(StandardIpmFolder.DeletedItems);
    FolderInfo subfolder = inbox.getSubFolder("Subfolder");

    // Move folder and message to the Deleted Items
    personalStorage.moveItem(subfolder, deleted);
    MessageInfoCollection contents = subfolder.getContents();
    personalStorage.moveItem(contents.get(0), deleted);

    // Move all inbox subfolders and subfolder contents to the Deleted Items
    inbox.moveSubfolders(deleted);
    subfolder.moveContents(deleted);
}

การอัปเดตคุณสมบัติของข้อความในไฟล์ PST

บางครั้งจำเป็นต้องอัปเดตคุณสมบัติบางอย่างของข้อความ เช่น การเปลี่ยนหัวเรื่อง การทำเครื่องหมายความสำคัญของข้อความ เป็นต้น การอัปเดตข้อความในไฟล์ PST ด้วยการเปลี่ยนแปลงคุณสมบัติเช่นนั้น สามารถทำได้โดยใช้ FolderInfo.changeMessages เมธอดนี้ บทความนี้แสดงวิธีอัปเดตข้อความเป็นกลุ่มในไฟล์ PST สำหรับการเปลี่ยนแปลงคุณสมบัติ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีอัปเดตคุณสมบัติของข้อความในโหมดกลุ่มสำหรับหลายข้อความในไฟล์ PST

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/" + "Sub.pst";

// Load the Outlook PST file
PersonalStorage personalStorage = PersonalStorage.fromFile(dataDir);

// Get Requierd Subfolder
FolderInfo inbox = personalStorage.getRootFolder().getSubFolder("Inbox");

// find messages having From = "someuser@domain.com"
PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder();
queryBuilder.getFrom().contains("someuser@domain.com");

// Get Contents from Query
MessageInfoCollection messages = inbox.getContents(queryBuilder.getQuery());

// Save (MessageInfo,EntryIdString) in List
List<String> changeList = new ArrayList<String>();
for (MessageInfo messageInfo : messages) {
    changeList.add(messageInfo.getEntryIdString());
}

// Compose the new properties
MapiPropertyCollection updatedProperties = new MapiPropertyCollection();
updatedProperties.add(MapiPropertyTag.PR_SUBJECT_W, new MapiProperty(MapiPropertyTag.PR_SUBJECT_W, "New Subject".getBytes(Charset.forName("utf-16le"))));
updatedProperties.add(MapiPropertyTag.PR_IMPORTANCE, new MapiProperty(MapiPropertyTag.PR_IMPORTANCE, BitConverter.getBytesInt64(2)));

// update messages having From = "someuser@domain.com" with new properties
inbox.changeMessages(changeList, updatedProperties);

อัปเดตคุณสมบัติแบบกำหนดเองในไฟล์ PST

บางครั้งจำเป็นต้องทำเครื่องหมายรายการที่ประมวลผลแล้วในไฟล์ PST API ของ Aspose.Email สามารถทำได้โดยใช้ MapiProperty และ MapiNamedProperty เมธอดต่อไปนี้จะเป็นประโยชน์ในการทำเช่นนั้น

  • constructor MapiNamedProperty(long propertyTag, String nameIdentifier, UUID propertyGuid, byte[] propertyValue)
  • constructor MapiNamedProperty(long propertyTag, long nameIdentifier, UUID propertyGuid, byte[] propertyValue)
  • FolderInfo.changeMessages(MapiPropertyCollection updatedProperties) - changes all messages in folder
  • PersonalStorage.changeMessage(String entryId, MapiPropertyCollection updatedProperties) - change message properties
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public static void run() {
    // The path to the File directory.
    String dataDir = "data/" + "Sub.pst";

    try (PersonalStorage personalStorage = PersonalStorage.fromFile(dataDir)) {
        FolderInfo testFolder = personalStorage.getRootFolder().getSubFolder("Inbox");

        // Create the collection of message properties for adding or updating
        MapiPropertyCollection newProperties = new MapiPropertyCollection();

        // Normal, Custom and PidLidLogFlags named property
        MapiProperty property = new MapiProperty(MapiPropertyTag.PR_ORG_EMAIL_ADDR_W, "test_address@org.com".getBytes(Charset.forName("utf-16le")));
        MapiProperty namedProperty1 = new MapiNamedProperty(generateNamedPropertyTag(0L, MapiPropertyType.PT_LONG), "ITEM_ID", UUID.randomUUID(),
                BitConverter.getBytesInt64(123));
        MapiProperty namedProperty2 = new MapiNamedProperty(generateNamedPropertyTag(1L, MapiPropertyType.PT_LONG), 0x0000870C,
                UUID.fromString("0006200A-0000-0000-C000-000000000046"), BitConverter.getBytesInt64(0));
        newProperties.add(namedProperty1.getTag(), namedProperty1);
        newProperties.add(namedProperty2.getTag(), namedProperty2);
        newProperties.add(property.getTag(), property);
        testFolder.changeMessages(testFolder.enumerateMessagesEntryId(), newProperties);
    }
}

private static long generateNamedPropertyTag(long index, int dataType) {
    return (((0x8000 | index) << 16) | (long) dataType) & 0x00000000FFFFFFFFL;
}

สกัดไฟล์แนบโดยไม่ต้องสกัดข้อความทั้งหมด

API ของ Aspose.Email สามารถใช้สกัดไฟล์แนบจากข้อความ PST โดยไม่ต้องสกัดข้อความทั้งหมดก่อน. ส่วน ExtractAttachments เมธอดของ PersonalStorage สามารถใช้ทำเช่นนี้ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสกัดไฟล์แนบโดยไม่ต้องสกัดข้อความทั้งหมด

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/";

try (PersonalStorage personalstorage = PersonalStorage.fromFile(dataDir + "Outlook.pst")) {
    FolderInfo folder = personalstorage.getRootFolder().getSubFolder("Inbox");

    for (String messageInfo : folder.enumerateMessagesEntryId()) {
        MapiAttachmentCollection attachments = personalstorage.extractAttachments(messageInfo);

        if (attachments.size() != 0) {
            for (MapiAttachment attachment : attachments) {
                if (attachment.getLongFileName() != null && !attachment.getLongFileName().isEmpty()) {
                    if (attachment.getLongFileName().contains(".msg")) {
                        continue;
                    } else {
                        attachment.save(dataDir + "Attachments/" + attachment.getLongFileName());
                    }
                }
            }
        }
    }
}

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

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/";

try (PersonalStorage personalStorage = PersonalStorage.create(dataDir + "Ps1_out.pst", FileFormatVersion.Unicode)) {
    FolderInfo folder = personalStorage.getRootFolder().addSubFolder("Files");

    // Add Document.doc file with the "IPM.Document" message class by default.
    folder.addFile(dataDir + "attachment_1.doc", null);
}