The com.arsdigita.mail.Mail class did not support the use of SMTP servers which

require authentication. The class now checks if the mail properties are 
containing the property mail.user and uses the properties mail.user and 
mail.password for creating an instance of the javax.mail.Authenticator which is
added to the javax.mail.Session used by the Mail class. If there is no 
mail.user property the old behaviour is used.


git-svn-id: https://svn.libreccm.org/ccm/trunk@3727 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2015-11-16 09:33:13 +00:00
parent 6976da93ab
commit 9f16494b9a
1 changed files with 233 additions and 220 deletions

View File

@ -19,6 +19,7 @@
package com.arsdigita.mail; package com.arsdigita.mail;
import com.arsdigita.util.MessageType; import com.arsdigita.util.MessageType;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -33,6 +34,7 @@ import java.util.Enumeration;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import javax.activation.DataHandler; import javax.activation.DataHandler;
import javax.activation.FileDataSource; import javax.activation.FileDataSource;
import javax.activation.URLDataSource; import javax.activation.URLDataSource;
@ -45,26 +47,30 @@ import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility; import javax.mail.internet.MimeUtility;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
import javax.mail.PasswordAuthentication;
/** /**
* Represents a email message with optional attachments. This class * Represents a email message with optional attachments. This class is a wrapper
* is a wrapper for the JavaMail API that makes it easier for * for the JavaMail API that makes it easier for application developers to
* application developers to create and send email. For simple plain * create and send email. For simple plain text message, there is a static
* text message, there is a static convenience method that does not * convenience method that does not require the construction of an explicit Mail
* require the construction of an explicit Mail object: * object:
* *
* <pre> * <pre>
* Mail.send(to, from, subject, body) * Mail.send(to, from, subject, body)
* </pre> * </pre>
* *
* <p>For more complex messages, the API provides methods to set all * <p>
* standard mail headers, attach other pieces of content, and finally * For more complex messages, the API provides methods to set all standard mail
* invoke the transport process to deliver the message. * headers, attach other pieces of content, and finally invoke the transport
* process to deliver the message.
* *
* @author Ron Henderson * @author Ron Henderson
* @version $Id: Mail.java 994 2005-11-14 14:29:25Z apevec $ * @version $Id: Mail.java 994 2005-11-14 14:29:25Z apevec $
@ -74,8 +80,7 @@ public class Mail implements MessageType {
/** /**
* Used for logging. * Used for logging.
*/ */
private static final Logger s_log = private static final Logger s_log = Logger.getLogger(Mail.class);
Logger.getLogger(Mail.class);
private static MailConfig s_config; private static MailConfig s_config;
public static MailConfig getConfig() { public static MailConfig getConfig() {
@ -86,8 +91,9 @@ public class Mail implements MessageType {
} }
return s_config; return s_config;
} }
private static final InternetAddress[] EMPTY_ADDRESS_LIST =
new InternetAddress[0]; private static final InternetAddress[] EMPTY_ADDRESS_LIST
= new InternetAddress[0];
/** /**
* Table of message headers. * Table of message headers.
*/ */
@ -99,11 +105,13 @@ public class Mail implements MessageType {
private InternetAddress[] m_filteredTo = EMPTY_ADDRESS_LIST; private InternetAddress[] m_filteredTo = EMPTY_ADDRESS_LIST;
private InternetAddress[] m_invalidTo = EMPTY_ADDRESS_LIST; private InternetAddress[] m_invalidTo = EMPTY_ADDRESS_LIST;
private static Set s_invalidDomains = new HashSet(); private static Set s_invalidDomains = new HashSet();
static { static {
s_log.debug("Static initalizer starting..."); s_log.debug("Static initalizer starting...");
s_invalidDomains.add("example.com"); s_invalidDomains.add("example.com");
s_log.debug("Static initalizer finished."); s_log.debug("Static initalizer finished.");
} }
/** /**
* Email address the message is being sent from. * Email address the message is being sent from.
*/ */
@ -133,8 +141,8 @@ public class Mail implements MessageType {
*/ */
private String m_alternate; private String m_alternate;
/** /**
* Encoding specification for m_body and m_alternate (optional). * Encoding specification for m_body and m_alternate (optional). Default
* Default value (null) implies "us-ascii" encoding. * value (null) implies "us-ascii" encoding.
*/ */
private String m_encoding; private String m_encoding;
/** /**
@ -150,13 +158,13 @@ public class Mail implements MessageType {
*/ */
private static Session s_session; private static Session s_session;
/** /**
* SMTP host to connect to. Only used to override the default for * SMTP host to connect to. Only used to override the default for testing
* testing purposes. * purposes.
*/ */
private static String s_host; private static String s_host;
/** /**
* SMTP port to connect to. Only used to override the default for * SMTP port to connect to. Only used to override the default for testing
* testing purposes. * purposes.
*/ */
private static String s_port; private static String s_port;
// Constants used by Mail // Constants used by Mail
@ -174,8 +182,8 @@ public class Mail implements MessageType {
public final static String ATTACHMENT = javax.mail.Part.ATTACHMENT; public final static String ATTACHMENT = javax.mail.Part.ATTACHMENT;
/** /**
* Default constructor. Must use the setTo, setSubject (and so on) * Default constructor. Must use the setTo, setSubject (and so on) methods
* methods to create a valid mail message. * to create a valid mail message.
*/ */
public Mail() { public Mail() {
this(null, null, null, null); this(null, null, null, null);
@ -184,8 +192,8 @@ public class Mail implements MessageType {
/** /**
* Constructor used to specify to, from, and subject. * Constructor used to specify to, from, and subject.
* *
* @param to one or more of addresses to send the message to * @param to one or more of addresses to send the message to
* @param from the address the message is being sent from * @param from the address the message is being sent from
* @param subject the subject for the message * @param subject the subject for the message
*/ */
public Mail(String to, public Mail(String to,
@ -197,10 +205,10 @@ public class Mail implements MessageType {
/** /**
* Constructor used to specify to, from, subject, and body. * Constructor used to specify to, from, subject, and body.
* *
* @param to one or more of addresses to send the message to * @param to one or more of addresses to send the message to
* @param from the address the message is being sent from * @param from the address the message is being sent from
* @param subject the subject for the message * @param subject the subject for the message
* @param body the plain text body of the message * @param body the plain text body of the message
*/ */
public Mail(String to, public Mail(String to,
String from, String from,
@ -214,14 +222,13 @@ public class Mail implements MessageType {
} }
/** /**
* Constructor used to specify to, from, subject, body, and * Constructor used to specify to, from, subject, body, and encoding.
* encoding.
* *
* @param to one or more of addresses to send the message to * @param to one or more of addresses to send the message to
* @param from the address the message is being sent from * @param from the address the message is being sent from
* @param subject the subject for the message * @param subject the subject for the message
* @param body is plain text body of the message * @param body is plain text body of the message
* @param enc the encoding of the body * @param enc the encoding of the body
*/ */
public Mail(String to, public Mail(String to,
String from, String from,
@ -235,17 +242,17 @@ public class Mail implements MessageType {
/** /**
* A convenience method to send a simple plain-text message. * A convenience method to send a simple plain-text message.
* *
* @param to one or more of addresses to send the message to * @param to one or more of addresses to send the message to
* @param from the address the message is being sent from * @param from the address the message is being sent from
* @param subject the subject for the message * @param subject the subject for the message
* @param body the plain text body of the message * @param body the plain text body of the message
*/ */
public static void send(String to, public static void send(String to,
String from, String from,
String subject, String subject,
String body) String body)
throws MessagingException, throws MessagingException,
SendFailedException { SendFailedException {
Mail msg = new Mail(to, from, subject, body); Mail msg = new Mail(to, from, subject, body);
msg.send(); msg.send();
} }
@ -254,8 +261,8 @@ public class Mail implements MessageType {
* Sends the message. * Sends the message.
*/ */
public void send() public void send()
throws MessagingException, throws MessagingException,
SendFailedException { SendFailedException {
Transport transport = getSession().getTransport(); Transport transport = getSession().getTransport();
transport.connect(); transport.connect();
send(transport); send(transport);
@ -263,17 +270,18 @@ public class Mail implements MessageType {
} }
/** /**
* Sends the message using a given Transport object (package-level * Sends the message using a given Transport object (package-level access).
* access). This method is used when sending multiple messages at * This method is used when sending multiple messages at once with a single
* once with a single connection to the mail server. * connection to the mail server.
* *
* @throws SendFailedException on any kind of MessagingException, * @throws SendFailedException on any kind of MessagingException, also such
* also such returned from the server. Applications might try * returned from the server. Applications might
* to catch this and re-schedule sending the mail. * try to catch this and re-schedule sending the
* mail.
*/ */
void send(Transport transport) void send(Transport transport)
throws MessagingException, throws MessagingException,
SendFailedException { SendFailedException {
Message msg = null; Message msg = null;
if (m_filteredTo.length > 0) { if (m_filteredTo.length > 0) {
msg = getMessage(); msg = getMessage();
@ -284,7 +292,6 @@ public class Mail implements MessageType {
// Close the transport agent and rethrow error for // Close the transport agent and rethrow error for
// detailed message. // detailed message.
transport.close(); transport.close();
throw new SendFailedException("send failed: ", mex); throw new SendFailedException("send failed: ", mex);
@ -292,14 +299,13 @@ public class Mail implements MessageType {
} }
// Write a copy of the message into the log file // Write a copy of the message into the log file
if (getConfig().isDebug()) { if (getConfig().isDebug()) {
if (msg != null) { if (msg != null) {
try { try {
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
msg.writeTo(os); msg.writeTo(os);
s_log.debug("message sent:\n" + os.toString() s_log.debug("message sent:\n" + os.toString()
+ "\n-- EOT --"); + "\n-- EOT --");
} catch (IOException ex) { } catch (IOException ex) {
s_log.error("unable to log message"); s_log.error("unable to log message");
} }
@ -308,15 +314,16 @@ public class Mail implements MessageType {
} }
} else { } else {
s_log.info("message sent to <" + Arrays.asList(m_filteredTo) s_log.info("message sent to <" + Arrays.asList(m_filteredTo)
+ "> from <" + m_from + "> subject <" + m_subject + ">"); + "> from <" + m_from + "> subject <" + m_subject
+ ">");
s_log.info("messages filtered for <" + Arrays.asList(m_invalidTo) s_log.info("messages filtered for <" + Arrays.asList(m_invalidTo)
+ "> from <" + m_from + "> subject <" + m_subject + ">"); + "> from <" + m_from + "> subject <" + m_subject
+ ">");
} }
} }
/** /**
* Sets the email address that the message is being sent * Sets the email address that the message is being sent to.
* to.
* *
* @param to one or more addresses to send the message to * @param to one or more addresses to send the message to
*/ */
@ -326,8 +333,7 @@ public class Mail implements MessageType {
} }
/** /**
* Sets the email address that the message is being sent * Sets the email address that the message is being sent from.
* from.
* *
* @param from the address the message is sent from * @param from the address the message is sent from
*/ */
@ -374,7 +380,7 @@ public class Mail implements MessageType {
/** /**
* Adds a header (name, value) pair. * Adds a header (name, value) pair.
* *
* @param name the header element name * @param name the header element name
* @param value the header element value * @param value the header element value
*/ */
public void addHeader(String name, String value) { public void addHeader(String name, String value) {
@ -387,6 +393,7 @@ public class Mail implements MessageType {
/** /**
* Sets the email address that is being carbon-copied. * Sets the email address that is being carbon-copied.
*
* @param cc the email address for a carbon copy * @param cc the email address for a carbon copy
*/ */
public void setCc(String cc) { public void setCc(String cc) {
@ -395,6 +402,7 @@ public class Mail implements MessageType {
/** /**
* Sets the email address that is being blind carbon-copied. * Sets the email address that is being blind carbon-copied.
*
* @param bcc the email address for a blind carbon copy * @param bcc the email address for a blind carbon copy
*/ */
public void setBcc(String bcc) { public void setBcc(String bcc) {
@ -403,6 +411,7 @@ public class Mail implements MessageType {
/** /**
* Sets the body of the email to a simple plain text message. * Sets the body of the email to a simple plain text message.
*
* @param body the body of the message in plain text * @param body the body of the message in plain text
*/ */
public void setBody(String body) { public void setBody(String body) {
@ -410,11 +419,11 @@ public class Mail implements MessageType {
} }
/** /**
* Sets the body of the email to an HTML encoded message with a * Sets the body of the email to an HTML encoded message with a plain text
* plain text alternative. * alternative.
* *
* @param body the body of the message in HTML * @param body the body of the message in HTML
* @param alt the alternate message body in plain text * @param alt the alternate message body in plain text
*/ */
public void setBody(String body, String alt) { public void setBody(String body, String alt) {
m_body = body; m_body = body;
@ -432,8 +441,8 @@ public class Mail implements MessageType {
} }
/** /**
* Returns the character encoding that is being used. The default * Returns the character encoding that is being used. The default is
* is "us-ascii". * "us-ascii".
* *
* @return the string value of the character encoding being used * @return the string value of the character encoding being used
*/ */
@ -442,14 +451,14 @@ public class Mail implements MessageType {
} }
/** /**
* Adds an attachment to a message. This method is private but * Adds an attachment to a message. This method is private but is invoked by
* is invoked by all of the other attach methods once they've * all of the other attach methods once they've constructed an appropraite
* constructed an appropraite MimeBodyPart to attach. * MimeBodyPart to attach.
* *
* @param part the message part to attach * @param part the message part to attach
*/ */
private void attach(MimeBodyPart part) private void attach(MimeBodyPart part)
throws MessagingException { throws MessagingException {
if (m_attachments == null) { if (m_attachments == null) {
m_attachments = new MimeMultipart(); m_attachments = new MimeMultipart();
} }
@ -457,27 +466,26 @@ public class Mail implements MessageType {
} }
/** /**
* Adds an attachment with a specified name and description to a * Adds an attachment with a specified name and description to a message by
* message by fetching its content from a URL. Sets the * fetching its content from a URL. Sets the disposition to ATTACHMENT.
* disposition to ATTACHMENT.
* *
* @param url the URL to retreieve the content from * @param url the URL to retreieve the content from
* @param name the name of the attachment * @param name the name of the attachment
* @param description a description of the attachment * @param description a description of the attachment
*/ */
public void attach(URL url, public void attach(URL url,
String name, String name,
String description) String description)
throws MessagingException { throws MessagingException {
attach(url, name, description, Mail.ATTACHMENT); attach(url, name, description, Mail.ATTACHMENT);
} }
/** /**
* Adds an attachment with a specified name, description and * Adds an attachment with a specified name, description and disposition to
* disposition to a message by fetching its content from a URL. * a message by fetching its content from a URL.
* *
* @param url the URL to retreieve the content from * @param url the URL to retreieve the content from
* @param name the name of the attachment * @param name the name of the attachment
* @param description a description of the attachment * @param description a description of the attachment
* @param disposition Mail.ATTACHMENT or Mail.INLINE * @param disposition Mail.ATTACHMENT or Mail.INLINE
*/ */
@ -485,7 +493,7 @@ public class Mail implements MessageType {
String name, String name,
String description, String description,
String disposition) String disposition)
throws MessagingException { throws MessagingException {
MimeBodyPart part = new MimeBodyPart(); MimeBodyPart part = new MimeBodyPart();
attach(part); attach(part);
@ -498,28 +506,27 @@ public class Mail implements MessageType {
} }
/** /**
* Adds an attachment with a specified name and description to a * Adds an attachment with a specified name and description to a message by
* message by fetching its content from a local file. Sets the * fetching its content from a local file. Sets the disposition to
* disposition to ATTACHMENT. * ATTACHMENT.
* *
* @param path the file path to retreieve the content from * @param path the file path to retreieve the content from
* @param name the name of the attachment * @param name the name of the attachment
* @param description a description of the attachment * @param description a description of the attachment
*/ */
public void attach(File path, public void attach(File path,
String name, String name,
String description) String description)
throws MessagingException { throws MessagingException {
attach(path, name, description, ATTACHMENT); attach(path, name, description, ATTACHMENT);
} }
/** /**
* Adds an attachment with a specified name, description and * Adds an attachment with a specified name, description and disposition to
* disposition to a message by fetching its content from a * a message by fetching its content from a local file.
* local file.
* *
* @param path the file path to retreieve the content from * @param path the file path to retreieve the content from
* @param name the name of the attachment * @param name the name of the attachment
* @param description a description of the attachment * @param description a description of the attachment
* @param disposition Mail.ATTACHMENT or Mail.INLINE * @param disposition Mail.ATTACHMENT or Mail.INLINE
*/ */
@ -527,7 +534,7 @@ public class Mail implements MessageType {
String name, String name,
String description, String description,
String disposition) String disposition)
throws MessagingException { throws MessagingException {
MimeBodyPart part = new MimeBodyPart(); MimeBodyPart part = new MimeBodyPart();
attach(part); attach(part);
@ -540,100 +547,98 @@ public class Mail implements MessageType {
} }
/** /**
* Attaches a byte array to a message. Sets the MIME type and * Attaches a byte array to a message. Sets the MIME type and name of the
* name of the attachment, and initializes its disposition to * attachment, and initializes its disposition to ATTACHMENT.
*
* @param data the content of the attachment
* @param type the MIME type of the attachment
* @param name the name of the attachment
*/
public void attach(byte[] data,
String type,
String name)
throws MessagingException {
attach(data, type, name, null, ATTACHMENT);
}
/**
* Attaches a byte array to a message. Sets the MIME type, name, description
* and disposition of the attachment.
*
* @param data the content of the attachment
* @param type the MIME type of the attachment
* @param name the name of the attachment
* @param description a description of the attachment
* @param disposition Mail.ATTACHMENT or Mail.INLINE
*/
public void attach(byte[] data,
String type,
String name,
String description,
String disposition)
throws MessagingException {
ByteArrayDataSource ds = new ByteArrayDataSource(data, type, name);
attach(ds, description, disposition);
}
/**
* Attaches a String to a message. Sets the MIME type and name of the
* attachment, and initializes the disposition to ATTACHMENT.
*
* @param data the content of the attachment
* @param type the MIME type of the attachment
* @param name the name of the attachment
*/
public void attach(String data,
String type,
String name)
throws MessagingException {
attach(data, type, name, null, ATTACHMENT);
}
/**
* Attaches a String to a message. Sets the MIME type, name, description and
* disposition of the attachment.
*
* @param data the content of the attachment
* @param type the MIME type of the attachment
* @param name the name of the attachment
* @param description a description of the attachment
* @param disposition Mail.ATTACHMENT or Mail.INLINE
*/
public void attach(String data,
String type,
String name,
String description,
String disposition)
throws MessagingException {
ByteArrayDataSource ds = new ByteArrayDataSource(data, type, name);
attach(ds, description, disposition);
}
/**
* Attaches the content from a ByteArrayInputStream to a message. Sets the
* MIME type and name of the attachment, and initializes the disposition to
* ATTACHMENT. * ATTACHMENT.
* *
* @param data the content of the attachment * @param is the input stream to read from.
* @param type the MIME type of the attachment
* @param name the name of the attachment
*/
public void attach(byte[] data,
String type,
String name)
throws MessagingException {
attach(data, type, name, null, ATTACHMENT);
}
/**
* Attaches a byte array to a message. Sets the MIME type, name,
* description and disposition of the attachment.
*
* @param data the content of the attachment
* @param type the MIME type of the attachment
* @param name the name of the attachment
* @param description a description of the attachment
* @param disposition Mail.ATTACHMENT or Mail.INLINE
*/
public void attach(byte[] data,
String type,
String name,
String description,
String disposition)
throws MessagingException {
ByteArrayDataSource ds = new ByteArrayDataSource(data, type, name);
attach(ds, description, disposition);
}
/**
* Attaches a String to a message. Sets the MIME type and name of
* the attachment, and initializes the disposition to ATTACHMENT.
*
* @param data the content of the attachment
* @param type the MIME type of the attachment
* @param name the name of the attachment
*/
public void attach(String data,
String type,
String name)
throws MessagingException {
attach(data, type, name, null, ATTACHMENT);
}
/**
* Attaches a String to a message. Sets the MIME type, name,
* description and disposition of the attachment.
*
* @param data the content of the attachment
* @param type the MIME type of the attachment
* @param name the name of the attachment
* @param description a description of the attachment
* @param disposition Mail.ATTACHMENT or Mail.INLINE
*/
public void attach(String data,
String type,
String name,
String description,
String disposition)
throws MessagingException {
ByteArrayDataSource ds = new ByteArrayDataSource(data, type, name);
attach(ds, description, disposition);
}
/**
* Attaches the content from a ByteArrayInputStream to a message.
* Sets the MIME type and name of the attachment, and initializes
* the disposition to ATTACHMENT.
*
* @param is the input stream to read from.
* @param type the MIME type of the attachment * @param type the MIME type of the attachment
* @param name the name of the attachment * @param name the name of the attachment
*/ */
public void attach(ByteArrayInputStream is, public void attach(ByteArrayInputStream is,
String type, String type,
String name) String name)
throws MessagingException { throws MessagingException {
attach(is, type, name, null, ATTACHMENT); attach(is, type, name, null, ATTACHMENT);
} }
/** /**
* Attaches the content from a ByteArrayInputStream to a message. * Attaches the content from a ByteArrayInputStream to a message. Sets the
* Sets the MIME type, name, description and disposition of the * MIME type, name, description and disposition of the attachment.
* attachment.
* *
* @param is the input stream to read from. * @param is the input stream to read from.
* @param type the MIME type of the attachment * @param type the MIME type of the attachment
* @param name the name of the attachment * @param name the name of the attachment
* @param description a description of the attachment * @param description a description of the attachment
* @param disposition Mail.ATTACHMENT or Mail.INLINE * @param disposition Mail.ATTACHMENT or Mail.INLINE
*/ */
@ -642,25 +647,25 @@ public class Mail implements MessageType {
String name, String name,
String description, String description,
String disposition) String disposition)
throws MessagingException { throws MessagingException {
ByteArrayDataSource ds = new ByteArrayDataSource(is, type, name); ByteArrayDataSource ds = new ByteArrayDataSource(is, type, name);
attach(ds, description, disposition); attach(ds, description, disposition);
} }
/** /**
* Attaches the content from a ByteArrayDataSource to a * Attaches the content from a ByteArrayDataSource to a message. This is
* message. This is used internally by various other methods that * used internally by various other methods that take higher-level object
* take higher-level object types as input. The MIME type and * types as input. The MIME type and name are determined directly from the
* name are determined directly from the dataSource. * dataSource.
* *
* @param dataSource the data source to read from * @param dataSource the data source to read from
* @param description a description of the attachment * @param description a description of the attachment
* @param disposition Mail.ATTACHMENT or Mail.INLINE * @param disposition Mail.ATTACHMENT or Mail.INLINE
*/ */
protected void attach(ByteArrayDataSource dataSource, protected void attach(ByteArrayDataSource dataSource,
String description, String description,
String disposition) String disposition)
throws MessagingException { throws MessagingException {
MimeBodyPart part = new MimeBodyPart(); MimeBodyPart part = new MimeBodyPart();
attach(part); attach(part);
@ -673,29 +678,29 @@ public class Mail implements MessageType {
} }
/** /**
* Attaches content to a message by supplying a DataHandler. All * Attaches content to a message by supplying a DataHandler. All relevant
* relevant parameters (MIME type, name, ...) are determined * parameters (MIME type, name, ...) are determined directly from the
* directly from the DataHandler. * DataHandler.
* *
* @param dh a DataHandler for some piece of content. * @param dh a DataHandler for some piece of content.
*/ */
public void attach(DataHandler dh) public void attach(DataHandler dh)
throws MessagingException { throws MessagingException {
attach(dh, null, ATTACHMENT); attach(dh, null, ATTACHMENT);
} }
/** /**
* Attaches content to a message by supplying a DataHandler. Sets * Attaches content to a message by supplying a DataHandler. Sets the
* the description and disposition of the content. * description and disposition of the content.
* *
* @param dh the data source to read from * @param dh the data source to read from
* @param description a description of the attachment * @param description a description of the attachment
* @param disposition Mail.ATTACHMENT or Mail.INLINE * @param disposition Mail.ATTACHMENT or Mail.INLINE
*/ */
public void attach(DataHandler dh, public void attach(DataHandler dh,
String description, String description,
String disposition) String disposition)
throws MessagingException { throws MessagingException {
MimeBodyPart part = new MimeBodyPart(); MimeBodyPart part = new MimeBodyPart();
attach(part); attach(part);
@ -706,17 +711,16 @@ public class Mail implements MessageType {
} }
/** /**
* Utility function that returns an appropriate Session object for * Utility function that returns an appropriate Session object for sending
* sending mail. This uses the default properties from the Mail * mail. This uses the default properties from the Mail initializer and any
* initializer and any of properties that can be overridden at * of properties that can be overridden at the package level.
* the package level.
*/ */
static synchronized Session getSession() { static synchronized Session getSession() {
if (s_session == null) { if (s_session == null) {
// Set up the properties // Set up the properties
Properties props = new Properties(getConfig().getJavamail()); final Properties props = new Properties(getConfig().getJavamail());
// Check for overrides of the server information // Check for overrides of the server information
if (s_host != null) { if (s_host != null) {
@ -727,7 +731,22 @@ public class Mail implements MessageType {
} }
// Set up the session // Set up the session
s_session = Session.getInstance(props, null); if (props.getProperty("mail.user") != null) {
s_session = Session.getInstance(
props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
props.getProperty("mail.user"),
props.getProperty("mail.password"));
}
});
} else {
s_session = Session.getInstance(props, null);
}
s_session.setDebug(getConfig().isDebug()); s_session.setDebug(getConfig().isDebug());
} }
@ -735,13 +754,13 @@ public class Mail implements MessageType {
} }
/** /**
* Utility function that returns the message part of the mail. * Utility function that returns the message part of the mail. Useful if you
* Useful if you want to build a separate Transport process (for * want to build a separate Transport process (for example, to queue a
* example, to queue a number of messages to send all at once rather * number of messages to send all at once rather than invoke the Mail.send()
* than invoke the Mail.send() method for each instance.) * method for each instance.)
*/ */
private Message getMessage() private Message getMessage()
throws MessagingException { throws MessagingException {
// Create the message // Create the message
MimeMessage msg = new MimeMessage(getSession()); MimeMessage msg = new MimeMessage(getSession());
@ -796,14 +815,13 @@ public class Mail implements MessageType {
} }
// Return the Message object with it's content ready to go. // Return the Message object with it's content ready to go.
return prepareMessageContent(msg); return prepareMessageContent(msg);
} }
/** /**
* Sets the host and port to connect to when sending * Sets the host and port to connect to when sending mail. Package-level
* mail. Package-level access (should only be used for testing). * access (should only be used for testing).
* *
* @param host the SMTP host to connect to * @param host the SMTP host to connect to
* @param port the port number on that host * @param port the port number on that host
@ -818,6 +836,7 @@ public class Mail implements MessageType {
/** /**
* Returns the SMTP mail host for debugging and account information. * Returns the SMTP mail host for debugging and account information.
*
* @return the SMTP mail host for debugging and account information. * @return the SMTP mail host for debugging and account information.
*/ */
public synchronized static String getSmtpServer() { public synchronized static String getSmtpServer() {
@ -825,13 +844,13 @@ public class Mail implements MessageType {
} }
/** /**
* Writes the content of the message to the given output stream. * Writes the content of the message to the given output stream. Useful for
* Useful for debugging. * debugging.
* *
* @param os the output stream to write the message to * @param os the output stream to write the message to
*/ */
public void writeTo(OutputStream os) public void writeTo(OutputStream os)
throws MessagingException { throws MessagingException {
try { try {
getMessage().writeTo(os); getMessage().writeTo(os);
} catch (IOException ex) { } catch (IOException ex) {
@ -953,7 +972,7 @@ public class Mail implements MessageType {
* Utility function to prepare the content of the message. * Utility function to prepare the content of the message.
*/ */
private Message prepareMessageContent(MimeMessage msg) private Message prepareMessageContent(MimeMessage msg)
throws MessagingException { throws MessagingException {
if (m_alternate == null && m_attachments == null) { if (m_alternate == null && m_attachments == null) {
// We have a plain-text message with no attachments. Use // We have a plain-text message with no attachments. Use
@ -966,24 +985,20 @@ public class Mail implements MessageType {
// For anything else the message will be a MIME multipart, // For anything else the message will be a MIME multipart,
// with a subtype of of either "mixed" or "alternative" // with a subtype of of either "mixed" or "alternative"
// depending on whether we have attachments. // depending on whether we have attachments.
String subtype = (m_attachments == null) ? ALTERNATIVE : MIXED; String subtype = (m_attachments == null) ? ALTERNATIVE : MIXED;
// Create a MIME multipart for the content. // Create a MIME multipart for the content.
MimeMultipart mp = new MimeMultipart(subtype); MimeMultipart mp = new MimeMultipart(subtype);
msg.setContent(mp); msg.setContent(mp);
// Next we need to look at whether the message part of the // Next we need to look at whether the message part of the
// content is going to be text/plain or text/html. // content is going to be text/plain or text/html.
MimeBodyPart part = new MimeBodyPart(); MimeBodyPart part = new MimeBodyPart();
if (m_alternate == null) { if (m_alternate == null) {
// No alternate, so it must be text/plain with // No alternate, so it must be text/plain with
// attachments. // attachments.
part.setText(m_body, m_encoding); part.setText(m_body, m_encoding);
part.setHeader(CONTENT_TYPE, MessageType.TEXT_PLAIN); part.setHeader(CONTENT_TYPE, MessageType.TEXT_PLAIN);
mp.addBodyPart(part); mp.addBodyPart(part);
@ -994,12 +1009,10 @@ public class Mail implements MessageType {
// the first part and the alternate as the second. // the first part and the alternate as the second.
// The overall MIME subtype is probably ALTERNATE // The overall MIME subtype is probably ALTERNATE
// (depending on whether we have attachments). // (depending on whether we have attachments).
part.setText(m_body, m_encoding); part.setText(m_body, m_encoding);
part.setHeader(CONTENT_TYPE, MessageType.TEXT_HTML); part.setHeader(CONTENT_TYPE, MessageType.TEXT_HTML);
mp.addBodyPart(part); mp.addBodyPart(part);
part = new MimeBodyPart(); part = new MimeBodyPart();
part.setText(m_alternate, m_encoding); part.setText(m_alternate, m_encoding);
part.setHeader(CONTENT_TYPE, MessageType.TEXT_PLAIN); part.setHeader(CONTENT_TYPE, MessageType.TEXT_PLAIN);
@ -1010,11 +1023,9 @@ public class Mail implements MessageType {
// Do we have attachments? If so then the MIME subtype // Do we have attachments? If so then the MIME subtype
// must be MIXED and and the attachments need to be // must be MIXED and and the attachments need to be
// transferred to the Message. // transferred to the Message.
if (m_attachments != null) { if (m_attachments != null) {
// Add attachments to the Message content. // Add attachments to the Message content.
for (int i = 0; i < m_attachments.getCount(); i++) { for (int i = 0; i < m_attachments.getCount(); i++) {
mp.addBodyPart(m_attachments.getBodyPart(i)); mp.addBodyPart(m_attachments.getBodyPart(i));
} }
@ -1023,7 +1034,6 @@ public class Mail implements MessageType {
// Save changes to the message. This will update the MIME // Save changes to the message. This will update the MIME
// headers so the message is ready to send. // headers so the message is ready to send.
msg.saveChanges(); msg.saveChanges();
return msg; return msg;
@ -1049,13 +1059,16 @@ public class Mail implements MessageType {
} else { } else {
invalid.add(m_to[i]); invalid.add(m_to[i]);
s_log.debug("filtering message to non-existent email address " s_log.debug("filtering message to non-existent email address "
+ m_to[i]); + m_to[i]);
} }
} }
m_filteredTo = (InternetAddress[]) filtered.toArray(new InternetAddress[filtered. m_filteredTo = (InternetAddress[]) filtered.toArray(
size()]); new InternetAddress[filtered.
m_invalidTo = (InternetAddress[]) invalid.toArray(new InternetAddress[invalid. size()]);
size()]); m_invalidTo = (InternetAddress[]) invalid.toArray(
new InternetAddress[invalid.
size()]);
return m_filteredTo; return m_filteredTo;
} }
} }