jakarta.mail.internet.MimeMessage .
Mirrors the simple setters of org.,
directly applying the values to the underlying MimeMessage. Allows for defining
a character encoding for the entire message, automatically applied by all methods
of this helper class.
Offers support for HTML text content, inline elements such as images, and typical mail attachments. Also supports personal names that accompany mail addresses. Note that advanced settings can still be applied directly to the underlying MimeMessage object!
Typically used in MimeMessagePreparator implementations or
JavaMailSender client code: simply instantiating it as a MimeMessage wrapper,
invoking setters on the wrapper, using the underlying MimeMessage for mail sending.
Also used internally by JavaMailSenderImpl.
Sample code for an HTML mail with an inline image and a PDF attachment:
mailSender.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws MessagingException {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setFrom("[email protected]");
message.setTo("[email protected]");
message.setSubject("my subject");
message.setText("my text <img src='cid:myLogo'>", true);
message.addInline("myLogo", new ClassPathResource("img/mylogo.gif"));
message.addAttachment("myDocument.pdf", new ClassPathResource("doc/myDocument.pdf"));
}
});
Consider using MimeMailMessage (which implements the common
org.springframework.mail.MailMessage interface, just like
org.springframework.mail.SimpleMailMessage ) on top of this helper,
in order to let message population code interact with a simple message
or a MIME message through a common interface.
Warning regarding multipart mails: Simple MIME messages that just contain HTML text but no inline elements or attachments will work on more or less any email client that is capable of HTML rendering. However, inline elements and attachments are still a major compatibility issue between email clients: It's virtually impossible to get inline elements and attachments working across Microsoft Outlook, Lotus Notes and Mac Mail. Consider choosing a specific multipart mode for your needs: The javadoc on the MULTIPART_MODE constants contains more detailed information.
setText(String, boolean), setText(String, String), addInline(String, org.springframework.core.io.Resource) , addAttachment(String, org.springframework.core.io.InputStreamSource) , MULTIPART_MODE_MIXED_RELATED, MULTIPART_MODE_RELATED, getMimeMessage(), JavaMailSender
| Modifier and Type | Field and Description |
|---|---|
| private static final String | |
| private static final String | |
| private static final String | |
| private boolean | |
| private final String | |
| private FileTypeMap | |
| private static final String | |
| private final MimeMessage | |
| private MimeMultipart | |
| public static final int | MULTIPART_MODE_MIXED
Constant indicating a multipart message with a single root multipart element of type "mixed". |
| 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". |
| public static final int | MULTIPART_MODE_NO
Constant indicating a non-multipart message. |
| public static final int | MULTIPART_MODE_RELATED
Constant indicating a multipart message with a single root multipart element of type "related". |
| private static final String | |
| private static final String | |
| private static final String | |
| private MimeMultipart | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public | MimeMessageHelper(MimeMessage
the mime message to work on mimeMessage)Create a new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content, i.e. no alternative texts and no inline elements or attachments). |
| public | MimeMessageHelper(MimeMessage
the mime message to work on mimeMessage, String the character encoding to use for the message encoding)Create a new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content, i.e. no alternative texts and no inline elements or attachments). |
| public | MimeMessageHelper(MimeMessage
the mime message to work on mimeMessage, boolean whether to create a multipart message that
supports alternative texts, inline elements and attachments
(corresponds to MULTIPART_MODE_MIXED_RELATED) multipart)Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested. |
| public | MimeMessageHelper(MimeMessage
the mime message to work on mimeMessage, boolean whether to create a multipart message that
supports alternative texts, inline elements and attachments
(corresponds to MULTIPART_MODE_MIXED_RELATED) multipart, String the character encoding to use for the message encoding)Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested. |
| public | MimeMessageHelper(MimeMessage
the mime message to work on mimeMessage, int which kind of multipart message to create
(MIXED, RELATED, MIXED_RELATED, or NO) multipartMode)Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested. |
| public | MimeMessageHelper(MimeMessage
the mime message to work on mimeMessage, int which kind of multipart message to create
(MIXED, RELATED, MIXED_RELATED, or NO) multipartMode, String the character encoding to use for the message encoding)Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting alternative texts, inline elements and attachments) if requested. |
| Modifier and Type | Method and Description |
|---|---|
| public void | addAttachment(String
the name of the attachment as it will
appear in the mail (the content type will be determined by this) attachmentFilename, DataSource the dataSource)jakarta.activation.DataSource to take
the content from, determining the InputStream and the content typeAdd an attachment to the MimeMessage, taking the content from a
|
| public void | addAttachment(String
the name of the attachment as it will
appear in the mail attachmentFilename, File the File resource to take the content from file)Add an attachment to the MimeMessage, taking the content from a
|
| public void | addAttachment(String
the name of the attachment as it will
appear in the mail attachmentFilename, InputStreamSource the resource to take the content from
(all of Spring's Resource implementations can be passed in here) inputStreamSource)Add an attachment to the MimeMessage, taking the content from an
|
| public void | addAttachment(String
the name of the attachment as it will
appear in the mail attachmentFilename, InputStreamSource the resource to take the content from
(all of Spring's Resource implementations can be passed in here) inputStreamSource, String the content type to use for the element contentType)Add an attachment to the MimeMessage, taking the content from an
|
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | addInline(String
the content ID to use. Will end up as "Content-ID" header
in the body part, surrounded by angle brackets: e.g. "myId" → "<myId>".
Can be referenced in HTML source via src="cid:myId" expressions. contentId, DataSource the dataSource)jakarta.activation.DataSource to take
the content from, determining the InputStream and the content typeAdd an inline element to the MimeMessage, taking the content from a
|
| public void | addInline(String
the content ID to use. Will end up as "Content-ID" header
in the body part, surrounded by angle brackets: e.g. "myId" → "<myId>".
Can be referenced in HTML source via src="cid:myId" expressions. contentId, File the File resource to take the content from file)Add an inline element to the MimeMessage, taking the content from a
|
| public void | addInline(String
the content ID to use. Will end up as "Content-ID" header
in the body part, surrounded by angle brackets: e.g. "myId" → "<myId>".
Can be referenced in HTML source via src="cid:myId" expressions. contentId, Resource the resource to take the content from resource)Add an inline element to the MimeMessage, taking the content from a
|
| public void | addInline(String
the content ID to use. Will end up as "Content-ID" header
in the body part, surrounded by angle brackets: e.g. "myId" → "<myId>".
Can be referenced in HTML source via src="cid:myId" expressions. contentId, InputStreamSource the resource to take the content from inputStreamSource, String the content type to use for the element contentType)Add an inline element to the MimeMessage, taking the content from an
|
| public void | |
| public void | |
| public void | |
| protected DataSource | Returns: the Activation Framework DataSourcethe InputStreamSource (typically a Spring Resource) inputStreamSource, final String the content type contentType, final String the name of the DataSource name)Create an Activation Framework DataSource for the given InputStreamSource. |
| protected void | createMimeMultiparts(MimeMessage
the MimeMessage object to add the root MimeMultipart
object to mimeMessage, int the multipart mode, as passed into the constructor
(MIXED, RELATED, MIXED_RELATED, or NO) 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. |
| protected String | Returns: the default encoding associated with the MimeMessage, ornull if none foundthe passed-in MimeMessage mimeMessage)Determine the default encoding for the given MimeMessage. |
| protected FileTypeMap | Returns: the default FileTypeMap associated with the MimeMessage, or a default ConfigurableMimeFileTypeMap if none found for the messagethe passed-in MimeMessage mimeMessage)Determine the default Java Activation FileTypeMap for the given MimeMessage. |
| public String | |
| public FileTypeMap | |
| private MimeBodyPart | |
| public final MimeMessage | |
| public final MimeMultipart | |
| public final MimeMultipart | |
| public boolean | isEncodeFilenames()
Return whether to encode attachment filenames passed to this helper's
|
| public final boolean | isMultipart()
Return whether this helper is in multipart mode, i.e. whether it holds a multipart message. |
| public boolean | |
| private InternetAddress | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | setEncodeFilenames(boolean encodeFilenames)
Set whether to encode attachment filenames passed to this helper's
|
| public void | setFileTypeMap(FileTypeMap fileTypeMap)
Set the Java Activation Framework |
| public void | |
| public void | |
| public void | |
| private void | |
| protected final void | setMimeMultiparts(MimeMultipart
the root MimeMultipart object, which attachments will be added to;
or root, MimeMultipart null to indicate no multipart at allthe 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) main)Set the given MimeMultipart objects for use by this MimeMessageHelper. |
| private void | |
| public void | setPriority(int
the priority value;
typically between 1 (highest) and 5 (lowest) priority)Set the priority ("X-Priority" header) of the message. |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | setSubject(String
the subject text subject)Set the subject of the message, using the correct encoding. |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | setValidateAddresses(boolean validateAddresses)
Set whether to validate all addresses which get passed to this helper. |
| protected void | |
| protected void | validateAddresses(InternetAddress[]
the addresses to validate addresses)Validate all given mail addresses. |