adams.core.net
Class EmailHelper

java.lang.Object
  extended by adams.core.net.EmailHelper

public class EmailHelper
extends Object

A helper class for emails.

Version:
$Revision: 6207 $
Author:
fracpete (fracpete at waikato dot ac dot nz)

Field Summary
static String DEFAULT_ADDRESS_FROM
          The default "from" email address.
static String DEFAULT_SIGNATURE
          The default "signature".
static String ENABLED
          Whether Email support is enabled.
static String FILENAME
          the name of the props file.
static String KEY_SMTPAUTH
          the system-wide property for SMTP Auth.
static String KEY_SMTPHOST
          the system-wide property for the SMTP host.
static String KEY_SMTPPORT
          the system-wide property for the SMTP port.
static String KEY_STARTTLS
          the system-wide property for StartTLS.
protected static Properties m_Properties
          the properties.
static String SIGNATURE_SEPARATOR
          the separator between body and signature.
static String SMTP_PASSWORD
          The password the SMTP server.
static String SMTP_PORT
          The SMTP port.
static String SMTP_REQUIRES_AUTHENTICATION
          Whether authentication is necessary.
static String SMTP_SERVER
          The SMTP server.
static String SMTP_START_TLS
          Whether STARTTLS is necessary.
static String SMTP_USER
          The user for the SMTP server.
 
Constructor Summary
EmailHelper()
           
 
Method Summary
static String combine(String body, String signature)
          Combines body and signature, but only if the signatures is neither null nor empty.
static String getDefaultFromAddress()
          Returns the default FROM address.
static String getDefaultSignature()
          Returns the default signature.
static Properties getProperties()
          Returns the underlying properties.
static BasePassword getSmtpPassword()
          Returns the SMTP password.
static int getSmtpPort()
          Returns the SMTP port.
static boolean getSmtpRequiresAuthentication()
          Returns whether the server requires authentication.
static String getSmtpServer()
          Returns the SMTP server.
static boolean getSmtpStartTLS()
          Returns whether to start TLS.
static String getSmtpUser()
          Returns the SMTP user.
static Properties initializeSmtpServer()
          Initializes the SMTP server.
static javax.mail.Session initializeSmtpSession(Properties props)
          Initializes the SMTP session.
static boolean isEnabled()
          Returns whether email support has been enabled.
static javax.mail.internet.MimeMessage newMessage(javax.mail.Session session, String fromAddress, String[] toAddress, String subject)
          Creates a new email message.
static javax.mail.internet.MimeMessage newMessage(javax.mail.Session session, String fromAddress, String[] toAddress, String[] ccAddress, String[] bccAddress, String subject)
          Creates a new email message.
static javax.mail.internet.MimeMessage newMessage(javax.mail.Session session, String fromAddress, String toAddress, String subject)
          Creates a new email message.
static boolean sendMail(String fromAddress, String[] toAddress, String[] ccAddress, String[] bccAddress, String subject, String body, File[] attachments)
          Sends an email.
static boolean sendMail(String fromAddress, String[] toAddress, String subject, String body)
          Sends an email with no attachments.
static boolean sendMail(String fromAddress, String[] toAddress, String subject, String body, File[] attachments)
          Sends an email.
static boolean sendMail(String fromAddress, String toAddress, String subject, String body)
          Sends an email with no attachments.
static boolean sendMail(String fromAddress, String toAddress, String subject, String body, File[] attachments)
          Sends an email.
static boolean writeProperties()
          Writes the specified properties to disk.
static boolean writeProperties(Properties props)
          Writes the specified properties to disk.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILENAME

public static final String FILENAME
the name of the props file.

See Also:
Constant Field Values

ENABLED

public static final String ENABLED
Whether Email support is enabled.

See Also:
Constant Field Values

SMTP_SERVER

public static final String SMTP_SERVER
The SMTP server.

See Also:
Constant Field Values

SMTP_PORT

public static final String SMTP_PORT
The SMTP port.

See Also:
Constant Field Values

SMTP_REQUIRES_AUTHENTICATION

public static final String SMTP_REQUIRES_AUTHENTICATION
Whether authentication is necessary.

See Also:
Constant Field Values

SMTP_START_TLS

public static final String SMTP_START_TLS
Whether STARTTLS is necessary.

See Also:
Constant Field Values

SMTP_USER

public static final String SMTP_USER
The user for the SMTP server.

See Also:
Constant Field Values

SMTP_PASSWORD

public static final String SMTP_PASSWORD
The password the SMTP server.

See Also:
Constant Field Values

DEFAULT_ADDRESS_FROM

public static final String DEFAULT_ADDRESS_FROM
The default "from" email address.

See Also:
Constant Field Values

DEFAULT_SIGNATURE

public static final String DEFAULT_SIGNATURE
The default "signature".

See Also:
Constant Field Values

KEY_SMTPHOST

public static final String KEY_SMTPHOST
the system-wide property for the SMTP host.

See Also:
Constant Field Values

KEY_SMTPPORT

public static final String KEY_SMTPPORT
the system-wide property for the SMTP port.

See Also:
Constant Field Values

KEY_STARTTLS

public static final String KEY_STARTTLS
the system-wide property for StartTLS.

See Also:
Constant Field Values

KEY_SMTPAUTH

public static final String KEY_SMTPAUTH
the system-wide property for SMTP Auth.

See Also:
Constant Field Values

SIGNATURE_SEPARATOR

public static final String SIGNATURE_SEPARATOR
the separator between body and signature.

See Also:
Constant Field Values

m_Properties

protected static Properties m_Properties
the properties.

Constructor Detail

EmailHelper

public EmailHelper()
Method Detail

getProperties

public static Properties getProperties()
Returns the underlying properties.

Returns:
the properties

writeProperties

public static boolean writeProperties()
Writes the specified properties to disk.

Parameters:
props - the properties to write to disk
Returns:
true if successfully stored

writeProperties

public static boolean writeProperties(Properties props)
Writes the specified properties to disk.

Parameters:
props - the properties to write to disk
Returns:
true if successfully stored

isEnabled

public static boolean isEnabled()
Returns whether email support has been enabled.

Returns:
true if enabled

getSmtpServer

public static String getSmtpServer()
Returns the SMTP server.

Returns:
the server

getSmtpPort

public static int getSmtpPort()
Returns the SMTP port.

Returns:
the port

getSmtpStartTLS

public static boolean getSmtpStartTLS()
Returns whether to start TLS.

Returns:
true if to start TLS

getSmtpRequiresAuthentication

public static boolean getSmtpRequiresAuthentication()
Returns whether the server requires authentication.

Returns:
true if authentication required

getSmtpUser

public static String getSmtpUser()
Returns the SMTP user.

Returns:
the user

getSmtpPassword

public static BasePassword getSmtpPassword()
Returns the SMTP password.

Returns:
the password

getDefaultFromAddress

public static String getDefaultFromAddress()
Returns the default FROM address.

Returns:
the default address

getDefaultSignature

public static String getDefaultSignature()
Returns the default signature.

Returns:
the default signature (back quoted)
See Also:
Utils.backQuoteChars(String)

initializeSmtpServer

public static Properties initializeSmtpServer()
Initializes the SMTP server.

Returns:
the initialized properties

initializeSmtpSession

public static javax.mail.Session initializeSmtpSession(Properties props)
Initializes the SMTP session.

Parameters:
props - the system properties
Returns:
the session

newMessage

public static javax.mail.internet.MimeMessage newMessage(javax.mail.Session session,
                                                         String fromAddress,
                                                         String toAddress,
                                                         String subject)
                                                  throws javax.mail.internet.AddressException,
                                                         javax.mail.MessagingException
Creates a new email message.

Parameters:
session - the server session
fromAddress - the sender, null to use default
toAddress - the recipient
subject - the subject
Returns:
the email message
Throws:
javax.mail.internet.AddressException - in case of invalid internet addresses
javax.mail.MessagingException - in case of a messaging problem

newMessage

public static javax.mail.internet.MimeMessage newMessage(javax.mail.Session session,
                                                         String fromAddress,
                                                         String[] toAddress,
                                                         String subject)
                                                  throws javax.mail.internet.AddressException,
                                                         javax.mail.MessagingException
Creates a new email message.

Parameters:
session - the server session
fromAddress - the sender, null to use default
toAddress - the recipients
subject - the subject
Returns:
the email message
Throws:
javax.mail.internet.AddressException - in case of invalid internet addresses
javax.mail.MessagingException - in case of a messaging problem

newMessage

public static javax.mail.internet.MimeMessage newMessage(javax.mail.Session session,
                                                         String fromAddress,
                                                         String[] toAddress,
                                                         String[] ccAddress,
                                                         String[] bccAddress,
                                                         String subject)
                                                  throws javax.mail.internet.AddressException,
                                                         javax.mail.MessagingException
Creates a new email message.

Parameters:
session - the server session
fromAddress - the sender, null to use default
toAddress - the recipients, can be null
ccAddress - the CC recipients, can be null
bccAddress - the BCC recipients, can be null
subject - the subject
Returns:
the email message
Throws:
javax.mail.internet.AddressException - in case of invalid internet addresses
javax.mail.MessagingException - in case of a messaging problem

sendMail

public static boolean sendMail(String fromAddress,
                               String toAddress,
                               String subject,
                               String body)
                        throws javax.mail.internet.AddressException,
                               javax.mail.MessagingException,
                               MalformedURLException
Sends an email with no attachments.

Parameters:
fromAdress - the sender, null to use default
toAddress - the address to send the email to
subject - the subject of the email
body - the body of the message
Returns:
true if successfully sent
Throws:
javax.mail.internet.AddressException - in case of invalid internet addresses
javax.mail.MessagingException - in case of a messaging problem
MalformedURLException - never

sendMail

public static boolean sendMail(String fromAddress,
                               String[] toAddress,
                               String subject,
                               String body)
                        throws javax.mail.internet.AddressException,
                               javax.mail.MessagingException,
                               MalformedURLException
Sends an email with no attachments.

Parameters:
fromAdress - the sender, null to use default
toAddress - the addresses to send the email to
subject - the subject of the email
body - the body of the message
Returns:
true if successfully sent
Throws:
javax.mail.internet.AddressException - in case of invalid internet addresses
javax.mail.MessagingException - in case of a messaging problem
MalformedURLException - never

sendMail

public static boolean sendMail(String fromAddress,
                               String toAddress,
                               String subject,
                               String body,
                               File[] attachments)
                        throws javax.mail.internet.AddressException,
                               javax.mail.MessagingException,
                               MalformedURLException
Sends an email.

Parameters:
fromAdress - the sender, null to use default
toAddress - the address to send the email to
subject - the subject of the email
body - the body of the message
attachments - the list of files to attach
Returns:
true if successfully sent
Throws:
javax.mail.internet.AddressException - in case of invalid internet addresses
javax.mail.MessagingException - in case of a messaging problem
MalformedURLException - in case of a malformed URL of an attachment

sendMail

public static boolean sendMail(String fromAddress,
                               String[] toAddress,
                               String subject,
                               String body,
                               File[] attachments)
                        throws javax.mail.internet.AddressException,
                               javax.mail.MessagingException,
                               MalformedURLException
Sends an email.

Parameters:
fromAdress - the sender, null to use default
toAddress - the addresses to send the email to
subject - the subject of the email
body - the body of the message
attachments - the list of files to attach
Returns:
true if successfully sent
Throws:
javax.mail.internet.AddressException - in case of invalid internet addresses
javax.mail.MessagingException - in case of a messaging problem
MalformedURLException - in case of a malformed URL of an attachment

sendMail

public static boolean sendMail(String fromAddress,
                               String[] toAddress,
                               String[] ccAddress,
                               String[] bccAddress,
                               String subject,
                               String body,
                               File[] attachments)
                        throws javax.mail.internet.AddressException,
                               javax.mail.MessagingException,
                               MalformedURLException
Sends an email.

Parameters:
fromAdress - the sender, null to use default
toAddress - the addresses to send the email to, can be null
ccAddress - the CC addresses to send the email to, can be null
bccAddress - the BCC addresses to send the email to, can be null
subject - the subject of the email
body - the body of the message
attachments - the list of files to attach
Returns:
true if successfully sent
Throws:
javax.mail.internet.AddressException - in case of invalid internet addresses
javax.mail.MessagingException - in case of a messaging problem
MalformedURLException - in case of a malformed URL of an attachment

combine

public static String combine(String body,
                             String signature)
Combines body and signature, but only if the signatures is neither null nor empty.

Parameters:
body - the actual body of the email
signature - the signature to add, ignored if null or empty
Returns:
the extended body


Copyright © 2012 University of Waikato, Hamilton, NZ. All Rights Reserved.