פענוח קבצי הודעות Outlook

Aspose.Email for Java מספקת את MapiMessage מחלקה המשמשת לפתיחה וניתוח קובץ MSG. מכיוון שיכולים להיות רבים ממקבלי ההודעה בקובץ MSG, ה- MapiMessage המחלקה מציגה את getRecipients() שיטה שמחזירה MapiRecipientCollection שמייצג אוסף של MapiRecipient אובייקטים. ה- MapiRecipient האובייקט חשוף עוד שיטות לעבודה עם תכונות הנמענים.

הסדרת הצעדים הבאים משמשת למטרה זו:

  1. צור מופע של MapiMessage class לטעון קובץ 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 מציע 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;
    }
}