Phân Tích Tệp Tin Nhắn Outlook
Sử dụng Aspose.Email cho Java, các nhà phát triển không chỉ có thể tải mà còn phân tích nội dung từ tệp tin nhắn Outlook.
- Để tải tệp MSG từ đĩa, sử dụng MapiMessage lớp tĩnh Load phương thức.
- Để phân tích nội dung tệp MSG, MapiMessage phơi bày một số phương thức.
Chủ đề này cho thấy cách tải và sau đó phân tích tệp MSG để hiển thị nội dung của nó.
Aspose.Email cho Java cung cấp MapiMessage lớp được sử dụng để mở và phân tích tệp MSG. Vì có thể có nhiều người nhận trong một tệp MSG, thì MapiMessage lớp này cung cấp getRecipients() phương thức trả về một MapiRecipientCollection mà đại diện cho một bộ sưu tập của MapiRecipient các đối tượng. Các MapiRecipient đối tượng này còn phơi bày các phương thức để làm việc với thuộc tính người nhận.
Chuỗi các bước sau thực hiện mục đích này:
- Tạo một đối tượng của MapiMessage lớp để tải tệp MSG từ Load phương thức tĩnh.
- Hiển thị tên người gửi, tiêu đề và nội dung từ tệp MSG bằng cách sử dụng getSenderName(), getSubject() và getBody() các phương thức.
- Gọi getRecipients() phương thức được cung cấp bởi MapiRecipient lớp để lấy tham chiếu đến bộ sưu tập của MapiRecipient đối tượng liên quan đến tệp MSG.
- Lặp qua MapiRecipientCollection bộ sưu tập để hiển thị nội dung cho mỗi MapiRecipient đối tượng thông qua các phương thức công khai của nó.
// 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());
}
Try it out!
Phân tích tệp email trực tuyến miễn phí Ứng dụng Phân tích Aspose.Email.
Lấy Kiểu Mục của Tin Nhắn MAPI
Aspose.Email cung cấp một MapiItemType enum đại diện cho một kiểu mục. Nó có thể được sử dụng để chuyển đổi tin nhắn thành một đối tượng của lớp tương ứng kế thừa từ IMapiMessageItem giao diện. Điều này giúp người dùng không phải kiểm tra giá trị thuộc tính MessageClass trước khi chuyển đổi tin nhắn.
Ví dụ mã sau cho thấy cách duyệt qua các tin nhắn trong một thư mục và chuyển đổi mỗi tin nhắn MAPI thành loại mục MAPI tương ứng, tùy thuộc vào loại của tin nhắn:
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;
}
}