atg.service.email
Class MimeMessageUtils

java.lang.Object
  extended by atg.service.email.MimeMessageUtils

public class MimeMessageUtils
extends java.lang.Object

A set of static utility methods for creating and filling in javax.mail.Message objects. In particular, the createMessage methods of this class create javax.mail.internet.MimeMessage objects; other methods can be used to set the various fields of the message, specify the message content, and so on.

Note that the setContent methods in this class typically call through to javax.mail.Part.setContent(Object obj, String type) to set the content of the message or each of its parts. The type of the content can be any MIME type; however, for the message to be sent successfully, a javax.activation.DataContentHandler class for the specified type should be available to the JavaMail implementation. For example, to do setContent(foobar, "application/x-foobar"), a DataContentHandler for "application/x-foobar" should be installed. The Object passed in to the setContent method should be of type expected by the DataContentHandler associated with the content type.

If you want to create and send messages with a content type unknown to the system, you will first need to write a DataContentHandler implementation specific to your content type. Once you have your FoobarDataContentHandler, you will need to make it available to the Java Activation Framework. One way to do that is by providing a mailcap file with the new content handler entry in it (see JAF documentation, particularly javax.activation.MailcapCommandMap class documentation, for more information). Another option is to provide the same information to the DataContentHandlerRegistry component, by setting its dataContentHandlerMap property.

Following is the list of MIME types currently supported by the system, assuming a DataContentHandlerRegistry component with the standard configuration is used. For each content type, we also indicate the type of Object that must be passed in to setContent for the associated DataContentHandler to work correctly:

See Also:
Message, MimeMessage, DataContentHandler, MailcapCommandMap, DataContentHandlerRegistry

Field Summary
static java.lang.String CLASS_VERSION
          Class version string
 
Constructor Summary
MimeMessageUtils()
           
 
Method Summary
static void addRecipient(javax.mail.Message pMessage, javax.mail.Message.RecipientType pRecipientType, java.lang.String pRecipient)
          Adds a recipient to the message.
static void addRecipients(javax.mail.Message pMessage, javax.mail.Message.RecipientType pRecipientType, java.lang.String[] pRecipients)
          Adds recipients to the message.
static javax.mail.Message createMessage()
          Creates a new, empty Message object.
static javax.mail.Message createMessage(javax.mail.Session pSession)
          Creates a new, empty Message object.
static javax.mail.Message createMessage(java.lang.String pFrom, java.lang.String pSubject)
          Creates a new Message object with the given From and Subject fields.
static javax.mail.Message createMessage(java.lang.String pFrom, java.lang.String pSubject, javax.mail.Session pSession)
          Creates a new Message object with the given From and Subject fields.
static javax.mail.Message createMessage(java.lang.String pFrom, java.lang.String pSubject, java.lang.String pReplyTo)
          Creates a new Message object with the given From ReplyTo and Subject fields.
static javax.mail.Message createMessage(java.lang.String pFrom, java.lang.String pSubject, java.lang.String pTo, java.lang.Object pContent, java.lang.String pContentType)
          Creates a new Message object with the given From, Subject, and To fields, and the given content.
static javax.mail.Message createMessage(java.lang.String pFrom, java.lang.String pSubject, java.lang.String pTo, java.lang.Object pContent, java.lang.String pContentType, javax.mail.Session pSession)
          Creates a new Message object with the given From, Subject, and To fields, and the given content.
static javax.mail.Message createMessage(java.lang.String pFrom, java.lang.String pSubject, java.lang.String pReplyTo, javax.mail.Session pSession)
          Creates a new Message object with the given From ReplyTo and Subject fields.
static javax.mail.Message createMessage(java.lang.String pFrom, java.lang.String pSubject, java.lang.String pTo, java.lang.String pTextContent)
          Creates a new Message object with the given From, Subject, and To fields, and a body given by pTextContent.
static javax.mail.Message createMessage(java.lang.String pFrom, java.lang.String pSubject, java.lang.String pTo, java.lang.String pTextContent, javax.mail.Session pSession)
          Creates a new Message object with the given From, Subject, and To fields, and a body given by pTextContent.
static javax.mail.Session createSession()
          Creates a new mail session.
static javax.mail.Session getDefaultSession()
          Gets the default mail session using without using and Authenticator.
static void setBcc(javax.mail.Message pMessage, java.lang.String pBcc)
          Adds the Bcc field of the message to the InternetAddress obtained from the given string.
static void setCc(javax.mail.Message pMessage, java.lang.String pCc)
          Adds the Cc field of the message to the InternetAddress obtained from the given string.
static void setContent(javax.mail.Message pMessage, ContentPart[] pContentParts)
          Sets the message content of a message with multiple content parts.
static void setContent(javax.mail.Message pMessage, ContentPart[] pContentParts, boolean pInlineAttachments, javax.activation.DataHandler[] pAttachments, java.lang.String pMultipartSubtype)
          Sets the message content of a message with multiple content parts and multiple file attachments.
static void setContent(javax.mail.Message pMessage, ContentPart[] pContentParts, java.io.File[] pAttachments, boolean pInlineAttachments)
          Sets the message content of a message with multiple content parts and multiple file attachments.
static void setContent(javax.mail.Message pMessage, ContentPart[] pContentParts, java.io.File[] pAttachments, boolean pInlineAttachments, java.lang.String pMultipartSubtype)
          Sets the message content of a message with multiple content parts and multiple file attachments.
static void setContent(javax.mail.Message pMessage, ContentPart[] pContentParts, java.io.File pAttachment, boolean pInlineAttachment)
          Sets the message content of a message with multiple content parts and a file attachment.
static void setContent(javax.mail.Message pMessage, ContentPart[] pContentParts, java.io.File pAttachment, boolean pInlineAttachment, java.lang.String pMultipartSubtype)
          Sets the message content of a message with multiple content parts and a file attachment.
static void setContent(javax.mail.Message pMessage, ContentPart[] pContentParts, java.lang.String pMultipartSubtype)
          Sets the message content of a message with multiple content parts.
static void setContent(javax.mail.Message pMessage, java.lang.Object pContent, java.lang.String pContentType, java.io.File[] pAttachments, boolean pInlineAttachments)
          Sets the message content of a message with multiple file attachments.
static void setContent(javax.mail.Message pMessage, java.lang.Object pContent, java.lang.String pContentType, java.io.File pAttachment, boolean pInlineAttachment)
          Sets the message content of a message with a file attachment.
static void setFrom(javax.mail.Message pMessage, java.lang.String pFrom)
          Sets the From field of the message to the InternetAddress obtained from the given string.
static void setRecipient(javax.mail.Message pMessage, javax.mail.Message.RecipientType pRecipientType, java.lang.String pRecipient)
          Sets the recipient of the message (either To, Cc, or Bcc field) to the InternetAddress obtained from the given string.
static void setRecipients(javax.mail.Message pMessage, javax.mail.Message.RecipientType pRecipientType, java.lang.String pRecipient)
          Describe setRecipients method here.
static void setRecipients(javax.mail.Message pMessage, javax.mail.Message.RecipientType pRecipientType, java.lang.String[] pRecipients)
          Sets the recipients of the message (either To, Cc, or Bcc field) to the InternetAddress array obtained from the given string array.
static void setReplyTo(javax.mail.Message pMessage, java.lang.String pReplyTo)
          Sets the ReplyTo field of the message to the InternetAddress obtained from the given string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_VERSION

public static java.lang.String CLASS_VERSION
Class version string

Constructor Detail

MimeMessageUtils

public MimeMessageUtils()
Method Detail

createSession

public static javax.mail.Session createSession()
Creates a new mail session.

See Also:
Session.getInstance(java.util.Properties,javax.mail.Authenticator);

getDefaultSession

public static javax.mail.Session getDefaultSession()
Gets the default mail session using without using and Authenticator.

See Also:
Session.getDefaultInstance(java.util.Properties,javax.mail.Authenticator);, Authenticator

createMessage

public static javax.mail.Message createMessage()
Creates a new, empty Message object.


createMessage

public static javax.mail.Message createMessage(javax.mail.Session pSession)
Creates a new, empty Message object.


createMessage

public static javax.mail.Message createMessage(java.lang.String pFrom,
                                               java.lang.String pSubject)
                                        throws javax.mail.MessagingException
Creates a new Message object with the given From and Subject fields.

Throws:
javax.mail.MessagingException - if any of the fields could not be set

createMessage

public static javax.mail.Message createMessage(java.lang.String pFrom,
                                               java.lang.String pSubject,
                                               javax.mail.Session pSession)
                                        throws javax.mail.MessagingException
Creates a new Message object with the given From and Subject fields.

Throws:
javax.mail.MessagingException - if any of the fields could not be set

createMessage

public static javax.mail.Message createMessage(java.lang.String pFrom,
                                               java.lang.String pSubject,
                                               java.lang.String pReplyTo)
                                        throws javax.mail.MessagingException
Creates a new Message object with the given From ReplyTo and Subject fields.

Throws:
javax.mail.MessagingException - if any of the fields could not be set

createMessage

public static javax.mail.Message createMessage(java.lang.String pFrom,
                                               java.lang.String pSubject,
                                               java.lang.String pReplyTo,
                                               javax.mail.Session pSession)
                                        throws javax.mail.MessagingException
Creates a new Message object with the given From ReplyTo and Subject fields.

Throws:
javax.mail.MessagingException - if any of the fields could not be set

createMessage

public static javax.mail.Message createMessage(java.lang.String pFrom,
                                               java.lang.String pSubject,
                                               java.lang.String pTo,
                                               java.lang.String pTextContent)
                                        throws javax.mail.MessagingException
Creates a new Message object with the given From, Subject, and To fields, and a body given by pTextContent. This is a convenience method that calls MimeMessage.setText(pTextContent) to set the message content; the resulting MIME type of the content is "text/plain."

Throws:
javax.mail.MessagingException - if any of the fields or the message content could not be set
See Also:
MimeMessage.setText(String)

createMessage

public static javax.mail.Message createMessage(java.lang.String pFrom,
                                               java.lang.String pSubject,
                                               java.lang.String pTo,
                                               java.lang.String pTextContent,
                                               javax.mail.Session pSession)
                                        throws javax.mail.MessagingException
Creates a new Message object with the given From, Subject, and To fields, and a body given by pTextContent. This is a convenience method that calls MimeMessage.setText(pTextContent) to set the message content; the resulting MIME type of the content is "text/plain."

Throws:
javax.mail.MessagingException - if any of the fields or the message content could not be set
See Also:
MimeMessage.setText(String)

createMessage

public static javax.mail.Message createMessage(java.lang.String pFrom,
                                               java.lang.String pSubject,
                                               java.lang.String pTo,
                                               java.lang.Object pContent,
                                               java.lang.String pContentType)
                                        throws javax.mail.MessagingException
Creates a new Message object with the given From, Subject, and To fields, and the given content. This method calls MimeMessage.setContent(pContent, pContentType) to set the message content.

Throws:
javax.mail.MessagingException - if any of the fields or the message content could not be set
See Also:
MimeMessage.setContent(Object, String)

createMessage

public static javax.mail.Message createMessage(java.lang.String pFrom,
                                               java.lang.String pSubject,
                                               java.lang.String pTo,
                                               java.lang.Object pContent,
                                               java.lang.String pContentType,
                                               javax.mail.Session pSession)
                                        throws javax.mail.MessagingException
Creates a new Message object with the given From, Subject, and To fields, and the given content. This method calls MimeMessage.setContent(pContent, pContentType) to set the message content.

Throws:
javax.mail.MessagingException - if any of the fields or the message content could not be set
See Also:
MimeMessage.setContent(Object, String)

setReplyTo

public static void setReplyTo(javax.mail.Message pMessage,
                              java.lang.String pReplyTo)
                       throws javax.mail.MessagingException
Sets the ReplyTo field of the message to the InternetAddress obtained from the given string.

Throws:
javax.mail.MessagingException - if the field could not be set

setFrom

public static void setFrom(javax.mail.Message pMessage,
                           java.lang.String pFrom)
                    throws javax.mail.MessagingException
Sets the From field of the message to the InternetAddress obtained from the given string.

Throws:
javax.mail.MessagingException - if the field could not be set

setCc

public static void setCc(javax.mail.Message pMessage,
                         java.lang.String pCc)
                  throws javax.mail.MessagingException
Adds the Cc field of the message to the InternetAddress obtained from the given string.

Throws:
javax.mail.MessagingException - if the field could not be set

setBcc

public static void setBcc(javax.mail.Message pMessage,
                          java.lang.String pBcc)
                   throws javax.mail.MessagingException
Adds the Bcc field of the message to the InternetAddress obtained from the given string.

Throws:
javax.mail.MessagingException - if the field could not be set

setRecipient

public static void setRecipient(javax.mail.Message pMessage,
                                javax.mail.Message.RecipientType pRecipientType,
                                java.lang.String pRecipient)
                         throws javax.mail.MessagingException
Sets the recipient of the message (either To, Cc, or Bcc field) to the InternetAddress obtained from the given string.

Parameters:
pRecipientType - the recipient type, given by one of the values enumerated in Message.RecipientType; e.g., specifying Message.RecipientType.TO for the recipient type will result in the To field being set
Throws:
javax.mail.MessagingException - if the field could not be set
See Also:
Message.RecipientType

setRecipients

public static void setRecipients(javax.mail.Message pMessage,
                                 javax.mail.Message.RecipientType pRecipientType,
                                 java.lang.String[] pRecipients)
                          throws javax.mail.MessagingException
Sets the recipients of the message (either To, Cc, or Bcc field) to the InternetAddress array obtained from the given string array.

Parameters:
pRecipientType - the recipient type, given by one of the values enumerated in Message.RecipientType; e.g., specifying Message.RecipientType.TO for the recipient type will result in the To field being set
Throws:
javax.mail.MessagingException - if the field could not be set
See Also:
Message.RecipientType

setRecipients

public static void setRecipients(javax.mail.Message pMessage,
                                 javax.mail.Message.RecipientType pRecipientType,
                                 java.lang.String pRecipient)
                          throws javax.mail.MessagingException
Describe setRecipients method here.

Parameters:
pMessage - a Message value
pRecipientType - a Message.RecipientType value
pRecipient - a String value
Throws:
javax.mail.MessagingException

addRecipient

public static void addRecipient(javax.mail.Message pMessage,
                                javax.mail.Message.RecipientType pRecipientType,
                                java.lang.String pRecipient)
                         throws javax.mail.MessagingException
Adds a recipient to the message.

Parameters:
pRecipientType - the recipient type, given by one of the values enumerated in Message.RecipientType; e.g., specifying Message.RecipientType.TO for the recipient type will result in the To field being updated
Throws:
javax.mail.MessagingException - if the field could not be set
See Also:
Message.RecipientType

addRecipients

public static void addRecipients(javax.mail.Message pMessage,
                                 javax.mail.Message.RecipientType pRecipientType,
                                 java.lang.String[] pRecipients)
                          throws javax.mail.MessagingException
Adds recipients to the message.

Parameters:
pRecipientType - the recipient type, given by one of the values enumerated in Message.RecipientType; e.g., specifying Message.RecipientType.TO for the recipient type will result in the To field being updated
Throws:
javax.mail.MessagingException - if the field could not be set
See Also:
Message.RecipientType

setContent

public static void setContent(javax.mail.Message pMessage,
                              ContentPart[] pContentParts)
                       throws javax.mail.MessagingException
Sets the message content of a message with multiple content parts. For example, a message could have two content parts, one of type "text/plain," another of type "text/html," and the message recipient could decide which of the two content parts to present to the user.

Each element in pContentParts represents a message part, with the content and its MIME type contained within the ContentPart object.

This method sets the message content to a MimeMultipart with a content type of "multipart/mixed;" for each contentPart, Part.setContent(contentPart.getContent(), contentPart.getContentType()) is called to set the part's content.

Throws:
javax.mail.MessagingException - if the message content could not be set
See Also:
Part.setContent(Object, String)

setContent

public static void setContent(javax.mail.Message pMessage,
                              ContentPart[] pContentParts,
                              java.lang.String pMultipartSubtype)
                       throws javax.mail.MessagingException
Sets the message content of a message with multiple content parts. For example, a message could have two content parts, one of type "text/plain," another of type "text/html," and the message recipient could decide which of the two content parts to present to the user.

Each element in pContentParts represents a message part, with the content and its MIME type contained within the ContentPart object.

This method sets the message content to a MimeMultipart with a content type of "multipart/pMultipartSubtype"; for each contentPart, Part.setContent(contentPart.getContent(), contentPart.getContentType()) is called to set the part's content.

Throws:
javax.mail.MessagingException - if the message content could not be set
See Also:
Part.setContent(Object, String)

setContent

public static void setContent(javax.mail.Message pMessage,
                              java.lang.Object pContent,
                              java.lang.String pContentType,
                              java.io.File pAttachment,
                              boolean pInlineAttachment)
                       throws javax.mail.MessagingException
Sets the message content of a message with a file attachment.

This method sets the message content to a MimeMultipart (with a content type of "multipart/mixed"); the first part is given by pContent, with the MIME type of pContentType, and the second part contains the file attachment.

Parameters:
pAttachment - the file to attach to the message
pInlineAttachment - if true, the file will be inlined into the message, rather than attached
Throws:
javax.mail.MessagingException - if the message content could not be set

setContent

public static void setContent(javax.mail.Message pMessage,
                              ContentPart[] pContentParts,
                              java.io.File pAttachment,
                              boolean pInlineAttachment)
                       throws javax.mail.MessagingException
Sets the message content of a message with multiple content parts and a file attachment. For example, a message could have two content parts, one of type "text/plain," another of type "text/html," and also a file attachment.

Each element in pContentParts represents a message part, with the content and its MIME type contained within the ContentPart object.

This method sets the message content to a MimeMultipart (with a content type of "multipart/mixed"). The first part of the multipart is itself a MimeMultipart, with a content type of "multipart/mixed;" for each contentPart, Part.setContent(contentPart.getContent(), contentPart.getContentType()) is called to set the part's content. The second part of the outer multipart contains the file attachment.

Parameters:
pAttachment - the file to attach to the message
pInlineAttachment - if true, the file will be inlined into the message, rather than attached
Throws:
javax.mail.MessagingException - if the message content could not be set
See Also:
Part.setContent(Object, String)

setContent

public static void setContent(javax.mail.Message pMessage,
                              ContentPart[] pContentParts,
                              java.io.File pAttachment,
                              boolean pInlineAttachment,
                              java.lang.String pMultipartSubtype)
                       throws javax.mail.MessagingException
Sets the message content of a message with multiple content parts and a file attachment. For example, a message could have two content parts, one of type "text/plain," another of type "text/html," and also a file attachment.

Each element in pContentParts represents a message part, with the content and its MIME type contained within the ContentPart object.

This method sets the message content to a MimeMultipart (with a content type of "multipart/mixed"). The first part of the multipart is itself a MimeMultipart, with a content type of "multipart/pMultipartSubtype;" for each contentPart, Part.setContent(contentPart.getContent(), contentPart.getContentType()) is called to set the part's content. The second part of the outer multipart contains the file attachment.

Parameters:
pAttachment - the file to attach to the message
pInlineAttachment - if true, the file will be inlined into the message, rather than attached
Throws:
javax.mail.MessagingException - if the message content could not be set
See Also:
Part.setContent(Object, String)

setContent

public static void setContent(javax.mail.Message pMessage,
                              java.lang.Object pContent,
                              java.lang.String pContentType,
                              java.io.File[] pAttachments,
                              boolean pInlineAttachments)
                       throws javax.mail.MessagingException
Sets the message content of a message with multiple file attachments.

This method sets the message content to a MimeMultipart (with a content type of "multipart/mixed"); the first part is given by pContent, with the MIME type of pContentType, and the rest of the parts contain the file attachments.

Parameters:
pAttachments - the files to attach to the message
pInlineAttachment - if true, each of the files will be inlined into the message, rather than attached
Throws:
javax.mail.MessagingException - if the message content could not be set

setContent

public static void setContent(javax.mail.Message pMessage,
                              ContentPart[] pContentParts,
                              java.io.File[] pAttachments,
                              boolean pInlineAttachments)
                       throws javax.mail.MessagingException
Sets the message content of a message with multiple content parts and multiple file attachments. For example, a message could have two content parts, one of type "text/plain," another of type "text/html," and also several file attachments.

Each element in pContentParts represents a message part, with the content and its MIME type contained within the ContentPart object.

This method sets the message content to a MimeMultipart (with a content type of "multipart/mixed"). The first part of the multipart is itself a MimeMultipart, with a content type of "multipart/mixed;" for each contentPart, Part.setContent(contentPart.getContent(), contentPart.getContentType()) is called to set the part's content. Additional parts of the outer multipart contain the file attachments.

Parameters:
pAttachments - the files to attach to the message
pInlineAttachments - if true, each of the files will be inlined into the message, rather than attached
Throws:
javax.mail.MessagingException - if the message content could not be set
See Also:
Part.setContent(Object, String)

setContent

public static void setContent(javax.mail.Message pMessage,
                              ContentPart[] pContentParts,
                              java.io.File[] pAttachments,
                              boolean pInlineAttachments,
                              java.lang.String pMultipartSubtype)
                       throws javax.mail.MessagingException
Sets the message content of a message with multiple content parts and multiple file attachments. For example, a message could have two content parts, one of type "text/plain," another of type "text/html," and also several file attachments.

Each element in pContentParts represents a message part, with the content and its MIME type contained within the ContentPart object.

This method sets the message content to a MimeMultipart (with a content type of "multipart/mixed"). The first part of the multipart is itself a MimeMultipart, with a content type of "multipart/pMultipartSubtype;" for each contentPart, Part.setContent(contentPart.getContent(), contentPart.getContentType()) is called to set the part's content. Additional parts of the outer multipart contain the file attachments.

Parameters:
pAttachments - the files to attach to the message
pInlineAttachments - if true, each of the files will be inlined into the message, rather than attached
Throws:
javax.mail.MessagingException - if the message content could not be set
See Also:
Part.setContent(Object, String)

setContent

public static void setContent(javax.mail.Message pMessage,
                              ContentPart[] pContentParts,
                              boolean pInlineAttachments,
                              javax.activation.DataHandler[] pAttachments,
                              java.lang.String pMultipartSubtype)
                       throws javax.mail.MessagingException
Sets the message content of a message with multiple content parts and multiple file attachments. For example, a message could have two content parts, one of type "text/plain," another of type "text/html," and also several file attachments.

Each element in pContentParts represents a message part, with the content and its MIME type contained within the ContentPart object.

This method sets the message content to a MimeMultipart (with a content type of "multipart/mixed"). The first part of the multipart is itself a MimeMultipart, with a content type of "multipart/pMultipartSubtype;" for each contentPart, Part.setContent(contentPart.getContent(), contentPart.getContentType()) is called to set the part's content. Additional parts of the outer multipart contain the file attachments.

Parameters:
pInlineAttachments - if true, each of the files will be inlined into the message, rather than attached
pAttachments - the DataHandlers representing the files to attach to the message
Throws:
javax.mail.MessagingException - if the message content could not be set
See Also:
Part.setContent(Object, String)