GroupDocs.Metadata for Java provides functionality that allows handling the most popular email message formats: EML and MSG. The following are some commonly used scenarios of working with the message content and metadata.
Reading EML message metadata
The EML message format is used by many email clients including Novell GroupWise, Microsoft Outlook Express, Lotus notes, Windows Mail, Mozilla Thunderbird, and Postbox. The files contain the email contents as plain text in MIME format, containing the email header and body, including attachments in one or more of several formats. The code sample below demonstrates how to extract metadata from an EML message.
MSG files are usually created by the Microsoft Outlook email client. The MSG format is used to represent individual email messages, appointments, contacts, tasks, and so on in the file system. The following code snippet shows how to read most common properties of an email message stored as an MSG file.
Both root package classes representing metadata in email messages (EmlRootPackage and MsgRootPackage) have the common ancestor which is the EmailRootPackage class. Since the MSG and EML formats have really similar functionality, in most cases, you don’t need to know the exact type of a loaded message. It is possible to refer to the EmailRootPackage class to access all basic features related to email messages. The following code sample shows how to remove all email message attachments using an instance of the EmailRootPackage class.
GroupDocs.Metadata for Java also allows the user to update some common email fields. The code sample below demonstrates how to alter the email subject and recipients.
try(Metadatametadata=newMetadata(Constants.InputEml)){EmailRootPackageroot=metadata.getRootPackageGeneric();root.getEmailPackage().setRecipients(newString[]{"sample@aspose.com"});root.getEmailPackage().setCarbonCopyRecipients(newString[]{"sample@groupdocs.com"});root.getEmailPackage().setSubject("RE: test subject");metadata.save(Constants.OutputEml);}
Updating MSG message fields
An MSG message has more editable fields than the common email package. The MsgPackage class allows you to change the following properties:
the body: setBody, setBodyHtml, setBodyRtf;
the sender: setSenderName, setSenderSmtpAddress, setSentRepresentingEmailAddress, setSentRepresentingName;
the times: setDeliveryTime, setClientSubmitTime;
other fields: setCategories, setBilling, setMileage, setReplyTo, setReadReceiptRequested;
the headers, through getHeaders().set(String, PropertyValue), which adds a new header or updates an existing one;
the attachments, through setAttachments.
Several other MAPI properties can be read but not changed, for example getInternetMessageId, getConversationTopic, getDisplayTo, getDisplayCc, getNormalizedSubject, and the isEncrypted, isSigned and isTemplate flags.
The code sample below changes the sender, the delivery time, the body, the categories, the headers and the attachments of a message.
try(Metadatametadata=newMetadata(Constants.InputMsg)){MsgRootPackageroot=metadata.getRootPackageGeneric();MsgPackagemsg=root.getMsgPackage();msg.setSenderName("sender");msg.setSenderSmtpAddress("sender1@sender.com");msg.setDeliveryTime(newDate());msg.setBody("text");msg.setCategories(newString[]{"test1","test2"});// Add new headers or update the existing ones
msg.getHeaders().set("X-Custom-Header",newPropertyValue("Custom Value"));msg.getHeaders().set("X-SID-Result",newPropertyValue("PASS"));// Replace the attachments
byte[]content=Files.readAllBytes(Paths.get(Constants.AttachmentJpg));msg.setAttachments(newMsgAttachmentPackage[]{newMsgAttachmentPackage("tree.jpg",content)});metadata.save(Constants.OutputMsg);}
Note
Starting from version 26.11, the sender address is returned by getSenderEmailAddress(), and the sender property is named SenderEmailAddress instead of Sender. The getSender() method is deprecated and returns the same value. If your code looks the property up by name, use the new name.
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: