|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.mail.javamail.MimeMessageHelper
Helper class for easy population of a javax.mail.internet.MimeMessage
.
Mirrors the simple setters of SimpleMailMessage, directly applying the values to the underlying MimeMessage. Allows to define a character encoding for the entire message, automatically applied by all methods of this helper.
Also offers support for typical mail attachments, and for personal names that accompany mail addresses. Note that advanced settings can still be applied directly to underlying MimeMessage!
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("images/mylogo.gif")); message.addAttachment("myDocument.pdf", new ClassPathResource("doc/myDocument.pdf")); } });
getMimeMessage()
,
MimeMessagePreparator
,
JavaMailSender
,
JavaMailSenderImpl
,
MimeMessage
,
SimpleMailMessage
Constructor Summary | |
MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage)
Create a new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content). |
|
MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage,
boolean multipart)
Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting attachments) if requested. |
|
MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage,
boolean multipart,
java.lang.String encoding)
Create a new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting attachments) if requested. |
|
MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage,
java.lang.String encoding)
Create a new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content). |
Method Summary | |
void |
addAttachment(java.lang.String attachmentFilename,
javax.activation.DataSource dataSource)
Add an attachment to the MimeMessage, taking the content from a javax.activation.DataSource .
|
void |
addAttachment(java.lang.String attachmentFilename,
java.io.File file)
Add an attachment to the MimeMessage, taking the content from a java.io.File .
|
void |
addAttachment(java.lang.String attachmentFilename,
InputStreamSource inputStreamSource)
Add an attachment to the MimeMessage, taking the content from an org.springframework.core.io.InputStreamResource .
|
void |
addBcc(javax.mail.internet.InternetAddress bcc)
|
void |
addBcc(java.lang.String bcc)
|
void |
addBcc(java.lang.String bcc,
java.lang.String personal)
|
void |
addCc(javax.mail.internet.InternetAddress cc)
|
void |
addCc(java.lang.String cc)
|
void |
addCc(java.lang.String cc,
java.lang.String personal)
|
void |
addInline(java.lang.String contentId,
javax.activation.DataSource dataSource)
Add an inline element to the MimeMessage, taking the content from a javax.activation.DataSource .
|
void |
addInline(java.lang.String contentId,
java.io.File file)
Add an inline element to the MimeMessage, taking the content from a java.io.File .
|
void |
addInline(java.lang.String contentId,
InputStreamSource inputStreamSource,
java.lang.String contentType)
Add an inline element to the MimeMessage, taking the content from an org.springframework.core.InputStreamResource .
|
void |
addInline(java.lang.String contentId,
Resource resource)
Add an inline element to the MimeMessage, taking the content from an org.springframework.core.io.InputStreamResource .
|
void |
addTo(javax.mail.internet.InternetAddress to)
|
void |
addTo(java.lang.String to)
|
void |
addTo(java.lang.String to,
java.lang.String personal)
|
protected javax.activation.DataSource |
createDataSource(InputStreamSource inputStreamSource,
java.lang.String contentType,
java.lang.String name)
Create an Activation Framework DataSource for the given InputStreamSource. |
java.lang.String |
getEncoding()
Return the character encoding used for this message. |
javax.mail.internet.MimeMessage |
getMimeMessage()
Return the underlying MimeMessage. |
javax.mail.internet.MimeMultipart |
getMimeMultipart()
Return the underlying MIME multipart object, if any |
boolean |
isMultipart()
Return whether this helper is in multipart mode, i.e. holds a multipart message. |
boolean |
isValidateAddresses()
Return whether this helper will validate all addresses passed to it. |
void |
setBcc(javax.mail.internet.InternetAddress bcc)
|
void |
setBcc(javax.mail.internet.InternetAddress[] bcc)
|
void |
setBcc(java.lang.String bcc)
|
void |
setBcc(java.lang.String[] bcc)
|
void |
setCc(javax.mail.internet.InternetAddress cc)
|
void |
setCc(javax.mail.internet.InternetAddress[] cc)
|
void |
setCc(java.lang.String cc)
|
void |
setCc(java.lang.String[] cc)
|
void |
setFrom(javax.mail.internet.InternetAddress from)
|
void |
setFrom(java.lang.String from)
|
void |
setFrom(java.lang.String from,
java.lang.String personal)
|
void |
setReplyTo(javax.mail.internet.InternetAddress replyTo)
|
void |
setReplyTo(java.lang.String replyTo)
|
void |
setReplyTo(java.lang.String replyTo,
java.lang.String personal)
|
void |
setSentDate(java.util.Date sentDate)
|
void |
setSubject(java.lang.String subject)
|
void |
setText(java.lang.String text)
|
void |
setText(java.lang.String text,
boolean html)
Set the given text directly as content in non-multipart mode respectively as default body part in multipart mode. |
void |
setTo(javax.mail.internet.InternetAddress to)
|
void |
setTo(javax.mail.internet.InternetAddress[] to)
|
void |
setTo(java.lang.String to)
|
void |
setTo(java.lang.String[] to)
|
void |
setValidateAddresses(boolean validateAddresses)
Set whether to validate all addresses which get passed to this helper. |
protected void |
validateAddress(javax.mail.internet.InternetAddress address)
Validate the given mail address. |
protected void |
validateAddresses(javax.mail.internet.InternetAddress[] addresses)
Validate all given mail addresses. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage)
mimeMessage
- MimeMessage to work onMimeMessageHelper(javax.mail.internet.MimeMessage, boolean)
public MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage, java.lang.String encoding)
mimeMessage
- MimeMessage to work onencoding
- the character encoding to use for the messageMimeMessageHelper(javax.mail.internet.MimeMessage, boolean)
public MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage, boolean multipart) throws javax.mail.MessagingException
mimeMessage
- MimeMessage to work onmultipart
- whether to create a multipart message that
supports attachmentspublic MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage, boolean multipart, java.lang.String encoding) throws javax.mail.MessagingException
mimeMessage
- MimeMessage to work onmultipart
- whether to create a multipart message that
supports attachmentsencoding
- the character encoding to use for the messageMethod Detail |
public final javax.mail.internet.MimeMessage getMimeMessage()
public final boolean isMultipart()
MimeMessageHelper(MimeMessage, boolean)
public final javax.mail.internet.MimeMultipart getMimeMultipart() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if this helper is not in multipart modeisMultipart()
public java.lang.String getEncoding()
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.
validateAddress(javax.mail.internet.InternetAddress)
public boolean isValidateAddresses()
protected void validateAddress(javax.mail.internet.InternetAddress address) throws javax.mail.internet.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 validate
javax.mail.internet.AddressException
- if validation failedisValidateAddresses()
,
InternetAddress.validate()
protected void validateAddresses(javax.mail.internet.InternetAddress[] addresses) throws javax.mail.internet.AddressException
addresses
- the addresses to validate
javax.mail.internet.AddressException
- if validation failedvalidateAddress(InternetAddress)
public void setFrom(javax.mail.internet.InternetAddress from) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setFrom(java.lang.String from) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setFrom(java.lang.String from, java.lang.String personal) throws javax.mail.MessagingException, java.io.UnsupportedEncodingException
javax.mail.MessagingException
java.io.UnsupportedEncodingException
public void setReplyTo(javax.mail.internet.InternetAddress replyTo) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setReplyTo(java.lang.String replyTo) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setReplyTo(java.lang.String replyTo, java.lang.String personal) throws javax.mail.MessagingException, java.io.UnsupportedEncodingException
javax.mail.MessagingException
java.io.UnsupportedEncodingException
public void setTo(javax.mail.internet.InternetAddress to) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setTo(javax.mail.internet.InternetAddress[] to) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setTo(java.lang.String to) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setTo(java.lang.String[] to) throws javax.mail.MessagingException
javax.mail.MessagingException
public void addTo(javax.mail.internet.InternetAddress to) throws javax.mail.MessagingException
javax.mail.MessagingException
public void addTo(java.lang.String to) throws javax.mail.MessagingException
javax.mail.MessagingException
public void addTo(java.lang.String to, java.lang.String personal) throws javax.mail.MessagingException, java.io.UnsupportedEncodingException
javax.mail.MessagingException
java.io.UnsupportedEncodingException
public void setCc(javax.mail.internet.InternetAddress cc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setCc(javax.mail.internet.InternetAddress[] cc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setCc(java.lang.String cc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setCc(java.lang.String[] cc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void addCc(javax.mail.internet.InternetAddress cc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void addCc(java.lang.String cc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void addCc(java.lang.String cc, java.lang.String personal) throws javax.mail.MessagingException, java.io.UnsupportedEncodingException
javax.mail.MessagingException
java.io.UnsupportedEncodingException
public void setBcc(javax.mail.internet.InternetAddress bcc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setBcc(javax.mail.internet.InternetAddress[] bcc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setBcc(java.lang.String bcc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setBcc(java.lang.String[] bcc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void addBcc(javax.mail.internet.InternetAddress bcc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void addBcc(java.lang.String bcc) throws javax.mail.MessagingException
javax.mail.MessagingException
public void addBcc(java.lang.String bcc, java.lang.String personal) throws javax.mail.MessagingException, java.io.UnsupportedEncodingException
javax.mail.MessagingException
java.io.UnsupportedEncodingException
public void setSentDate(java.util.Date sentDate) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setSubject(java.lang.String subject) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setText(java.lang.String text) throws javax.mail.MessagingException
javax.mail.MessagingException
public void setText(java.lang.String text, boolean html) throws javax.mail.MessagingException
text
- text to sethtml
- whether to apply content type "text/html" for an
HTML mail, using default content type ("text/plain") else
javax.mail.MessagingException
public void addInline(java.lang.String contentId, javax.activation.DataSource dataSource) throws javax.mail.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.
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 type
javax.mail.MessagingException
- in case of errorsaddAttachment(String, File)
,
addAttachment(String, org.springframework.core.io.InputStreamSource)
public void addInline(java.lang.String contentId, java.io.File file) throws javax.mail.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)!
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 from
javax.mail.MessagingException
addAttachment(String, org.springframework.core.io.InputStreamSource)
,
addAttachment(String, javax.activation.DataSource)
public void addInline(java.lang.String contentId, Resource resource) throws javax.mail.MessagingException
org.springframework.core.io.InputStreamResource
.
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)!
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 from
javax.mail.MessagingException
addAttachment(String, File)
,
addAttachment(String, javax.activation.DataSource)
public void addInline(java.lang.String contentId, InputStreamSource inputStreamSource, java.lang.String contentType) throws javax.mail.MessagingException
org.springframework.core.InputStreamResource
.
Note that you can determine the content type for any given filename
via the Activation Framework's FileTypeMap utility:
FileTypeMap.getDefaultFileTypeMap().getContentType(myFilename)
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 element
javax.mail.MessagingException
addAttachment(String, File)
,
addAttachment(String, javax.activation.DataSource)
,
FileTypeMap.getDefaultFileTypeMap()
,
FileTypeMap.getContentType(java.io.File)
public void addAttachment(java.lang.String attachmentFilename, javax.activation.DataSource dataSource) throws javax.mail.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 type
javax.mail.MessagingException
- in case of errorsaddAttachment(String, org.springframework.core.io.InputStreamSource)
,
addAttachment(String, java.io.File)
public void addAttachment(java.lang.String attachmentFilename, java.io.File file) throws javax.mail.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 from
javax.mail.MessagingException
addAttachment(String, org.springframework.core.io.InputStreamSource)
,
addAttachment(String, javax.activation.DataSource)
public void addAttachment(java.lang.String attachmentFilename, InputStreamSource inputStreamSource) throws javax.mail.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.
attachmentFilename
- the name of the attachment as it will
appear in the mailinputStreamSource
- the resource to take the content from
javax.mail.MessagingException
addAttachment(String, java.io.File)
,
addAttachment(String, javax.activation.DataSource)
protected javax.activation.DataSource createDataSource(InputStreamSource inputStreamSource, java.lang.String contentType, java.lang.String name)
inputStreamSource
- the InputStreamSource (typically a Spring Resource)contentType
- the content typename
- the name of the DataSource
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |