Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Created March 16, 2022 08:42
Show Gist options
  • Save conholdate-gists/a1e600b31c9c1bdd10c3171922e06692 to your computer and use it in GitHub Desktop.
Save conholdate-gists/a1e600b31c9c1bdd10c3171922e06692 to your computer and use it in GitHub Desktop.
Read Outlook MSG File using Java
// This code example demonstrates how to save attachments from MSG file.
// Load an MSG file from disk
MapiMessage outlookMessageFile = MapiMessage.load("D:\\Files\\Email\\WithEmbeddedMsg.msg");
// Loop through the attachments collection associated with the MapiMessage object
for (int i = 0; i < outlookMessageFile.getAttachments().size(); i++) {
// Set a reference to the MapiAttachment object
MapiAttachment outlookMessageAttachment = (MapiAttachment) outlookMessageFile.getAttachments().get_Item(i);
// Display attachment type
System.out.println("Att Type : " + outlookMessageAttachment.getMimeTag());
// Display attached file name
System.out.println("File Name : " + outlookMessageAttachment.getLongFileName());
// Save attachment to the disk
outlookMessageAttachment.save("D:\\Files\\Email\\" + outlookMessageAttachment.getDisplayName());
}
// This code example demonstrates how to read MSG file.
// Load an MSG file from disk
MapiMessage outlookMessageFile = MapiMessage.load("D:\\Files\\Email\\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());
}
// This code example demonstrates how to read an embedded message attached in MSG.
// Load an MSG file
MapiMessage mapi = MapiMessage.load(dataDir + "EmbededMessageAsAttachment.msg");
// Show total attachments
System.out.println("Total attachments : " + mapi.getAttachments().size());
// Read attachment as MapiMessage
MapiMessage emb = mapi.getAttachments().get_Item(0).getObjectData().toMapiMessage();
// Display sender's name
System.out.println("Sender Name : " + emb.getSenderName());
// Display Subject
System.out.println("Subject : " + emb.getSubject());
// Display Body
System.out.println("Body : " + emb.getBody());
// Display Recipient's info
System.out.println("Recipients :");
// Loop through the recipients collection associated with the MapiMessage object
for (int i = 0; i < emb.getRecipients().size(); i++) {
// Set a reference to the MapiRecipient object
MapiRecipient rcp = (MapiRecipient) emb.getRecipients().get_Item(i);
// Display recipient email address
System.out.println("\t Email : " + rcp.getEmailAddress());
// Display recipient name
System.out.println("\t Name : " + rcp.getDisplayName());
// Display recipient type
System.out.println("\t Recipient Type : " + rcp.getRecipientType());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment