Class MimeMessageUtils
- java.lang.Object
-
- io.camunda.google.MimeMessageUtils
-
public class MimeMessageUtils extends java.lang.ObjectCopied and simplified from Spring MimeMessageHelper
-
-
Field Summary
Fields Modifier and Type Field Description static intMULTIPART_MODE_MIXEDConstant indicating a multipart message with a single root multipart element of type "mixed".static intMULTIPART_MODE_MIXED_RELATEDConstant indicating a multipart message with a root multipart element "mixed" plus a nested multipart element of type "related".static intMULTIPART_MODE_NOConstant indicating a non-multipart message.static intMULTIPART_MODE_RELATEDConstant indicating a multipart message with a single root multipart element of type "related".
-
Constructor Summary
Constructors Constructor Description MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage)Create a new MimeMessageUtils for the given MimeMessage, assuming a simple text message (no multipart content, i.e.MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage, boolean multipart)Create a new MimeMessageUtils for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage, boolean multipart, java.lang.String encoding)Create a new MimeMessageUtils for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage, int multipartMode)Create a new MimeMessageUtils for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage, int multipartMode, java.lang.String encoding)Create a new MimeMessageUtils for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage, java.lang.String encoding)Create a new MimeMessageUtils for the given MimeMessage, assuming a simple text message (no multipart content, i.e.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAttachment(java.lang.String attachmentFilename, java.io.File file)Add an attachment to the MimeMessage, taking the content from ajava.io.File.voidaddAttachment(java.lang.String attachmentFilename, javax.activation.DataSource dataSource)Add an attachment to the MimeMessage, taking the content from ajakarta.activation.DataSource.protected voidcreateMimeMultiparts(javax.mail.internet.MimeMessage mimeMessage, int multipartMode)Determine the MimeMultipart objects to use, which will be used to store attachments on the one hand and text(s) and inline elements on the other hand.java.lang.StringgetEncoding()javax.mail.internet.MimeMessagegetMimeMessage()javax.mail.internet.MimeMultipartgetMimeMultipart()Return the underlying MIME "multipart/related" object, if any.javax.mail.internet.MimeMultipartgetRootMimeMultipart()Return the root MIME "multipart/mixed" object, if any.booleanisEncodeFilenames()Return whether to encode attachment filenames passed to this helper's#addAttachmentmethods.booleanisMultipart()voidsetBcc(java.lang.String bcc)voidsetBcc(java.lang.String[] bcc)voidsetBcc(javax.mail.internet.InternetAddress bcc)voidsetBcc(javax.mail.internet.InternetAddress[] bcc)voidsetCc(java.lang.String cc)voidsetCc(java.lang.String[] cc)voidsetCc(javax.mail.internet.InternetAddress cc)voidsetCc(javax.mail.internet.InternetAddress[] cc)voidsetEncodeFilenames(boolean encodeFilenames)Set whether to encode attachment filenames passed to this helper's#addAttachmentmethods.voidsetFrom(java.lang.String from)voidsetFrom(java.lang.String from, java.lang.String personal)voidsetFrom(javax.mail.internet.InternetAddress from)protected voidsetMimeMultiparts(javax.mail.internet.MimeMultipart root, javax.mail.internet.MimeMultipart main)Set the given MimeMultipart objects for use by this MimeMessageUtils.voidsetPriority(int priority)Set the priority ("X-Priority" header) of the message.voidsetReplyTo(java.lang.String replyTo)voidsetReplyTo(java.lang.String replyTo, java.lang.String personal)voidsetReplyTo(javax.mail.internet.InternetAddress replyTo)voidsetSentDate(java.util.Date sentDate)Set the sent-date of the message.voidsetSubject(java.lang.String subject)Set the subject of the message, using the correct encoding.voidsetText(java.lang.String text)Set the given text directly as content in non-multipart mode or as default body part in multipart mode.voidsetText(java.lang.String text, boolean html)Set the given text directly as content in non-multipart mode or as default body part in multipart mode.voidsetText(java.lang.String plainText, java.lang.String htmlText)Set the given plain text and HTML text as alternatives, offering both options to the email client.voidsetTo(java.lang.String to)voidsetTo(java.lang.String[] to)voidsetTo(javax.mail.internet.InternetAddress to)voidsetTo(javax.mail.internet.InternetAddress[] to)
-
-
-
Field Detail
-
MULTIPART_MODE_NO
public static final int MULTIPART_MODE_NO
Constant indicating a non-multipart message.- See Also:
- Constant Field Values
-
MULTIPART_MODE_MIXED
public static final int MULTIPART_MODE_MIXED
Constant indicating a multipart message with a single root multipart element of type "mixed". Texts, inline elements and attachements will all get added to that root element.This was Spring 1.0's default behavior. It is known to work properly on Outlook. However, other mail clients tend to misinterpret inline elements as attachments and/or show attachments inline as well.
- See Also:
- Constant Field Values
-
MULTIPART_MODE_RELATED
public static final int MULTIPART_MODE_RELATED
Constant indicating a multipart message with a single root multipart element of type "related". Texts, inline elements and attachements will all get added to that root element.This was the default behavior from Spring 1.1 up to 1.2 final. This is the "Microsoft multipart mode", as natively sent by Outlook. It is known to work properly on Outlook, Outlook Express, Yahoo Mail, and to a large degree also on Mac Mail (with an additional attachment listed for an inline element, despite the inline element also shown inline). Does not work properly on Lotus Notes (attachments won't be shown there).
- See Also:
- Constant Field Values
-
MULTIPART_MODE_MIXED_RELATED
public static final int MULTIPART_MODE_MIXED_RELATED
Constant indicating a multipart message with a root multipart element "mixed" plus a nested multipart element of type "related". Texts and inline elements will get added to the nested "related" element, while attachments will get added to the "mixed" root element.This is the default since Spring 1.2.1. This is arguably the most correct MIME structure, according to the MIME spec: It is known to work properly on Outlook, Outlook Express, Yahoo Mail, and Lotus Notes. Does not work properly on Mac Mail. If you target Mac Mail or experience issues with specific mails on Outlook, consider using MULTIPART_MODE_RELATED instead.
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MimeMessageUtils
public MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage)
Create a new MimeMessageUtils for the given MimeMessage, assuming a simple text message (no multipart content, i.e. no alternative texts and no inline elements or attachments).The character encoding for the message will be taken from the passed-in MimeMessage object, if carried there. Else, JavaMail's default encoding will be used.
- Parameters:
mimeMessage- the mime message to work on
-
MimeMessageUtils
public MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage, java.lang.String encoding)Create a new MimeMessageUtils for the given MimeMessage, assuming a simple text message (no multipart content, i.e. no alternative texts and no inline elements or attachments).- Parameters:
mimeMessage- the mime message to work onencoding- the character encoding to use for the message
-
MimeMessageUtils
public MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage, boolean multipart) throws javax.mail.MessagingExceptionCreate a new MimeMessageUtils for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.Consider using the MimeMessageUtils constructor that takes a multipartMode argument to choose a specific multipart mode other than MULTIPART_MODE_MIXED_RELATED.
The character encoding for the message will be taken from the passed-in MimeMessage object, if carried there. Else, JavaMail's default encoding will be used.
- Parameters:
mimeMessage- the mime message to work onmultipart- whether to create a multipart message that supports alternative texts, inline elements and attachments (corresponds to MULTIPART_MODE_MIXED_RELATED)- Throws:
javax.mail.MessagingException- if multipart creation failed
-
MimeMessageUtils
public MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage, boolean multipart, java.lang.String encoding) throws javax.mail.MessagingExceptionCreate a new MimeMessageUtils for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.Consider using the MimeMessageUtils constructor that takes a multipartMode argument to choose a specific multipart mode other than MULTIPART_MODE_MIXED_RELATED.
- Parameters:
mimeMessage- the mime message to work onmultipart- whether to create a multipart message that supports alternative texts, inline elements and attachments (corresponds to MULTIPART_MODE_MIXED_RELATED)encoding- the character encoding to use for the message- Throws:
javax.mail.MessagingException- if multipart creation failed
-
MimeMessageUtils
public MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage, int multipartMode) throws javax.mail.MessagingExceptionCreate a new MimeMessageUtils for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.The character encoding for the message will be taken from the passed-in MimeMessage object, if carried there. Else, JavaMail's default encoding will be used.
- Parameters:
mimeMessage- the mime message to work onmultipartMode- which kind of multipart message to create (MIXED, RELATED, MIXED_RELATED, or NO)- Throws:
javax.mail.MessagingException- if multipart creation failed
-
MimeMessageUtils
public MimeMessageUtils(javax.mail.internet.MimeMessage mimeMessage, int multipartMode, java.lang.String encoding) throws javax.mail.MessagingExceptionCreate a new MimeMessageUtils for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested.- Parameters:
mimeMessage- the mime message to work onmultipartMode- which kind of multipart message to create (MIXED, RELATED, MIXED_RELATED, or NO)encoding- the character encoding to use for the message- Throws:
javax.mail.MessagingException- if multipart creation failed
-
-
Method Detail
-
getMimeMessage
public final javax.mail.internet.MimeMessage getMimeMessage()
-
createMimeMultiparts
protected void createMimeMultiparts(javax.mail.internet.MimeMessage mimeMessage, int multipartMode) throws javax.mail.MessagingExceptionDetermine the MimeMultipart objects to use, which will be used to store attachments on the one hand and text(s) and inline elements on the other hand.Texts and inline elements can either be stored in the root element itself (MULTIPART_MODE_MIXED, MULTIPART_MODE_RELATED) or in a nested element rather than the root element directly (MULTIPART_MODE_MIXED_RELATED).
By default, the root MimeMultipart element will be of type "mixed" (MULTIPART_MODE_MIXED) or "related" (MULTIPART_MODE_RELATED). The main multipart element will either be added as nested element of type "related" (MULTIPART_MODE_MIXED_RELATED) or be identical to the root element itself (MULTIPART_MODE_MIXED, MULTIPART_MODE_RELATED).
- Parameters:
mimeMessage- the MimeMessage object to add the root MimeMultipart object tomultipartMode- the multipart mode, as passed into the constructor (MIXED, RELATED, MIXED_RELATED, or NO)- Throws:
javax.mail.MessagingException- if multipart creation failed
-
setMimeMultiparts
protected final void setMimeMultiparts(@Nullable javax.mail.internet.MimeMultipart root, @Nullable javax.mail.internet.MimeMultipart main)Set the given MimeMultipart objects for use by this MimeMessageUtils.- Parameters:
root- the root MimeMultipart object, which attachments will be added to; ornullto indicate no multipart at allmain- the main MimeMultipart object, which text(s) and inline elements will be added to (can be the same as the root multipart object, or an element nested underneath the root multipart element)
-
isMultipart
public final boolean isMultipart()
-
getRootMimeMultipart
public final javax.mail.internet.MimeMultipart getRootMimeMultipart() throws java.lang.IllegalStateExceptionReturn the root MIME "multipart/mixed" object, if any. Can be used to manually add attachments.This will be the direct content of the MimeMessage, in case of a multipart mail.
- Returns:
- the root MIME "multipart/mixed" object
- Throws:
java.lang.IllegalStateException- if this helper is not in multipart mode
-
getMimeMultipart
public final javax.mail.internet.MimeMultipart getMimeMultipart() throws java.lang.IllegalStateExceptionReturn the underlying MIME "multipart/related" object, if any. Can be used to manually add body parts, inline elements, etc.This will be nested within the root MimeMultipart, in case of a multipart mail.
- Returns:
- the root MIME "multipart/related" object
- Throws:
java.lang.IllegalStateException- if this helper is not in multipart mode
-
getEncoding
public java.lang.String getEncoding()
-
setEncodeFilenames
public void setEncodeFilenames(boolean encodeFilenames)
Set whether to encode attachment filenames passed to this helper's#addAttachmentmethods.The default is
falsefor standard MIME behavior; turn this totruefor compatibility with older email clients. On a related note, check out JavaMail'smail.mime.encodefilenamesystem property.NOTE: The default changed to
falsein 5.3, in favor of JavaMail's standardmail.mime.encodefilenamesystem property.- Parameters:
encodeFilenames- whether to encode attachment filenames
-
isEncodeFilenames
public boolean isEncodeFilenames()
Return whether to encode attachment filenames passed to this helper's#addAttachmentmethods.
-
setFrom
public void setFrom(javax.mail.internet.InternetAddress from) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setFrom
public void setFrom(java.lang.String from) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setFrom
public void setFrom(java.lang.String from, java.lang.String personal) throws javax.mail.MessagingException, java.io.UnsupportedEncodingException- Throws:
javax.mail.MessagingExceptionjava.io.UnsupportedEncodingException
-
setReplyTo
public void setReplyTo(javax.mail.internet.InternetAddress replyTo) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setReplyTo
public void setReplyTo(java.lang.String replyTo) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setReplyTo
public void setReplyTo(java.lang.String replyTo, java.lang.String personal) throws javax.mail.MessagingException, java.io.UnsupportedEncodingException- Throws:
javax.mail.MessagingExceptionjava.io.UnsupportedEncodingException
-
setTo
public void setTo(javax.mail.internet.InternetAddress to) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setTo
public void setTo(javax.mail.internet.InternetAddress[] to) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setTo
public void setTo(java.lang.String to) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setTo
public void setTo(java.lang.String[] to) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setCc
public void setCc(javax.mail.internet.InternetAddress cc) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setCc
public void setCc(javax.mail.internet.InternetAddress[] cc) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setCc
public void setCc(java.lang.String cc) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setCc
public void setCc(java.lang.String[] cc) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setBcc
public void setBcc(javax.mail.internet.InternetAddress bcc) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setBcc
public void setBcc(javax.mail.internet.InternetAddress[] bcc) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setBcc
public void setBcc(java.lang.String bcc) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setBcc
public void setBcc(java.lang.String[] bcc) throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
setPriority
public void setPriority(int priority) throws javax.mail.MessagingExceptionSet the priority ("X-Priority" header) of the message.- Parameters:
priority- the priority value; typically between 1 (highest) and 5 (lowest)- Throws:
javax.mail.MessagingException- in case of errors
-
setSentDate
public void setSentDate(java.util.Date sentDate) throws javax.mail.MessagingExceptionSet the sent-date of the message.- Parameters:
sentDate- the date to set (nevernull)- Throws:
javax.mail.MessagingException- in case of errors
-
setSubject
public void setSubject(java.lang.String subject) throws javax.mail.MessagingExceptionSet the subject of the message, using the correct encoding.- Parameters:
subject- the subject text- Throws:
javax.mail.MessagingException- in case of errors
-
setText
public void setText(java.lang.String text) throws javax.mail.MessagingExceptionSet the given text directly as content in non-multipart mode or as default body part in multipart mode. Always applies the default content type "text/plain". else, mail readers might not be able to resolve inline references correctly.- Parameters:
text- the text for the message- Throws:
javax.mail.MessagingException- in case of errors
-
setText
public void setText(java.lang.String text, boolean html) throws javax.mail.MessagingExceptionSet the given text directly as content in non-multipart mode or as default body part in multipart mode. The "html" flag determines the content type to apply. else, mail readers might not be able to resolve inline references correctly.- Parameters:
text- the text for the messagehtml- whether to apply content type "text/html" for an HTML mail, using default content type ("text/plain") else- Throws:
javax.mail.MessagingException- in case of errors
-
setText
public void setText(java.lang.String plainText, java.lang.String htmlText) throws javax.mail.MessagingExceptionSet the given plain text and HTML text as alternatives, offering both options to the email client. Requires multipart mode. else, mail readers might not be able to resolve inline references correctly.- Parameters:
plainText- the plain text for the messagehtmlText- the HTML text for the message- Throws:
javax.mail.MessagingException- in case of errors
-
addAttachment
public void addAttachment(java.lang.String attachmentFilename, javax.activation.DataSource dataSource) throws javax.mail.MessagingExceptionAdd an attachment to the MimeMessage, taking the content from ajakarta.activation.DataSource.Note that the InputStream returned by the DataSource implementation needs to be a fresh one on each call, as JavaMail will invoke
getInputStream()multiple times.- Parameters:
attachmentFilename- the name of the attachment as it will appear in the mail (the content type will be determined by this)dataSource- thejakarta.activation.DataSourceto take the content from, determining the InputStream and the content type- Throws:
javax.mail.MessagingException- in case of errors
-
addAttachment
public void addAttachment(java.lang.String attachmentFilename, java.io.File file) throws javax.mail.MessagingExceptionAdd an attachment to the MimeMessage, taking the content from ajava.io.File.The content type will be determined by the name of the given content file. Do not use this for temporary files with arbitrary filenames (possibly ending in ".tmp" or the like)!
- Parameters:
attachmentFilename- the name of the attachment as it will appear in the mailfile- the File resource to take the content from- Throws:
javax.mail.MessagingException- in case of errors
-
-