org.springframework.mail.javamail
Class MimeMessageHelper


public class 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. Also offers support for typical mail attachments. Advanced settings can still be applied directly to underlying MimeMessage!

Typically used in MimeMessagePreparator implementations or JavaMailSender client code: simply instantiating it as a facade to a MimeMessage, invoking setters on the facade, using the underlying MimeMessage for mail sending. Also used internally by JavaMailSenderImpl.

Sample code:

mailSender.send(new MimeMessagePreparator() {
  public void prepare(MimeMessage mimeMessage) throws MessagingException {
    MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
    message.setFrom("me@mail.com");
    message.setTo("you@mail.com");
    message.setSubject("my subject");
    message.setText("my text");
    message.addAttachment("logo.gif", new ClassPathResource("images/mylogo.gif"));
  }
});

Author:
Juergen Hoeller
Since: 19.01.2004
See Also: javax.mail.internet.MimeMessage, getMimeMessage(), MimeMessagePreparator, JavaMailSender, JavaMailSenderImpl, SimpleMailMessage

Constructor Summary
MimeMessageHelper(MimeMessage mimeMessage)
          Create new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content).
MimeMessageHelper(MimeMessage mimeMessage, boolean multipart)
          Create new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting attachments) if requested.

Method Summary
 voidaddAttachment(String attachmentFilename, File file)
          Add an attachment to the given MimeMessage, taking the content from a java.io.File.
 voidaddAttachment(String attachmentFilename, InputStreamSource inputStreamSource)
          Add an attachment to the given MimeMessage, taking the content from an org.springframework.core.InputStreamResource.
 voidaddAttachment(String attachmentFilename, DataSource dataSource)
          Add an attachment to the given MimeMessage, taking the content from a javax.activation.DataSource.
 MimeMessagegetMimeMessage()
          Return the underlying MimeMessage.
 voidsetBcc(String bcc)
           
 voidsetBcc(String[] bcc)
           
 voidsetCc(String cc)
           
 voidsetCc(String[] cc)
           
 voidsetFrom(String from)
           
 voidsetSubject(String subject)
           
 voidsetText(String text)
           
 voidsetText(String text, boolean html)
          Sets the given text directly as content in non-multipart mode respectively as default body part in multipart mode.
 voidsetTo(String to)
           
 voidsetTo(String[] to)
           

Constructor Detail

MimeMessageHelper

public MimeMessageHelper(MimeMessage mimeMessage)
Create new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content).
Parameters:
mimeMessage - MimeMessage to work on
See Also:
#MimeMessageHelper(javax.mail.internet.MimeMessage, boolean)

MimeMessageHelper

public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart)
throws MessagingException
Create new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting attachments) if requested.
Parameters:
mimeMessage - MimeMessage to work on
multipart - whether to create a multipart message that supports attachments
Method Detail

addAttachment

public void addAttachment(String attachmentFilename, File file)
throws MessagingException
Add an attachment to the given MimeMessage, taking the content from a 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)!

Parameters:
attachmentFilename - the name of the attachment as it will appear in the mail
file - the File resource to take the content from
Throws:
MessagingException
See Also:
addAttachment(java.lang.String,org.springframework.core.io.InputStreamSource), #addAttachment(String, javax.activation.DataSource)

addAttachment

public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource)
throws MessagingException
Add an attachment to the given MimeMessage, taking the content from an org.springframework.core.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.

Parameters:
attachmentFilename - the name of the attachment as it will appear in the mail
inputStreamSource - the resource to take the content from
See Also:
addAttachment(java.lang.String,java.io.File), #addAttachment(String, javax.activation.DataSource)

addAttachment

public void addAttachment(String attachmentFilename, DataSource dataSource)
throws MessagingException
Add an attachment to the given MimeMessage, taking the content from a 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.

Parameters:
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
Throws:
MessagingException - in case of errors
See Also:
addAttachment(java.lang.String,java.io.File), addAttachment(java.lang.String,org.springframework.core.io.InputStreamSource)

getMimeMessage

public MimeMessage getMimeMessage()
Return the underlying MimeMessage.

setBcc

public void setBcc(String bcc)
throws MessagingException

setBcc

public void setBcc(String[] bcc)
throws MessagingException

setCc

public void setCc(String cc)
throws MessagingException

setCc

public void setCc(String[] cc)
throws MessagingException

setFrom

public void setFrom(String from)
throws MessagingException

setSubject

public void setSubject(String subject)
throws MessagingException

setText

public void setText(String text)
throws MessagingException

setText

public void setText(String text, boolean html)
throws MessagingException
Sets the given text directly as content in non-multipart mode respectively as default body part in multipart mode.
Parameters:
text - text to set
html - whether to apply content type "text/html" for an HTML mail, using default content type ("text/plain") else

setTo

public void setTo(String to)
throws MessagingException

setTo

public void setTo(String[] to)
throws MessagingException