public class MimeMessageHelper extends Object
MimeMessage
.
Mirrors the simple setters of 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("me@mail.com"); message.setTo("you@mail.com"); 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
MailMessage
interface, just like
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 |
---|---|
static int |
MULTIPART_MODE_MIXED
Constant indicating a multipart message with a single root multipart
element of type "mixed".
|
static int |
MULTIPART_MODE_MIXED_RELATED
Constant indicating a multipart message with a root multipart element
"mixed" plus a nested multipart element of type "related".
|
static int |
MULTIPART_MODE_NO
Constant indicating a non-multipart message.
|
static int |
MULTIPART_MODE_RELATED
Constant indicating a multipart message with a single root multipart
element of type "related".
|
Constructor and Description |
---|
MimeMessageHelper(MimeMessage mimeMessage)
Create a new MimeMessageHelper for the given MimeMessage,
assuming a simple text message (no multipart content,
i.e.
|
MimeMessageHelper(MimeMessage mimeMessage,
boolean multipart)
Create a new MimeMessageHelper for the given MimeMessage,
in multipart mode (supporting alternative texts, inline
elements and attachments) if requested.
|
MimeMessageHelper(MimeMessage mimeMessage,
boolean multipart,
String encoding)
Create a new MimeMessageHelper for the given MimeMessage,
in multipart mode (supporting alternative texts, inline
elements and attachments) if requested.
|
MimeMessageHelper(MimeMessage mimeMessage,
int multipartMode)
Create a new MimeMessageHelper for the given MimeMessage,
in multipart mode (supporting alternative texts, inline
elements and attachments) if requested.
|
MimeMessageHelper(MimeMessage mimeMessage,
int multipartMode,
String encoding)
Create a new MimeMessageHelper for the given MimeMessage,
in multipart mode (supporting alternative texts, inline
elements and attachments) if requested.
|
MimeMessageHelper(MimeMessage mimeMessage,
String encoding)
Create a new MimeMessageHelper for the given MimeMessage,
assuming a simple text message (no multipart content,
i.e.
|
Modifier and Type | Method and Description |
---|---|
void |
addAttachment(String attachmentFilename,
DataSource dataSource)
Add an attachment to the MimeMessage, taking the content from a
javax.activation.DataSource . |
void |
addAttachment(String attachmentFilename,
File file)
Add an attachment to the MimeMessage, taking the content from a
java.io.File . |
void |
addAttachment(String attachmentFilename,
InputStreamSource inputStreamSource)
Add an attachment to the MimeMessage, taking the content from an
org.springframework.core.io.InputStreamResource . |
void |
addAttachment(String attachmentFilename,
InputStreamSource inputStreamSource,
String contentType)
Add an attachment to the MimeMessage, taking the content from an
org.springframework.core.io.InputStreamResource . |
void |
addBcc(InternetAddress bcc) |
void |
addBcc(String bcc) |
void |
addBcc(String bcc,
String personal) |
void |
addCc(InternetAddress cc) |
void |
addCc(String cc) |
void |
addCc(String cc,
String personal) |
void |
addInline(String contentId,
DataSource dataSource)
Add an inline element to the MimeMessage, taking the content from a
javax.activation.DataSource . |
void |
addInline(String contentId,
File file)
Add an inline element to the MimeMessage, taking the content from a
java.io.File . |
void |
addInline(String contentId,
InputStreamSource inputStreamSource,
String contentType)
Add an inline element to the MimeMessage, taking the content from an
org.springframework.core.InputStreamResource , and
specifying the content type explicitly. |
void |
addInline(String contentId,
Resource resource)
Add an inline element to the MimeMessage, taking the content from a
org.springframework.core.io.Resource . |
void |
addTo(InternetAddress to) |
void |
addTo(String to) |
void |
addTo(String to,
String personal) |
protected DataSource |
createDataSource(InputStreamSource inputStreamSource,
String contentType,
String name)
Create an Activation Framework DataSource for the given InputStreamSource.
|
protected void |
createMimeMultiparts(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.
|
protected String |
getDefaultEncoding(MimeMessage mimeMessage)
Determine the default encoding for the given MimeMessage.
|
protected FileTypeMap |
getDefaultFileTypeMap(MimeMessage mimeMessage)
Determine the default Java Activation FileTypeMap for the given MimeMessage.
|
String |
getEncoding()
Return the specific character encoding used for this message, if any.
|
FileTypeMap |
getFileTypeMap()
Return the
FileTypeMap used by this MimeMessageHelper. |
MimeMessage |
getMimeMessage()
Return the underlying MimeMessage object.
|
MimeMultipart |
getMimeMultipart()
Return the underlying MIME "multipart/related" object, if any.
|
MimeMultipart |
getRootMimeMultipart()
Return the root MIME "multipart/mixed" object, if any.
|
boolean |
isMultipart()
Return whether this helper is in multipart mode,
i.e.
|
boolean |
isValidateAddresses()
Return whether this helper will validate all addresses passed to it.
|
void |
setBcc(InternetAddress bcc) |
void |
setBcc(InternetAddress[] bcc) |
void |
setBcc(String bcc) |
void |
setBcc(String[] bcc) |
void |
setCc(InternetAddress cc) |
void |
setCc(InternetAddress[] cc) |
void |
setCc(String cc) |
void |
setCc(String[] cc) |
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. |
void |
setFrom(InternetAddress from) |
void |
setFrom(String from) |
void |
setFrom(String from,
String personal) |
protected void |
setMimeMultiparts(MimeMultipart root,
MimeMultipart main)
Set the given MimeMultipart objects for use by this MimeMessageHelper.
|
void |
setPriority(int priority)
Set the priority ("X-Priority" header) of the message.
|
void |
setReplyTo(InternetAddress replyTo) |
void |
setReplyTo(String replyTo) |
void |
setReplyTo(String replyTo,
String personal) |
void |
setSentDate(Date sentDate)
Set the sent-date of the message.
|
void |
setSubject(String subject)
Set the subject of the message, using the correct encoding.
|
void |
setText(String text)
Set the given text directly as content in non-multipart mode
or as default body part in multipart mode.
|
void |
setText(String text,
boolean html)
Set the given text directly as content in non-multipart mode
or as default body part in multipart mode.
|
void |
setText(String plainText,
String htmlText)
Set the given plain text and HTML text as alternatives, offering
both options to the email client.
|
void |
setTo(InternetAddress to) |
void |
setTo(InternetAddress[] to) |
void |
setTo(String to) |
void |
setTo(String[] to) |
void |
setValidateAddresses(boolean validateAddresses)
Set whether to validate all addresses which get passed to this helper.
|
protected void |
validateAddress(InternetAddress address)
Validate the given mail address.
|
protected void |
validateAddresses(InternetAddress[] addresses)
Validate all given mail addresses.
|
public static final int MULTIPART_MODE_NO
public static final int MULTIPART_MODE_MIXED
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.
public static final int MULTIPART_MODE_RELATED
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).
public static final int MULTIPART_MODE_MIXED_RELATED
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.
public MimeMessageHelper(MimeMessage mimeMessage)
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.
mimeMessage
- the mime message to work onMimeMessageHelper(javax.mail.internet.MimeMessage, boolean)
,
getDefaultEncoding(javax.mail.internet.MimeMessage)
,
JavaMailSenderImpl.setDefaultEncoding(java.lang.String)
public MimeMessageHelper(MimeMessage mimeMessage, @Nullable String encoding)
mimeMessage
- the mime message to work onencoding
- the character encoding to use for the messageMimeMessageHelper(javax.mail.internet.MimeMessage, boolean)
public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart) throws MessagingException
Consider using the MimeMessageHelper 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.
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)MessagingException
- if multipart creation failedMimeMessageHelper(javax.mail.internet.MimeMessage, int)
,
getDefaultEncoding(javax.mail.internet.MimeMessage)
,
JavaMailSenderImpl.setDefaultEncoding(java.lang.String)
public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart, @Nullable String encoding) throws MessagingException
Consider using the MimeMessageHelper constructor that takes a multipartMode argument to choose a specific multipart mode other than MULTIPART_MODE_MIXED_RELATED.
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 messageMessagingException
- if multipart creation failedMimeMessageHelper(javax.mail.internet.MimeMessage, int, String)
public MimeMessageHelper(MimeMessage mimeMessage, int multipartMode) throws MessagingException
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.
mimeMessage
- the mime message to work onmultipartMode
- which kind of multipart message to create
(MIXED, RELATED, MIXED_RELATED, or NO)MessagingException
- if multipart creation failedMULTIPART_MODE_NO
,
MULTIPART_MODE_MIXED
,
MULTIPART_MODE_RELATED
,
MULTIPART_MODE_MIXED_RELATED
,
getDefaultEncoding(javax.mail.internet.MimeMessage)
,
JavaMailSenderImpl.setDefaultEncoding(java.lang.String)
public MimeMessageHelper(MimeMessage mimeMessage, int multipartMode, @Nullable String encoding) throws MessagingException
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 messageMessagingException
- if multipart creation failedMULTIPART_MODE_NO
,
MULTIPART_MODE_MIXED
,
MULTIPART_MODE_RELATED
,
MULTIPART_MODE_MIXED_RELATED
public final MimeMessage getMimeMessage()
protected void createMimeMultiparts(MimeMessage mimeMessage, int multipartMode) throws MessagingException
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).
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)MessagingException
- if multipart creation failedsetMimeMultiparts(javax.mail.internet.MimeMultipart, javax.mail.internet.MimeMultipart)
,
MULTIPART_MODE_NO
,
MULTIPART_MODE_MIXED
,
MULTIPART_MODE_RELATED
,
MULTIPART_MODE_MIXED_RELATED
protected final void setMimeMultiparts(@Nullable MimeMultipart root, @Nullable MimeMultipart main)
root
- the root MimeMultipart object, which attachments will be added to;
or null
to 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)public final boolean isMultipart()
MimeMessageHelper(MimeMessage, boolean)
public final MimeMultipart getRootMimeMultipart() throws IllegalStateException
This will be the direct content of the MimeMessage, in case of a multipart mail.
IllegalStateException
- if this helper is not in multipart modeisMultipart()
,
getMimeMessage()
,
MimeMultipart.addBodyPart(javax.mail.BodyPart)
public final MimeMultipart getMimeMultipart() throws IllegalStateException
This will be nested within the root MimeMultipart, in case of a multipart mail.
IllegalStateException
- if this helper is not in multipart modeisMultipart()
,
getRootMimeMultipart()
,
MimeMultipart.addBodyPart(javax.mail.BodyPart)
@Nullable protected String getDefaultEncoding(MimeMessage mimeMessage)
mimeMessage
- the passed-in MimeMessagenull
if none found@Nullable public String getEncoding()
protected FileTypeMap getDefaultFileTypeMap(MimeMessage mimeMessage)
mimeMessage
- the passed-in MimeMessageConfigurableMimeFileTypeMap
public void setFileTypeMap(@Nullable FileTypeMap fileTypeMap)
FileTypeMap
to use
for determining the content type of inline content and attachments
that get added to the message.
Default is the FileTypeMap
that the underlying
MimeMessage carries, if any, or the Activation Framework's default
FileTypeMap
instance else.
addInline(java.lang.String, javax.activation.DataSource)
,
addAttachment(java.lang.String, javax.activation.DataSource)
,
getDefaultFileTypeMap(javax.mail.internet.MimeMessage)
,
JavaMailSenderImpl.setDefaultFileTypeMap(javax.activation.FileTypeMap)
,
FileTypeMap.getDefaultFileTypeMap()
,
ConfigurableMimeFileTypeMap
public FileTypeMap getFileTypeMap()
FileTypeMap
used by this MimeMessageHelper.public void setValidateAddresses(boolean validateAddresses)
Note that this is by default just available for JavaMail >= 1.3.
You can override the default validateAddress method
for
validation on older JavaMail versions (or for custom validation).
public boolean isValidateAddresses()
protected void validateAddress(InternetAddress address) throws AddressException
Default implementation invokes InternetAddress.validate()
,
provided that address validation is activated for the helper instance.
Note that this method will just work on JavaMail >= 1.3. You can override it for validation on older JavaMail versions or for custom validation.
address
- the address to validateAddressException
- if validation failedisValidateAddresses()
,
InternetAddress.validate()
protected void validateAddresses(InternetAddress[] addresses) throws AddressException
addresses
- the addresses to validateAddressException
- if validation failedvalidateAddress(InternetAddress)
public void setFrom(InternetAddress from) throws MessagingException
MessagingException
public void setFrom(String from) throws MessagingException
MessagingException
public void setFrom(String from, String personal) throws MessagingException, UnsupportedEncodingException
public void setReplyTo(InternetAddress replyTo) throws MessagingException
MessagingException
public void setReplyTo(String replyTo) throws MessagingException
MessagingException
public void setReplyTo(String replyTo, String personal) throws MessagingException, UnsupportedEncodingException
public void setTo(InternetAddress to) throws MessagingException
MessagingException
public void setTo(InternetAddress[] to) throws MessagingException
MessagingException
public void setTo(String to) throws MessagingException
MessagingException
public void setTo(String[] to) throws MessagingException
MessagingException
public void addTo(InternetAddress to) throws MessagingException
MessagingException
public void addTo(String to) throws MessagingException
MessagingException
public void addTo(String to, String personal) throws MessagingException, UnsupportedEncodingException
public void setCc(InternetAddress cc) throws MessagingException
MessagingException
public void setCc(InternetAddress[] cc) throws MessagingException
MessagingException
public void setCc(String cc) throws MessagingException
MessagingException
public void setCc(String[] cc) throws MessagingException
MessagingException
public void addCc(InternetAddress cc) throws MessagingException
MessagingException
public void addCc(String cc) throws MessagingException
MessagingException
public void addCc(String cc, String personal) throws MessagingException, UnsupportedEncodingException
public void setBcc(InternetAddress bcc) throws MessagingException
MessagingException
public void setBcc(InternetAddress[] bcc) throws MessagingException
MessagingException
public void setBcc(String bcc) throws MessagingException
MessagingException
public void setBcc(String[] bcc) throws MessagingException
MessagingException
public void addBcc(InternetAddress bcc) throws MessagingException
MessagingException
public void addBcc(String bcc) throws MessagingException
MessagingException
public void addBcc(String bcc, String personal) throws MessagingException, UnsupportedEncodingException
public void setPriority(int priority) throws MessagingException
priority
- the priority value;
typically between 1 (highest) and 5 (lowest)MessagingException
- in case of errorspublic void setSentDate(Date sentDate) throws MessagingException
sentDate
- the date to set (never null
)MessagingException
- in case of errorspublic void setSubject(String subject) throws MessagingException
subject
- the subject textMessagingException
- in case of errorspublic void setText(String text) throws MessagingException
NOTE: Invoke addInline(java.lang.String, javax.activation.DataSource)
after setText
;
else, mail readers might not be able to resolve inline references correctly.
text
- the text for the messageMessagingException
- in case of errorspublic void setText(String text, boolean html) throws MessagingException
NOTE: Invoke addInline(java.lang.String, javax.activation.DataSource)
after setText
;
else, mail readers might not be able to resolve inline references correctly.
text
- the text for the messagehtml
- whether to apply content type "text/html" for an
HTML mail, using default content type ("text/plain") elseMessagingException
- in case of errorspublic void setText(String plainText, String htmlText) throws MessagingException
NOTE: Invoke addInline(java.lang.String, javax.activation.DataSource)
after setText
;
else, mail readers might not be able to resolve inline references correctly.
plainText
- the plain text for the messagehtmlText
- the HTML text for the messageMessagingException
- in case of errorspublic void addInline(String contentId, DataSource dataSource) throws MessagingException
javax.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.
NOTE: Invoke addInline
after setText(java.lang.String)
;
else, mail readers might not be able to resolve inline references correctly.
contentId
- 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.dataSource
- the javax.activation.DataSource
to take
the content from, determining the InputStream and the content typeMessagingException
- in case of errorsaddInline(String, java.io.File)
,
addInline(String, org.springframework.core.io.Resource)
public void addInline(String contentId, File file) throws MessagingException
java.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)!
NOTE: Invoke addInline
after setText(java.lang.String)
;
else, mail readers might not be able to resolve inline references correctly.
contentId
- 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.file
- the File resource to take the content fromMessagingException
- in case of errorssetText(java.lang.String)
,
addInline(String, org.springframework.core.io.Resource)
,
addInline(String, javax.activation.DataSource)
public void addInline(String contentId, Resource resource) throws MessagingException
org.springframework.core.io.Resource
.
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)!
Note that the InputStream returned by the Resource implementation
needs to be a fresh one on each call, as JavaMail will invoke
getInputStream()
multiple times.
NOTE: Invoke addInline
after setText(java.lang.String)
;
else, mail readers might not be able to resolve inline references correctly.
contentId
- 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.resource
- the resource to take the content fromMessagingException
- in case of errorssetText(java.lang.String)
,
addInline(String, java.io.File)
,
addInline(String, javax.activation.DataSource)
public void addInline(String contentId, InputStreamSource inputStreamSource, String contentType) throws MessagingException
org.springframework.core.InputStreamResource
, and
specifying the content type explicitly.
You can determine the content type for any given filename via a Java Activation Framework's FileTypeMap, for example the one held by this helper.
Note that the InputStream returned by the InputStreamSource implementation
needs to be a fresh one on each call, as JavaMail will invoke
getInputStream()
multiple times.
NOTE: Invoke addInline
after setText
;
else, mail readers might not be able to resolve inline references correctly.
contentId
- 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.inputStreamSource
- the resource to take the content fromcontentType
- the content type to use for the elementMessagingException
- in case of errorssetText(java.lang.String)
,
getFileTypeMap()
,
addInline(String, org.springframework.core.io.Resource)
,
addInline(String, javax.activation.DataSource)
public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException
javax.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.
attachmentFilename
- the name of the attachment as it will
appear in the mail (the content type will be determined by this)dataSource
- the javax.activation.DataSource
to take
the content from, determining the InputStream and the content typeMessagingException
- in case of errorsaddAttachment(String, org.springframework.core.io.InputStreamSource)
,
addAttachment(String, java.io.File)
public void addAttachment(String attachmentFilename, File file) throws MessagingException
java.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)!
attachmentFilename
- the name of the attachment as it will
appear in the mailfile
- the File resource to take the content fromMessagingException
- in case of errorsaddAttachment(String, org.springframework.core.io.InputStreamSource)
,
addAttachment(String, javax.activation.DataSource)
public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource) throws MessagingException
org.springframework.core.io.InputStreamResource
.
The content type will be determined by the given filename for the attachment. Thus, any content source will be fine, including temporary files with arbitrary filenames.
Note that the InputStream returned by the InputStreamSource
implementation needs to be a fresh one on each call, as
JavaMail will invoke getInputStream()
multiple times.
attachmentFilename
- the name of the attachment as it will
appear in the mailinputStreamSource
- the resource to take the content from
(all of Spring's Resource implementations can be passed in here)MessagingException
- in case of errorsaddAttachment(String, java.io.File)
,
addAttachment(String, javax.activation.DataSource)
,
Resource
public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource, String contentType) throws MessagingException
org.springframework.core.io.InputStreamResource
.
Note that the InputStream returned by the InputStreamSource
implementation needs to be a fresh one on each call, as
JavaMail will invoke getInputStream()
multiple times.
attachmentFilename
- the name of the attachment as it will
appear in the mailinputStreamSource
- the resource to take the content from
(all of Spring's Resource implementations can be passed in here)contentType
- the content type to use for the elementMessagingException
- in case of errorsaddAttachment(String, java.io.File)
,
addAttachment(String, javax.activation.DataSource)
,
Resource
protected DataSource createDataSource(InputStreamSource inputStreamSource, String contentType, String name)
inputStreamSource
- the InputStreamSource (typically a Spring Resource)contentType
- the content typename
- the name of the DataSource