public class OutlookMessage extends Object
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.
| Constructor and Description |
|---|
OutlookMessage()
Creates a new empty message.
|
OutlookMessage(File mapiMessageFile)
Creates a new message with the data of the specified
mapiMessageFile. |
OutlookMessage(InputStream mapiMessageInputStream)
Creates a new message with the data of the specified
mapiMessageInputStream. |
OutlookMessage(org.apache.poi.hsmf.MAPIMessage mapiMessage)
Creates a new message with the data of the specified
mapiMessage. |
| Modifier and Type | Method and Description |
|---|---|
void |
addAttachment(OutlookMessageAttachment attachment)
Add a new attachment to this message.
|
OutlookMessageAttachment |
addAttachment(String name,
String mimeType,
InputStream input)
Add a new attachment to this message.
|
OutlookMessageAttachment |
addAttachment(String name,
String mimeType,
OutlookMessageAttachment.InputStreamCreator inputStreamCreator)
Add a new attachment to this message.
|
OutlookMessageRecipient |
addRecipient(OutlookMessageRecipient.Type type,
String email)
Creates and add a new
OutlookMessageRecipient to this message. |
OutlookMessageRecipient |
addRecipient(OutlookMessageRecipient.Type type,
String email,
String name)
Creates and add a new
OutlookMessageRecipient to this message. |
void |
addRecipient(OutlookMessageRecipient recipient)
Add the specified
recipient to this message. |
static String |
extractEmail(String mixedMailStr)
Extracts the mail from a mixed mail string, for instance "John Doe <john.doe@hotmail.com>".
|
List<OutlookMessageRecipient> |
getAllRecipients()
Returns all the recipients of this message.
|
List<OutlookMessageAttachment> |
getAttachments()
Returns the attachments of this message.
|
String |
getFrom()
Defines the
From email address from which a message has been sent. |
String |
getPlainTextBody()
Defines the plain text body of the message.
|
List<OutlookMessageRecipient> |
getRecipients(OutlookMessageRecipient.Type type)
Returns all the recipients of the specified type.
|
List<String> |
getReplyTo()
Defines the addresses use when the user hits the 'reply' button.
|
String |
getSubject()
Defines the subject of the message.
|
void |
removeAllAttachments()
Removes all the attachments from this message.
|
void |
removeAllRecipients()
Removes all the recipients.
|
void |
removeAllRecipients(OutlookMessageRecipient.Type type)
Removes all the recipients of the given type.
|
void |
removeAttachment(OutlookMessageAttachment attachment)
Removes the specified attachment from this message.
|
void |
removeRecipient(OutlookMessageRecipient recipient)
Removes the specified
recipient from this message. |
void |
setFrom(String from) |
void |
setPlainTextBody(String plainTextBody) |
void |
setReplyTo(List<String> replyTo) |
void |
setSubject(String subject) |
javax.mail.internet.MimeMessage |
toMimeMessage()
Creates a new
MimeMessage from this OutlookMessage. |
javax.mail.internet.MimeMessage |
toMimeMessage(Properties sessionProps)
Creates a new
MimeMessage from this OutlookMessage. |
javax.mail.internet.MimeMessage |
toMimeMessage(javax.mail.Session session)
Creates a new
MimeMessage from this OutlookMessage. |
void |
writeTo(File file)
Writes the content of this message to the specified
file. |
void |
writeTo(OutputStream outputStream)
Writes the content of this message to the specified
outputStream. |
public OutlookMessage()
public OutlookMessage(InputStream mapiMessageInputStream) throws IOException
mapiMessageInputStream.mapiMessageInputStream - The source message data.IOException - If an I/O error occurs.public OutlookMessage(File mapiMessageFile) throws IOException
mapiMessageFile.mapiMessageFile - The source message data.IOException - If an I/O error occurs.public OutlookMessage(org.apache.poi.hsmf.MAPIMessage mapiMessage)
mapiMessage.
All the data will be copied from the source message and the latter can be then discarded.mapiMessage - The source message data.public String getSubject()
public void setSubject(String subject)
public String getPlainTextBody()
PidTagBodyHtml
not supported by Outlook).
This value may be null.public void setPlainTextBody(String plainTextBody)
public String getFrom()
From email address from which a message has been sent.
This value may be null.public void setFrom(String from)
public List<String> getReplyTo()
MimeMessage but isn't
used for .msg generation.
This value may be null.toMimeMessage()public List<OutlookMessageRecipient> getRecipients(OutlookMessageRecipient.Type type)
type - The recipients type.public List<OutlookMessageRecipient> getAllRecipients()
public OutlookMessageRecipient addRecipient(OutlookMessageRecipient.Type type, String email)
OutlookMessageRecipient to this message.type - The type.email - The email.public OutlookMessageRecipient addRecipient(OutlookMessageRecipient.Type type, String email, String name)
OutlookMessageRecipient to this message.type - The type.email - The email.name - The name or null.public void addRecipient(OutlookMessageRecipient recipient)
recipient to this message.recipient - The recipient to add.public void removeRecipient(OutlookMessageRecipient recipient)
recipient from this message.recipient - The recipient to remove.public void removeAllRecipients(OutlookMessageRecipient.Type type)
type - The type of recipients to remove.public void removeAllRecipients()
public List<OutlookMessageAttachment> getAttachments()
public OutlookMessageAttachment addAttachment(String name, String mimeType, InputStream input)
The data of the InputStream will be loaded into memory (see OutlookMessageAttachment.MemoryInputStreamCreator).
If you don't expect to invoke writeTo() or toMimeMessage() multiple times,
then consider using the other
method, which uses a OutlookMessageAttachment.InputStreamCreator.
name - The name.mimeType - The MIME type.input - The input data.public OutlookMessageAttachment addAttachment(String name, String mimeType, OutlookMessageAttachment.InputStreamCreator inputStreamCreator)
InputStreamCreator can be
set to the attachment later.
To use this method with a single-usage InputStream:
message.addAttachment("myAttachment", "text/plain", a -> myInputStream);name - The name.mimeType - The MIME type.inputStreamCreator - The InputStream creator or null.public void addAttachment(OutlookMessageAttachment attachment)
attachment - The attachment.public void removeAttachment(OutlookMessageAttachment attachment)
attachment - The attachment to remove.public void removeAllAttachments()
public javax.mail.internet.MimeMessage toMimeMessage()
throws IOException,
javax.mail.MessagingException
MimeMessage from this OutlookMessage.
A new Session will be created with an empty Properties instance.MimeMessage instance.IOExceptionjavax.mail.MessagingExceptiontoMimeMessage(java.util.Properties)public javax.mail.internet.MimeMessage toMimeMessage(Properties sessionProps) throws IOException, javax.mail.MessagingException
MimeMessage from this OutlookMessage.
A new Session will be created with the specified sessionProps.sessionProps - The Session properties.MimeMessage instance.IOExceptionjavax.mail.MessagingExceptiontoMimeMessage(javax.mail.Session)public javax.mail.internet.MimeMessage toMimeMessage(javax.mail.Session session)
throws IOException,
javax.mail.MessagingException
MimeMessage from this OutlookMessage.
This method will generate a multipart/mixed MimeMessage, with the first
part being the message body (named 'body').session - The Session to use for message creation.MimeMessage instance.IOExceptionjavax.mail.MessagingExceptionpublic static String extractEmail(String mixedMailStr)
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.
mixedMailStr - A string that contains (potentially) an email address.public void writeTo(File file) throws IOException
file. The created
file will be in format .msg that can be open by Microsoft Outlook.file - The .msg file to create.IOException - If an I/O error occurs.public void writeTo(OutputStream outputStream) throws IOException
outputStream. The
bytes written represent a .msg file that can be open by Microsoft Outlook.
The outputStream will remain open.outputStream - The stream to write to.IOException - If an I/O error occurs.Copyright © 2017. All rights reserved.