org.springframework.mail.javamail
Class MimeMessageHelper

java.lang.Object
  extended byorg.springframework.mail.javamail.MimeMessageHelper

public class MimeMessageHelper
extends java.lang.Object

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:

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

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

Constructor Summary
MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage)
          Create new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content).
MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage, boolean multipart)
          Create 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 new MimeMessageHelper for the given MimeMessage, in multipart mode (supporting attachments) if requested.
MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage, java.lang.String encoding)
          Create 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 given MimeMessage, taking the content from a javax.activation.DataSource.
 void addAttachment(java.lang.String attachmentFilename, java.io.File file)
          Add an attachment to the given MimeMessage, taking the content from a java.io.File.
 void addAttachment(java.lang.String attachmentFilename, InputStreamSource inputStreamSource)
          Add an attachment to the given MimeMessage, taking the content from an org.springframework.core.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 addTo(javax.mail.internet.InternetAddress to)
           
 void addTo(java.lang.String to)
           
 void addTo(java.lang.String to, java.lang.String personal)
           
 java.lang.String getEncoding()
          Return the character encoding used for this message.
 javax.mail.internet.MimeMessage getMimeMessage()
          Return the underlying MimeMessage.
 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 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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MimeMessageHelper

public MimeMessageHelper(javax.mail.internet.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(javax.mail.internet.MimeMessage mimeMessage,
                         boolean multipart)
                  throws javax.mail.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

MimeMessageHelper

public MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage,
                         java.lang.String encoding)
Create new MimeMessageHelper for the given MimeMessage, assuming a simple text message (no multipart content).

Parameters:
mimeMessage - MimeMessage to work on
encoding - the character encoding to use for the message
See Also:
MimeMessageHelper(javax.mail.internet.MimeMessage, boolean)

MimeMessageHelper

public MimeMessageHelper(javax.mail.internet.MimeMessage mimeMessage,
                         boolean multipart,
                         java.lang.String encoding)
                  throws javax.mail.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
encoding - the character encoding to use for the message
Method Detail

getMimeMessage

public javax.mail.internet.MimeMessage getMimeMessage()
Return the underlying MimeMessage.


getEncoding

public java.lang.String getEncoding()
Return the character encoding used for this message.


setFrom

public void setFrom(javax.mail.internet.InternetAddress from)
             throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setFrom

public void setFrom(java.lang.String from)
             throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setFrom

public void setFrom(java.lang.String from,
                    java.lang.String personal)
             throws javax.mail.MessagingException,
                    java.io.UnsupportedEncodingException
Throws:
javax.mail.MessagingException
java.io.UnsupportedEncodingException

setTo

public void setTo(javax.mail.internet.InternetAddress to)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setTo

public void setTo(javax.mail.internet.InternetAddress[] to)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setTo

public void setTo(java.lang.String to)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setTo

public void setTo(java.lang.String[] to)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

addTo

public void addTo(javax.mail.internet.InternetAddress to)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

addTo

public void addTo(java.lang.String to)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

addTo

public void addTo(java.lang.String to,
                  java.lang.String personal)
           throws javax.mail.MessagingException,
                  java.io.UnsupportedEncodingException
Throws:
javax.mail.MessagingException
java.io.UnsupportedEncodingException

setCc

public void setCc(javax.mail.internet.InternetAddress cc)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setCc

public void setCc(javax.mail.internet.InternetAddress[] cc)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setCc

public void setCc(java.lang.String cc)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setCc

public void setCc(java.lang.String[] cc)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

addCc

public void addCc(javax.mail.internet.InternetAddress cc)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

addCc

public void addCc(java.lang.String cc)
           throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

addCc

public void addCc(java.lang.String cc,
                  java.lang.String personal)
           throws javax.mail.MessagingException,
                  java.io.UnsupportedEncodingException
Throws:
javax.mail.MessagingException
java.io.UnsupportedEncodingException

setBcc

public void setBcc(javax.mail.internet.InternetAddress bcc)
            throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setBcc

public void setBcc(javax.mail.internet.InternetAddress[] bcc)
            throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setBcc

public void setBcc(java.lang.String bcc)
            throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setBcc

public void setBcc(java.lang.String[] bcc)
            throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

addBcc

public void addBcc(javax.mail.internet.InternetAddress bcc)
            throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

addBcc

public void addBcc(java.lang.String bcc)
            throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

addBcc

public void addBcc(java.lang.String bcc,
                   java.lang.String personal)
            throws javax.mail.MessagingException,
                   java.io.UnsupportedEncodingException
Throws:
javax.mail.MessagingException
java.io.UnsupportedEncodingException

setSubject

public void setSubject(java.lang.String subject)
                throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setText

public void setText(java.lang.String text)
             throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

setText

public void setText(java.lang.String text,
                    boolean html)
             throws javax.mail.MessagingException
Set 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
Throws:
javax.mail.MessagingException

addAttachment

public void addAttachment(java.lang.String attachmentFilename,
                          java.io.File file)
                   throws javax.mail.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:
javax.mail.MessagingException
See Also:
addAttachment(String, org.springframework.core.io.InputStreamSource), addAttachment(String, javax.activation.DataSource)

addAttachment

public void addAttachment(java.lang.String attachmentFilename,
                          InputStreamSource inputStreamSource)
                   throws javax.mail.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
Throws:
javax.mail.MessagingException
See Also:
addAttachment(String, File), addAttachment(String, javax.activation.DataSource)

addAttachment

public void addAttachment(java.lang.String attachmentFilename,
                          javax.activation.DataSource dataSource)
                   throws javax.mail.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:
javax.mail.MessagingException - in case of errors
See Also:
addAttachment(String, File), addAttachment(String, org.springframework.core.io.InputStreamSource)


Copyright (C) 2003-2004 The Spring Framework Project.