Top Description Fields Constructors Methods
org.springframework.mail.javamail

public Class MimeMessageHelper

extends Object
Class Inheritance
Imports
java.io.File, .IOException, .InputStream, .OutputStream, .UnsupportedEncodingException, java.util.Date, jakarta.activation.DataHandler, .DataSource, .FileDataSource, .FileTypeMap, jakarta.mail.BodyPart, .Message, .MessagingException, .Part, jakarta.mail.internet.AddressException, .InternetAddress, .MimeBodyPart, .MimeMessage, .MimeMultipart, .MimePart, .MimeUtility, org.springframework.core.io.InputStreamSource, .Resource, org.springframework.lang.Nullable, org.springframework.util.Assert

Helper class for populating a jakarta.mail.internet.MimeMessage.

Mirrors the simple setters of org.springframework.mail.SimpleMailMessage, 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.

Authors
Juergen Hoeller, Sam Brannen
Since
19.01.2004
See Also
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

Field Summary

Modifier and TypeField 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

Constructor Summary

AccessConstructor 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.

Method Summary

Modifier and TypeMethod 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 jakarta.activation.DataSource to take the content from, determining the InputStream and the content type
dataSource
)

Add an attachment to the MimeMessage, taking the content from a jakarta.activation.DataSource.

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 java.io.File.

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 org.springframework.core.io.InputStreamResource.

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 org.springframework.core.io.InputStreamResource.

public void
public void
public void
addBcc(String bcc, String personal)

public void
public void
public void
addCc(String cc, String personal)

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 jakarta.activation.DataSource to take the content from, determining the InputStream and the content type
dataSource
)

Add an inline element to the MimeMessage, taking the content from a jakarta.activation.DataSource.

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 java.io.File.

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 org.springframework.core.io.Resource.

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 org.springframework.core.InputStreamResource, and specifying the content type explicitly.

public void
public void
public void
addTo(String to, String personal)

protected DataSource

Returns:

the Activation Framework DataSource
createDataSource
(final InputStreamSource
the 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, or null if none found
getDefaultEncoding
(MimeMessage
the 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 message
getDefaultFileTypeMap
(MimeMessage
the passed-in MimeMessage
mimeMessage
)

Determine the default Java Activation FileTypeMap for the given MimeMessage.

public String
getEncoding()

Return the specific character encoding used for this message, if any.

public FileTypeMap
getFileTypeMap()

Return the FileTypeMap used by this MimeMessageHelper.

private MimeBodyPart
public final MimeMessage
getMimeMessage()

Return the underlying MimeMessage object.

public final MimeMultipart
getMimeMultipart()

Return the underlying MIME "multipart/related" object, if any.

public final MimeMultipart
getRootMimeMultipart()

Return the root MIME "multipart/mixed" object, if any.

public boolean
isEncodeFilenames()

Return whether to encode attachment filenames passed to this helper's #addAttachment methods.

public final boolean
isMultipart()

Return whether this helper is in multipart mode, i.e. whether it holds a multipart message.

public boolean
isValidateAddresses()

Return whether this helper will validate all addresses passed to it.

private InternetAddress
public void
public void
public void
public void
setBcc(String[] bcc)

public void
public void
public void
public void
setCc(String[] cc)

public void
setEncodeFilenames(boolean encodeFilenames)

Set whether to encode attachment filenames passed to this helper's #addAttachment methods.

public void
setFileTypeMap(FileTypeMap fileTypeMap)

Set the Java Activation Framework FileTypeMap to use for determining the content type of inline content and attachments that get added to the message.

public void
public void
public void
setFrom(String from, String personal)

private void
protected final void
setMimeMultiparts(MimeMultipart
the root MimeMultipart object, which attachments will be added to; or null to indicate no multipart at all
root
,
MimeMultipart
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)
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
setReplyTo(String replyTo)

public void
setReplyTo(String replyTo, String personal)

public void
setSentDate(Date
the date to set (never null)
sentDate
)

Set the sent-date of the message.

public void
setSubject(String
the subject text
subject
)

Set the subject of the message, using the correct encoding.

public void
setText(String
the text for the message
text
)

Set the given text directly as content in non-multipart mode or as default body part in multipart mode.

public void
setText(String
the text for the message
text
,
boolean
whether to apply content type "text/html" for an HTML mail, using default content type ("text/plain") else
html
)

Set the given text directly as content in non-multipart mode or as default body part in multipart mode.

public void
setText(String
the plain text for the message
plainText
,
String
the HTML text for the message
htmlText
)

Set the given plain text and HTML text as alternatives, offering both options to the email client.

public void
public void
public void
public void
setTo(String[] to)

public void
setValidateAddresses(boolean validateAddresses)

Set whether to validate all addresses which get passed to this helper.

protected void
validateAddress(InternetAddress
the address to validate
address
)

Validate the given mail address.

protected void
validateAddresses(InternetAddress[]
the addresses to validate
addresses
)

Validate all given mail addresses.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait