การแยกไฟล์ข้อความ Outlook

Aspose.Email for Java มี MapiMessage คลาสที่ใช้เปิดและแยกไฟล์ MSG เนื่องจากอาจมีผู้รับหลายคนในไฟล์ MSG, MapiMessage คลาสเปิดเผย getRecipients() เมธอดที่คืนค่า MapiRecipientCollection ซึ่งเป็นคอลเลกชันของ MapiRecipient อ็อบเจกต์. MapiRecipient อ็อบเจกต์ยังเปิดเผยเมธอดสำหรับทำงานกับแอตทริบิวต์ของผู้รับ.

ลำดับขั้นตอนต่อไปนี้ใช้เพื่อบรรลุเป้าหมาย:

  1. สร้างอินสแตนซ์ของ MapiMessage คลาสเพื่อโหลดไฟล์ MSG จาก Load เมธอดสเตติก.
  2. แสดงชื่อผู้ส่ง, หัวข้อ, และเนื้อหาจากไฟล์ MSG โดยใช้ getSenderName(), getSubject() และ getBody() เมธอด.
  3. เรียกใช้ getRecipients() เมธอดที่เปิดเผยโดย MapiRecipient คลาสเพื่ออ้างอิงคอลเลกชันของ MapiRecipient อ็อบเจกต์ที่เชื่อมโยงกับไฟล์ MSG.
  4. วนลูปผ่าน MapiRecipientCollection คอลเลกชันเพื่อแสดงเนื้อหาสำหรับแต่ละ MapiRecipient อ็อบเจกต์ผ่านเมธอดสาธารณะของมัน.
// The path to the resource directory.
String dataDir = Utils.getSharedDataDir(ParsingOutlookMessageFiles.class) + "outlook/";

//Instantiate an MSG file to load an MSG file from disk
MapiMessage outlookMessageFile = MapiMessage.fromFile(dataDir + "message.msg");
//Display sender's name
System.out.println("Sender Name : " + outlookMessageFile.getSenderName());
//Display Subject
System.out.println("Subject : " + outlookMessageFile.getSubject());
//Display Body
System.out.println("Body : " + outlookMessageFile.getBody());
//Display Recipient's info
System.out.println("Recipients : \n");

//Loop through the recipients collection associated with the MapiMessage object
for (int i = 0; i < outlookMessageFile.getRecipients().size(); i++) {
	//Set a reference to the MapiRecipient object
	MapiRecipient rcp = (MapiRecipient) outlookMessageFile.getRecipients().get_Item(i);
	//Display recipient email address
	System.out.println("Email : " + rcp.getEmailAddress());
	//Display recipient name
	System.out.println("Name : " + rcp.getDisplayName());
	//Display recipient type
	System.out.println("Recipient Type : " + rcp.getRecipientType());
}

รับประเภทรายการของข้อความ MAPI

Aspose.Email ให้บริการ a MapiItemType enum ที่แสดงประเภทของรายการ สามารถใช้สำหรับการแปลงข้อความเป็นอ็อบเจ็กต์ของคลาสที่สืบทอดจาก IMapiMessageItem อินเทอร์เฟซ การทำเช่นนี้ช่วยหลีกเลี่ยงไม่ให้ผู้ใช้ต้องตรวจสอบค่าคุณสมบัติ MessageClass ก่อนการแปลงข้อความ.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีวนผ่านข้อความในโฟลเดอร์และแปลงแต่ละข้อความ MAPI ให้เป็นประเภทรายการ MAPI ที่สอดคล้องกัน ขึ้นอยู่กับประเภทของข้อความ:

for (MessageInfo messageInfo : folder.enumerateMessages()) {
    MapiMessage msg = pst.extractMessage(messageInfo);

    switch (msg.getSupportedType()) {
        // Non-supported type. MapiMessage cannot be converted to an appropriate item type.
        // Just use in MSG format.
        case MapiItemType.None:
            break;
        // An email message. Conversion isn't required.
        case MapiItemType.Message:
            break;
        // A contact item. Can be converted to MapiContact.
        case MapiItemType.Contact:
            MapiContact contact = (MapiContact) msg.toMapiMessageItem();
            break;
        // A calendar item. Can be converted to MapiCalendar.
        case MapiItemType.Calendar:
            MapiCalendar calendar = (MapiCalendar) msg.toMapiMessageItem();
            break;
        // A distribution list. Can be converted to MapiDistributionList.
        case MapiItemType.DistList:
            MapiDistributionList dl = (MapiDistributionList) msg.toMapiMessageItem();
            break;
        // A Journal entry. Can be converted to MapiJournal.
        case MapiItemType.Journal:
            MapiJournal journal = (MapiJournal) msg.toMapiMessageItem();
            break;
        // A StickyNote. Can be converted to MapiNote.
        case MapiItemType.Note:
            MapiNote note = (MapiNote) msg.toMapiMessageItem();
            break;
        // A Task item. Can be converted to MapiTask.
        case MapiItemType.Task:
            MapiTask task = (MapiTask) msg.toMapiMessageItem();
            break;
    }
}