com.github.jknack.mwa.mail
Class MailBuilder

java.lang.Object
  extended by com.github.jknack.mwa.mail.MailBuilder

public class MailBuilder
extends Object

Helper class for sending rich mail messages.

Since:
0.1.3
Author:
edgar.espina

Method Summary
 MailBuilder attach(File file)
          Add an attachment to the MimeMessage, taking the content from a java.io.File.
 MailBuilder attach(String name, File file)
          Add an attachment to the MimeMessage, taking the content from a java.io.File.
 MailBuilder attach(String name, InputStream input, String contentType)
          Add an attachment to the MimeMessage.
 MailBuilder attach(String name, org.springframework.core.io.Resource resource, String contentType)
          Add an attachment to the MimeMessage.
 MailBuilder bcc(Iterable<String> bcc)
          Set 'bcc' with the provided email addresses.
 MailBuilder bcc(String... bcc)
          Set 'bcc' with the provided email addresses.
 javax.mail.internet.MimeMessage build()
          Return a MimeMessage ready to be send.
 MailBuilder cc(Iterable<String> cc)
          Set cc with the provided email addresses.
 MailBuilder cc(String... cc)
          Set cc with the provided email addresses.
 MailBuilder from(String from)
          Set 'from' with the provided email address.
 MailBuilder from(String from, String name)
          Set 'from' with the provided email address.
 MailBuilder html(String html, Object... args)
          Set the given text directly as content in non-multipart mode or as default body part in multipart mode.
static InputStream mailInputStream(InputStream input, String contentType)
          Add content-type information to an InputStream.
static MailBuilder newMail(org.springframework.mail.javamail.JavaMailSender sender)
          Creates a new mail message with multi-part support.
static MailBuilder newSimpleMail(org.springframework.mail.javamail.JavaMailSender sender)
          Creates a new simple mail message without multi-part support.
 MailBuilder replyTo(String replayTo)
          Set 'replayTo' with the provided email address.
 MailBuilder replyTo(String replayTo, String name)
          Set 'replayTo' with the provided email address.
 MailBuilder subject(String subject)
          Set 'subject' of the message.
 MailBuilder text(String text, Object... args)
          Set the given text directly as content.
 MailBuilder to(Iterable<String> to)
          Set 'to' with the provided email addresses.
 MailBuilder to(String... to)
          Set 'to' with the provided email addresses.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

text

public MailBuilder text(String text,
                        Object... args)
                 throws javax.mail.MessagingException
Set the given text directly as content. Always applies the default content type "text/plain". Optionally, you can send a message template and pass some argument for merging and create the final message.

Usage:

 text("Hello {0}!", "World");
 

Parameters:
text - The text for the message.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException - in case of errors.
See Also:
MessageFormat.format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition)

to

public MailBuilder to(String... to)
               throws javax.mail.MessagingException
Set 'to' with the provided email addresses.

Parameters:
to - The email addresses.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException

to

public MailBuilder to(Iterable<String> to)
               throws javax.mail.MessagingException
Set 'to' with the provided email addresses.

Parameters:
to - The email addresses.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException

bcc

public MailBuilder bcc(String... bcc)
                throws javax.mail.MessagingException
Set 'bcc' with the provided email addresses.

Parameters:
bcc - The email addresses.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException

bcc

public MailBuilder bcc(Iterable<String> bcc)
                throws javax.mail.MessagingException
Set 'bcc' with the provided email addresses.

Parameters:
bcc - The email addresses.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException

cc

public MailBuilder cc(String... cc)
               throws javax.mail.MessagingException
Set cc with the provided email addresses.

Parameters:
cc - The email addresses.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException

cc

public MailBuilder cc(Iterable<String> cc)
               throws javax.mail.MessagingException
Set cc with the provided email addresses.

Parameters:
cc - The email addresses.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException

from

public MailBuilder from(String from)
                 throws javax.mail.MessagingException
Set 'from' with the provided email address.

Parameters:
from - The email address.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException

from

public MailBuilder from(String from,
                        String name)
                 throws javax.mail.MessagingException,
                        UnsupportedEncodingException
Set 'from' with the provided email address.

Parameters:
from - The email address.
name - The personal name.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException
UnsupportedEncodingException

replyTo

public MailBuilder replyTo(String replayTo)
                    throws javax.mail.MessagingException
Set 'replayTo' with the provided email address.

Parameters:
replayTo - The email address.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException

replyTo

public MailBuilder replyTo(String replayTo,
                           String name)
                    throws javax.mail.MessagingException,
                           UnsupportedEncodingException
Set 'replayTo' with the provided email address.

Parameters:
replayTo - The email address.
name - The personal name.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException
UnsupportedEncodingException

subject

public MailBuilder subject(String subject)
                    throws javax.mail.MessagingException
Set 'subject' of the message.

Parameters:
subject - The email address.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException

html

public MailBuilder html(String html,
                        Object... args)
                 throws javax.mail.MessagingException,
                        IOException
Set the given text directly as content in non-multipart mode or as default body part in multipart mode. The "html" flag determines the content type to apply.

Usage:

  html("<img src=\"{0}\">"<p>Hello {1}!</p>,
    new File("header.jpg"),
    "World");
 
Or:
  html("<img src=\"{0}\">"<p>Hello {1}!</p>,
    MailBuilder.mailInputStream(source, "image/jpg"),
    "World");
 

Parameters:
html - The html code as string.
args - The embedded elements. Optional.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException - If something goes wrong.
IOException - If the embedded/inline resource cannot be read it.

attach

public MailBuilder attach(String name,
                          File file)
                   throws javax.mail.MessagingException
Add an attachment to the 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:
name - The attachment name.
file - The resource to add.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException - If something goes wrong.

attach

public MailBuilder attach(File file)
                   throws javax.mail.MessagingException
Add an attachment to the 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:
file - The resource to add.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException - If something goes wrong.

attach

public MailBuilder attach(String name,
                          InputStream input,
                          String contentType)
                   throws javax.mail.MessagingException,
                          IOException
Add an attachment to the MimeMessage.

Parameters:
name - The attachment name.
input - The resource to add.
contentType - The resource content type.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException - If something goes wrong.
IOException - If something goes wrong.

attach

public MailBuilder attach(String name,
                          org.springframework.core.io.Resource resource,
                          String contentType)
                   throws javax.mail.MessagingException
Add an attachment to the MimeMessage.

Parameters:
name - The attachment name.
resource - The resource to add.
contentType - The resource content type.
Returns:
This MailBuilder.
Throws:
javax.mail.MessagingException - If something goes wrong.

build

public javax.mail.internet.MimeMessage build()
Return a MimeMessage ready to be send.

Returns:
A MimeMessage fully configured.

newMail

public static MailBuilder newMail(org.springframework.mail.javamail.JavaMailSender sender)
                           throws javax.mail.MessagingException
Creates a new mail message with multi-part support.

Parameters:
sender - The email sender. Required.
Returns:
A new mail message with multi-part support.
Throws:
javax.mail.MessagingException - If the message cannot be created.

newSimpleMail

public static MailBuilder newSimpleMail(org.springframework.mail.javamail.JavaMailSender sender)
                                 throws javax.mail.MessagingException
Creates a new simple mail message without multi-part support.

Parameters:
sender - The email sender. Required.
Returns:
A new mail message without multi-part support.
Throws:
javax.mail.MessagingException - If the message cannot be created.

mailInputStream

public static InputStream mailInputStream(InputStream input,
                                          String contentType)
Add content-type information to an InputStream.

Parameters:
input - The candidate input stream. Required.
contentType - The content-type. Required.
Returns:
An input-stream with content-type information.


Copyright © 2012. All Rights Reserved.