Class OutlookMessage
This class is meant to be a very simple and easy-to-use API to read and create
.msg files for Microsoft Outlook. It is also compatible with the standard
javax.mail package, by the usage of the toMimeMessage() method.
This implementation is capable of generating .msg files that can be open by
Microsoft Outlook, so it behaves like if the user has created a new email. However it's
purpose is not to make a full-integration of Microsoft Outlook with all the features it
has (such as calendars, appointments, tasks, ...). Those features may be covered in future
developments.
The Microsoft specification MS-OXCMSG can be found in the ms-specifications
folder of jotlmsg, or can be downloaded here.
The detailed list of properties MS-OXPROPS is also available in the ms-specifications
folder or here.
- Author:
- Cedric Tabin
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringFormat of a MIME date that can be used as pattern in aSimpleDateFormat. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new empty message.OutlookMessage(File mapiMessageFile) Creates a new message with the data of the specifiedmapiMessageFile.OutlookMessage(InputStream mapiMessageInputStream) Creates a new message with the data of the specifiedmapiMessageInputStream.OutlookMessage(org.apache.poi.hsmf.MAPIMessage mapiMessage) Creates a new message with the data of the specifiedmapiMessage. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAttachment(OutlookMessageAttachment attachment) Add a new attachment to this message.addAttachment(String name, String mimeType, OutlookMessageAttachment.InputStreamCreator inputStreamCreator) Add a new attachment to this message.addAttachment(String name, String mimeType, InputStream input) Add a new attachment to this message.voidaddRecipient(OutlookMessageRecipient recipient) Add the specifiedrecipientto this message.addRecipient(OutlookMessageRecipient.Type type, String email) Creates and add a newOutlookMessageRecipientto this message.addRecipient(OutlookMessageRecipient.Type type, String email, String name) Creates and add a newOutlookMessageRecipientto this message.static StringextractEmail(String mixedMailStr) Extracts the mail from a mixed mail string, for instance "John Doe <john.doe@hotmail.com>".Returns all the recipients of this message.Returns the attachments of this message.getFrom()Defines theFromemail address from which a message has been sent.Defines the HTML body of the message.Defines the plain text body of the message.Returns all the recipients of the specified type.Defines the addresses use when the user hits the 'reply' button.Defines the sent date of the message.Defines the subject of the message.voidRemoves all the attachments from this message.voidRemoves all the recipients.voidRemoves all the recipients of the given type.voidremoveAttachment(OutlookMessageAttachment attachment) Removes the specified attachment from this message.voidremoveRecipient(OutlookMessageRecipient recipient) Removes the specifiedrecipientfrom this message.voidvoidsetHtmlBody(String htmlBody) voidsetPlainTextBody(String plainTextBody) voidsetReplyTo(List<String> replyTo) voidsetSentDate(Date d) voidsetSubject(String subject) jakarta.mail.internet.MimeMessageCreates a newMimeMessagefrom thisOutlookMessage.jakarta.mail.internet.MimeMessagetoMimeMessage(jakarta.mail.Session session) Creates a newMimeMessagefrom thisOutlookMessage.jakarta.mail.internet.MimeMessagetoMimeMessage(Properties sessionProps) Creates a newMimeMessagefrom thisOutlookMessage.voidWrites the content of this message to the specifiedfile.voidwriteTo(OutputStream outputStream) Writes the content of this message to the specifiedoutputStream.
-
Field Details
-
MIME_DATE_FORMAT
Format of a MIME date that can be used as pattern in aSimpleDateFormat.- See Also:
-
-
Constructor Details
-
OutlookMessage
public OutlookMessage()Creates a new empty message. -
OutlookMessage
Creates a new message with the data of the specifiedmapiMessageInputStream.- Parameters:
mapiMessageInputStream- The source message data.- Throws:
IOException- If an I/O error occurs.
-
OutlookMessage
Creates a new message with the data of the specifiedmapiMessageFile.- Parameters:
mapiMessageFile- The source message data.- Throws:
IOException- If an I/O error occurs.
-
OutlookMessage
public OutlookMessage(org.apache.poi.hsmf.MAPIMessage mapiMessage) Creates a new message with the data of the specifiedmapiMessage. All the data will be copied from the source message and the latter can be then discarded.- Parameters:
mapiMessage- The source message data.
-
-
Method Details
-
getSubject
Defines the subject of the message. This value may be null. -
setSubject
-
getPlainTextBody
Defines the plain text body of the message. This value may be null.If both a plain text and an html body are set, then the html body will be displayed by Outlook.
-
setPlainTextBody
-
getHtmlBody
Defines the HTML body of the message. This value may be null.If both a plain text and an html body are set, then the html body will be displayed by Outlook.
-
setHtmlBody
-
getFrom
Defines theFromemail address from which a message has been sent. This value may be null. -
setFrom
-
getReplyTo
Defines the addresses use when the user hits the 'reply' button. Currently, this value is only used to create aMimeMessagebut isn't used for.msggeneration. This value may be null.- See Also:
-
setReplyTo
-
getSentDate
Defines the sent date of the message. If this value is not defined, then it means the message is considered as unsent.However, it is possible that this value is set when reading a message that was created by Outlook and may then contains its creation date.
-
setSentDate
-
getRecipients
Returns all the recipients of the specified type. If there is none, then an empty list will be returned.- Parameters:
type- The recipients type.- Returns:
- An immutable list with all the recipients of the given type.
-
getAllRecipients
Returns all the recipients of this message. If there is no recipient, an empty list will be returned.- Returns:
- A new list with all the recipients.
-
addRecipient
Creates and add a newOutlookMessageRecipientto this message.- Parameters:
type- The type.email- The email.- Returns:
- The created recipient.
-
addRecipient
public OutlookMessageRecipient addRecipient(OutlookMessageRecipient.Type type, String email, String name) Creates and add a newOutlookMessageRecipientto this message.- Parameters:
type- The type.email- The email.name- The name or null.- Returns:
- The created recipient.
-
addRecipient
Add the specifiedrecipientto this message.- Parameters:
recipient- The recipient to add.
-
removeRecipient
Removes the specifiedrecipientfrom this message.- Parameters:
recipient- The recipient to remove.
-
removeAllRecipients
Removes all the recipients of the given type.- Parameters:
type- The type of recipients to remove.
-
removeAllRecipients
public void removeAllRecipients()Removes all the recipients. -
getAttachments
Returns the attachments of this message. This list can be directly modified.- Returns:
- The attachments.
-
addAttachment
Add a new attachment to this message.The data of the
InputStreamwill be loaded into memory (seeOutlookMessageAttachment.MemoryInputStreamCreator). If you don't expect to invokewriteTo()ortoMimeMessage()multiple times, then consider using theothermethod, which uses aOutlookMessageAttachment.InputStreamCreator.The MIME type must be set accordingly to the
input.- Parameters:
name- The name.mimeType- The MIME type.input- The input data.- Returns:
- The created attachment.
-
addAttachment
public OutlookMessageAttachment addAttachment(String name, String mimeType, OutlookMessageAttachment.InputStreamCreator inputStreamCreator) Add a new attachment to this message. TheInputStreamCreatorcan be set to the attachment later.To use this method with a single-usage
InputStream:message.addAttachment("myAttachment", "text/plain", a -> myInputStream);The MIME type must be set accordingly to the
input.- Parameters:
name- The name.mimeType- The MIME type.inputStreamCreator- TheInputStreamcreator or null.- Returns:
- The created attachment.
-
addAttachment
Add a new attachment to this message.- Parameters:
attachment- The attachment.
-
removeAttachment
Removes the specified attachment from this message.- Parameters:
attachment- The attachment to remove.
-
removeAllAttachments
public void removeAllAttachments()Removes all the attachments from this message. -
toMimeMessage
public jakarta.mail.internet.MimeMessage toMimeMessage() throws IOException, jakarta.mail.MessagingExceptionCreates a newMimeMessagefrom thisOutlookMessage. A newSessionwill be created with an emptyPropertiesinstance.- Returns:
- A new
MimeMessageinstance. - Throws:
IOExceptionjakarta.mail.MessagingException- See Also:
-
toMimeMessage
public jakarta.mail.internet.MimeMessage toMimeMessage(Properties sessionProps) throws IOException, jakarta.mail.MessagingException Creates a newMimeMessagefrom thisOutlookMessage. A newSessionwill be created with the specifiedsessionProps.- Parameters:
sessionProps- TheSessionproperties.- Returns:
- A new
MimeMessageinstance. - Throws:
IOExceptionjakarta.mail.MessagingException- See Also:
-
toMimeMessage
public jakarta.mail.internet.MimeMessage toMimeMessage(jakarta.mail.Session session) throws IOException, jakarta.mail.MessagingException Creates a newMimeMessagefrom thisOutlookMessage. This method will generate a multipart/mixedMimeMessage, with the first part being the message body (named 'body').- Parameters:
session- TheSessionto use for message creation.- Returns:
- A new
MimeMessageinstance. - Throws:
IOExceptionjakarta.mail.MessagingException
-
extractEmail
Extracts the mail from a mixed mail string, for instance "John Doe <john.doe@hotmail.com>".This method uses a simple pattern to get the first string which looks like an email, eg there is an @ with at least one char on each side. Spaces are considered as separators.
If there are multiple mails in
mixedMailStr, only the first one will be returned.This method does not check at all if the returned mail is valid.
- Parameters:
mixedMailStr- A string that contains (potentially) an email address.- Returns:
- The first string looking like an email or null.
-
writeTo
Writes the content of this message to the specifiedfile. The created file will be in format.msgthat can be open by Microsoft Outlook.- Parameters:
file- The.msgfile to create.- Throws:
IOException- If an I/O error occurs.
-
writeTo
Writes the content of this message to the specifiedoutputStream. The bytes written represent a.msgfile that can be open by Microsoft Outlook. TheoutputStreamwill remain open.- Parameters:
outputStream- The stream to write to.- Throws:
IOException- If an I/O error occurs.
-