解析 Outlook 消息文件

Aspose.Email for Java 提供了 MapiMessage 用于打开和解析 MSG 文件的类。由于 MSG 文件中可能有许多收件人, MapiMessage 类公开了 getRecipients() 方法返回一个 MapiRecipientCollection 表示一组 MapiRecipient 对象。该 MapiRecipient 对象进一步公开了用于处理收件人属性的方法。

以下步骤序列实现此目的:

  1. 创建该类的实例 MapiMessage 类,用于从 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 枚举,表示项目类型。它可用于将消息转换为从中派生的相应类的对象 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;
    }
}