org.mentawai.mail
Class Email

java.lang.Object
  extended by org.mentawai.mail.Email
Direct Known Subclasses:
MultiPartEmail, SimpleEmail

public abstract class Email
extends Object

This code belongs to the Apache Commons Email project. Some modifications to support SLL, so that you can use this class with GMAIL's SMTP, was done by Erko Bridee de Almeida Cabrera. ==== BEGIN ORIGINAL JAVADOC ===== The base class for all email messages. This class sets the sender's email & name, receiver's email & name, subject, and the sent date. Subclasses are responsible for setting the message body.

Author:
Quinton McCombs, Jon S. Stevens, Frank Y. Kim, Brett McLaughlin, Greg Ritter, Regis Koenig, Colin Chalmers, Matthias Wessendorf, Corey Scott

Field Summary
static String ATTACHMENTS
           
protected  javax.mail.Authenticator authenticator
          Instance of an Authenticator object that will be used when authentication is requested from the mail server.
protected  List<javax.mail.internet.InternetAddress> bccList
          List of "bcc" email adresses
protected  String bounceAddress
          Address to which undeliverable mail should be sent.
protected  List<javax.mail.internet.InternetAddress> ccList
          List of "cc" email adresses
protected  String charset
          The charset to use for this message
protected  Object content
          The content
static String CONTENT_TYPE
           
protected  String contentType
          The content type
protected  boolean debug
          Set session debugging on or off
static String EMAIL_BODY
           
static String EMAIL_SUBJECT
           
protected  javax.mail.internet.MimeMultipart emailBody
          An attachment
static String FILE_SERVER
           
protected  javax.mail.internet.InternetAddress fromAddress
          The Address of the sending party, mandatory
protected  Map<String,String> headers
          Used to specify the mail headers.
protected  String hostName
          The hostname of the mail server with which to connect.
static String ISO_8859_1
           
static String KOI8_R
           
static String MAIL_DEBUG
           
static String MAIL_FACTORY_CLASS
           
static String MAIL_FACTORY_FALLBACK
           
static String MAIL_FACTORY_PORT
           
static String MAIL_HOST
           
static String MAIL_PORT
           
static String MAIL_SMTP_AUTH
           
static String MAIL_SMTP_FROM
           
static String MAIL_TRANSPORT_PROTOCOL
           
protected  javax.mail.internet.MimeMessage message
          The email message to send.
protected  boolean popBeforeSmtp
          Used to determine whether to use pop3 before smtp, and if so the settings.
protected  String popHost
          the host name of the pop3 server
protected  String popPassword
          the password to log into the pop3 server
protected  String popUsername
          the user name to log into the pop3 server
static String RECEIVER_EMAIL
           
static String RECEIVER_NAME
           
protected  List<javax.mail.internet.InternetAddress> replyList
          List of "replyTo" email adresses
static String SENDER_EMAIL
           
static String SENDER_NAME
           
protected  Date sentDate
          Sent date
static String SMTP
           
protected  String smtpPort
          The port number of the mail server to connect to.
protected  boolean ssl_connection
           
protected  String subject
          The Subject
static String TEXT_HTML
           
static String TEXT_PLAIN
           
protected  List<javax.mail.internet.InternetAddress> toList
          List of "to" email adresses
static String US_ASCII
           
 
Constructor Summary
Email()
          Construtor da classe
 
Method Summary
 Email addBcc(String email)
          Add a blind BCC recipient to the email.
 Email addBcc(String email, String name)
          Add a blind BCC recipient to the email.
 Email addCc(String email)
          Add a recipient CC to the email.
 Email addCc(String email, String name)
          Add a recipient CC to the email.
 void addHeader(String name, String value)
          Adds a header ( name, value ) to the headers Map.
 Email addReplyTo(String email)
          Add a reply to address to the email.
 Email addReplyTo(String email, String name)
          Add a reply to address to the email.
 Email addTo(String email)
          Add a recipient TO to the email.
 Email addTo(String email, String name)
          Add a recipient TO to the email.
protected  javax.mail.Session getMailSession()
          Initialise a mailsession object
 Date getSentDate()
          Gets the sent date for the email.
 void removeBcc(String email)
           
 void removeCc(String email)
           
 void removeTo(String email)
           
 void send()
           
 void send(boolean recoverFromBadEmails)
          Does the work of actually sending the email.
 void sendLater()
           
 void sendLater(boolean recoverFromBadEmails)
           
 void sendLater(boolean printException, boolean recoverFromBadEmails)
           
 void setAuthentication(String userName, String password)
          Sets the userName and password if authentication is needed.
 void setAuthenticator(javax.mail.Authenticator newAuthenticator)
          Sets the Authenticator to be used when authentication is requested from the mail server.
 Email setBcc(Collection aCollection)
          Set a list of "BCC" addresses
 Email setBounceAddress(String email)
          Set the "bounce address" - the address to which undeliverable messages will be returned.
 Email setCc(Collection aCollection)
          Set a list of "CC" addresses.
 void setCharset(String newCharset)
          Set the charset of the message.
 void setContent(javax.mail.internet.MimeMultipart aMimeMultipart)
          Set the emailBody to a MimeMultiPart
 void setContent(Object aObject, String aContentType)
          Set the content & contentType
 void setDebug(boolean d)
          Setting to true will enable the display of debug information.
static void setDefaultAuthentication(String user, String pass)
           
static void setDefaultBounceAddress(String email)
           
static void setDefaultCharset(String charset)
           
static void setDefaultFrom(String email, String name)
           
static void setDefaultHostName(String host)
           
static void setDefaultPort(int port)
           
static void setDefaultPort(String port)
           
static void setDefaultSslConnection(boolean active)
           
static void setFactoryFallBack(String active)
          true or false
 Email setFrom(String email)
          Set the FROM field of the email.
 Email setFrom(String email, String name)
          Set the FROM field of the email.
 void setHeaders(Map<String,String> map)
          Used to specify the mail headers.
 void setHostName(String aHostName)
          Set the hostname of the outgoing mail server
 void setMailSession(javax.mail.Session aSession)
          Supply a mail Session object to use
abstract  Email setMsg(String msg)
          Define the content of the mail.
 void setPopBeforeSmtp(boolean newPopBeforeSmtp, String newPopHost, String newPopUsername, String newPopPassword)
          Set details regarding "pop3 before smtp" authentication.
static void setSendEmail(boolean sendEmail)
           
 void setSentDate(Date date)
          Sets the sent date for the email.
 void setSmtpPort(int aPortNumber)
          Set the port number of the outgoing mail server.
 void setSslConnection(boolean flag)
           
static void setSSLConnection(boolean active)
          Deprecated. use setDefaultSslConnection instead.
 Email setSubject(String aSubject)
          Set the email subject.
 Email setTo(Collection aCollection)
          Set a list of "TO" addresses.
protected  javax.mail.internet.InternetAddress[] toInternetAddressArray(List<javax.mail.internet.InternetAddress> list)
          Utility to copy List of known InternetAddress objects into an array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SENDER_EMAIL

public static final String SENDER_EMAIL
See Also:
Constant Field Values

SENDER_NAME

public static final String SENDER_NAME
See Also:
Constant Field Values

RECEIVER_EMAIL

public static final String RECEIVER_EMAIL
See Also:
Constant Field Values

RECEIVER_NAME

public static final String RECEIVER_NAME
See Also:
Constant Field Values

EMAIL_SUBJECT

public static final String EMAIL_SUBJECT
See Also:
Constant Field Values

EMAIL_BODY

public static final String EMAIL_BODY
See Also:
Constant Field Values

CONTENT_TYPE

public static final String CONTENT_TYPE
See Also:
Constant Field Values

MAIL_HOST

public static final String MAIL_HOST
See Also:
Constant Field Values

MAIL_PORT

public static final String MAIL_PORT
See Also:
Constant Field Values

MAIL_SMTP_FROM

public static final String MAIL_SMTP_FROM
See Also:
Constant Field Values

MAIL_SMTP_AUTH

public static final String MAIL_SMTP_AUTH
See Also:
Constant Field Values

MAIL_TRANSPORT_PROTOCOL

public static final String MAIL_TRANSPORT_PROTOCOL
See Also:
Constant Field Values

SMTP

public static final String SMTP
See Also:
Constant Field Values

TEXT_HTML

public static final String TEXT_HTML
See Also:
Constant Field Values

TEXT_PLAIN

public static final String TEXT_PLAIN
See Also:
Constant Field Values

ATTACHMENTS

public static final String ATTACHMENTS
See Also:
Constant Field Values

FILE_SERVER

public static final String FILE_SERVER
See Also:
Constant Field Values

MAIL_DEBUG

public static final String MAIL_DEBUG
See Also:
Constant Field Values

KOI8_R

public static final String KOI8_R
See Also:
Constant Field Values

ISO_8859_1

public static final String ISO_8859_1
See Also:
Constant Field Values

US_ASCII

public static final String US_ASCII
See Also:
Constant Field Values

message

protected javax.mail.internet.MimeMessage message
The email message to send.


charset

protected String charset
The charset to use for this message


fromAddress

protected javax.mail.internet.InternetAddress fromAddress
The Address of the sending party, mandatory


subject

protected String subject
The Subject


emailBody

protected javax.mail.internet.MimeMultipart emailBody
An attachment


content

protected Object content
The content


contentType

protected String contentType
The content type


debug

protected boolean debug
Set session debugging on or off


sentDate

protected Date sentDate
Sent date


authenticator

protected javax.mail.Authenticator authenticator
Instance of an Authenticator object that will be used when authentication is requested from the mail server.


hostName

protected String hostName
The hostname of the mail server with which to connect. If null will try to get property from system.properties. If still null, quit


smtpPort

protected String smtpPort
The port number of the mail server to connect to. Defaults to the standard port ( 25 ).


ssl_connection

protected boolean ssl_connection

toList

protected List<javax.mail.internet.InternetAddress> toList
List of "to" email adresses


ccList

protected List<javax.mail.internet.InternetAddress> ccList
List of "cc" email adresses


bccList

protected List<javax.mail.internet.InternetAddress> bccList
List of "bcc" email adresses


replyList

protected List<javax.mail.internet.InternetAddress> replyList
List of "replyTo" email adresses


bounceAddress

protected String bounceAddress
Address to which undeliverable mail should be sent. Because this is handled by JavaMail as a String property in the mail session, this property is of type String rather than InternetAddress.


headers

protected Map<String,String> headers
Used to specify the mail headers. Example: X-Mailer: Sendmail, X-Priority: 1( highest ) or 2( high ) 3( normal ) 4( low ) and 5( lowest ) Disposition-Notification-To: user@domain.net


popBeforeSmtp

protected boolean popBeforeSmtp
Used to determine whether to use pop3 before smtp, and if so the settings.


popHost

protected String popHost
the host name of the pop3 server


popUsername

protected String popUsername
the user name to log into the pop3 server


popPassword

protected String popPassword
the password to log into the pop3 server


MAIL_FACTORY_PORT

public static final String MAIL_FACTORY_PORT
See Also:
Constant Field Values

MAIL_FACTORY_CLASS

public static final String MAIL_FACTORY_CLASS
See Also:
Constant Field Values

MAIL_FACTORY_FALLBACK

public static final String MAIL_FACTORY_FALLBACK
See Also:
Constant Field Values
Constructor Detail

Email

public Email()
Construtor da classe

Method Detail

setSendEmail

public static void setSendEmail(boolean sendEmail)

setDefaultCharset

public static void setDefaultCharset(String charset)

setDefaultAuthentication

public static void setDefaultAuthentication(String user,
                                            String pass)

setDefaultHostName

public static void setDefaultHostName(String host)

setDefaultFrom

public static void setDefaultFrom(String email,
                                  String name)

setDefaultBounceAddress

public static void setDefaultBounceAddress(String email)

setDefaultPort

public static void setDefaultPort(int port)

setDefaultPort

public static void setDefaultPort(String port)

setDefaultSslConnection

public static void setDefaultSslConnection(boolean active)

setSSLConnection

public static void setSSLConnection(boolean active)
Deprecated. use setDefaultSslConnection instead.

Sets the default connection to ssl.

Parameters:
active - true to set default connection to SSL

setFactoryFallBack

public static void setFactoryFallBack(String active)
true or false

Parameters:
active - (String)

setSslConnection

public void setSslConnection(boolean flag)

setDebug

public void setDebug(boolean d)
Setting to true will enable the display of debug information.

Parameters:
d - A boolean.

setAuthentication

public void setAuthentication(String userName,
                              String password)
Sets the userName and password if authentication is needed. If this method is not used, no authentication will be performed.

This method will create a new instance of DefaultAuthenticator using the supplied parameters.

Parameters:
userName - User name for the SMTP server
password - password for the SMTP server
See Also:
DefaultAuthenticator, setAuthenticator(javax.mail.Authenticator)

setAuthenticator

public void setAuthenticator(javax.mail.Authenticator newAuthenticator)
Sets the Authenticator to be used when authentication is requested from the mail server.

This method should be used when your outgoing mail server requires authentication. Your mail server must also support RFC2554.

Parameters:
newAuthenticator - the Authenticator object.
See Also:
Authenticator

setCharset

public void setCharset(String newCharset)
Set the charset of the message.

Parameters:
newCharset - A String.

setContent

public void setContent(javax.mail.internet.MimeMultipart aMimeMultipart)
Set the emailBody to a MimeMultiPart

Parameters:
aMimeMultipart - aMimeMultipart

setContent

public void setContent(Object aObject,
                       String aContentType)
Set the content & contentType

Parameters:
aObject - aObject
aContentType - aContentType

setHostName

public void setHostName(String aHostName)
Set the hostname of the outgoing mail server

Parameters:
aHostName - aHostName

setSmtpPort

public void setSmtpPort(int aPortNumber)
Set the port number of the outgoing mail server.

Parameters:
aPortNumber - aPortNumber

setMailSession

public void setMailSession(javax.mail.Session aSession)
Supply a mail Session object to use

Parameters:
aSession - mail session to be used

getMailSession

protected javax.mail.Session getMailSession()
                                     throws EmailException
Initialise a mailsession object

Returns:
A Session.
Throws:
EmailException - thrown when host name was not set.

setFrom

public Email setFrom(String email)
              throws EmailException
Set the FROM field of the email.

Parameters:
email - A String.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address.

setFrom

public Email setFrom(String email,
                     String name)
              throws EmailException
Set the FROM field of the email.

Parameters:
email - A String.
name - A String.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address.

addTo

public Email addTo(String email)
            throws EmailException
Add a recipient TO to the email.

Parameters:
email - A String.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address.

addTo

public Email addTo(String email,
                   String name)
            throws EmailException
Add a recipient TO to the email.

Parameters:
email - A String.
name - A String.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address.

removeTo

public void removeTo(String email)

setTo

public Email setTo(Collection aCollection)
            throws EmailException
Set a list of "TO" addresses.

Parameters:
aCollection - collection of InternetAddress objects.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address.

addCc

public Email addCc(String email)
            throws EmailException
Add a recipient CC to the email.

Parameters:
email - A String.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address.

addCc

public Email addCc(String email,
                   String name)
            throws EmailException
Add a recipient CC to the email.

Parameters:
email - A String.
name - A String.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address.

setCc

public Email setCc(Collection aCollection)
            throws EmailException
Set a list of "CC" addresses.

Parameters:
aCollection - collection of InternetAddress objects.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address.

removeCc

public void removeCc(String email)

addBcc

public Email addBcc(String email)
             throws EmailException
Add a blind BCC recipient to the email.

Parameters:
email - A String.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address

addBcc

public Email addBcc(String email,
                    String name)
             throws EmailException
Add a blind BCC recipient to the email.

Parameters:
email - A String.
name - A String.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address

removeBcc

public void removeBcc(String email)

setBcc

public Email setBcc(Collection aCollection)
             throws EmailException
Set a list of "BCC" addresses

Parameters:
aCollection - collection of InternetAddress objects
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address

addReplyTo

public Email addReplyTo(String email)
                 throws EmailException
Add a reply to address to the email.

Parameters:
email - A String.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address

addReplyTo

public Email addReplyTo(String email,
                        String name)
                 throws EmailException
Add a reply to address to the email.

Parameters:
email - A String.
name - A String.
Returns:
An Email.
Throws:
EmailException - Indicates an invalid email address

setHeaders

public void setHeaders(Map<String,String> map)
Used to specify the mail headers. Example: X-Mailer: Sendmail, X-Priority: 1( highest ) or 2( high ) 3( normal ) 4( low ) and 5( lowest ) Disposition-Notification-To: user@domain.net

Parameters:
map - A Map.

addHeader

public void addHeader(String name,
                      String value)
Adds a header ( name, value ) to the headers Map.

Parameters:
name - A String with the name.
value - A String with the value.

setSubject

public Email setSubject(String aSubject)
Set the email subject.

Parameters:
aSubject - A String.
Returns:
An Email.

setBounceAddress

public Email setBounceAddress(String email)
Set the "bounce address" - the address to which undeliverable messages will be returned. If this value is never set, then the message will be sent to the address specified with the System property "mail.smtp.from", or if that value is not set, then to the "from" address.

Parameters:
email - A String.
Returns:
An Email.

setMsg

public abstract Email setMsg(String msg)
                      throws EmailException
Define the content of the mail. It should be overidden by the subclasses.

Parameters:
msg - A String.
Returns:
An Email.
Throws:
EmailException - generic exception.

sendLater

public void sendLater()

sendLater

public void sendLater(boolean recoverFromBadEmails)

sendLater

public void sendLater(boolean printException,
                      boolean recoverFromBadEmails)

send

public void send()
          throws EmailException
Throws:
EmailException

send

public void send(boolean recoverFromBadEmails)
          throws EmailException
Does the work of actually sending the email.

Throws:
EmailException - if there was an error.

setSentDate

public void setSentDate(Date date)
Sets the sent date for the email. The sent date will default to the current date if not explictly set.

Parameters:
date - Date to use as the sent date on the email

getSentDate

public Date getSentDate()
Gets the sent date for the email.

Returns:
date to be used as the sent date for the email

toInternetAddressArray

protected javax.mail.internet.InternetAddress[] toInternetAddressArray(List<javax.mail.internet.InternetAddress> list)
Utility to copy List of known InternetAddress objects into an array.

Parameters:
list - A List.
Returns:
An InternetAddress[].

setPopBeforeSmtp

public void setPopBeforeSmtp(boolean newPopBeforeSmtp,
                             String newPopHost,
                             String newPopUsername,
                             String newPopPassword)
Set details regarding "pop3 before smtp" authentication.

Parameters:
newPopBeforeSmtp - Wether or not to log into pop3 server before sending mail.
newPopHost - The pop3 host to use.
newPopUsername - The pop3 username.
newPopPassword - The pop3 password.


Copyright © 2013. All Rights Reserved.