com.darwinsys.mail.Mailer Maven / Gradle / Ivy
// BEGIN package
package com.darwinsys.mail;
// END package
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/** Mailer. No relation to Norman. Simply sends an email message.
* Example usage:
*
* Mailer mb = new Mailer();
* mb.setFrom("[email protected]");
* mb.addTo("[email protected]");
* mb.setSubject("LHBOOKS ORDER!!");
* mb.setBody(order.toString());
* mb.setServer(application.getInitParameter("mail.server.smtp"));
* try {
* mb.doSend();
* } catch (MessagingException ex) {
* ...
* }
*
* @author Ian F. Darwin
*/
// BEGIN main
public class Mailer {
/** The javamail session object. */
protected Session session;
/** The sender's email address */
protected String from;
/** The subject of the message. */
protected String subject;
/** The recipient ("To:"), as Strings. */
protected List toList = new ArrayList<>();
/** The CC list, as Strings. */
protected List ccList = new ArrayList();
/** The BCC list, as Strings. */
protected List bccList = new ArrayList();
/** The text of the message. */
protected String body;
/** The SMTP relay host */
protected String mailHost;
/** The verbosity setting */
protected boolean verbose;
public String getFrom() {
return from;
}
public void setFrom(String fm) {
from = fm;
}
public String getSubject() {
return subject;
}
public void setSubject(String subj) {
subject = subj;
}
// SETTERS/GETTERS FOR TO: LIST
/** Get tolist, as an array of Strings.
* @return The list of recipients
*/
public List getToList() {
return toList;
}
/** Set to list to an ArrayList of Strings
* @param to The list of recipients
*/
public void setToList(ArrayList to) {
toList = to;
}
/** Set to as a string like "tom, mary, robin@host". Loses any
* previously set values.
* @param to The list of recipients
*/
public void setToList(String to) {
toList = Arrays.asList(to.split(",\\s+"));
}
/** Add one "to" recipient.
* @param to The recipient to add
*/
public void addTo(String to) {
toList.add(to);
}
// SETTERS/GETTERS FOR CC: LIST
/** @return cclist, as an array of Strings */
public List getCcList() {
return ccList;
}
/** @param cc list to an ArrayList of Strings */
public void setCcList(ArrayList cc) {
ccList = cc;
}
/** @param cc as a string like "tom, mary, robin@host". Loses any
* previously set values. */
public void setCcList(String cc) {
ccList = Arrays.asList(cc.split(",\\s+"));
}
/** @param cc one recipient */
public void addCc(String cc) {
ccList.add(cc);
}
// SETTERS/GETTERS FOR BCC: LIST
/** @return bcclist, as an array of Strings */
public List getBccList() {
return bccList;
}
/** @param bcc list to an ArrayList of Strings */
public void setBccList(List bcc) {
bccList = bcc;
}
/** Set bcc as a string like "tom, mary, robin@host". Loses any
* previously set values.
* @param s The list of bcc's
*/
public void setBccList(String s) {
bccList = Arrays.asList(s.split(",\\s+"));
}
/** @param bcc one "bcc" recipient to be added */
public void addBcc(String bcc) {
bccList.add(bcc);
}
// SETTER/GETTER FOR MESSAGE BODY
public String getBody() {
return body;
}
public void setBody(String text) {
body = text;
}
// SETTER/GETTER FOR VERBOSITY
public boolean isVerbose() {
return verbose;
}
public void setVerbose(boolean v) {
verbose = v;
}
/** Check if all required fields have been set before sending.
* Normally called before doSend; called by doSend for verification.
* @return True if message is complete enough to send
*/
public boolean isComplete() {
if (from == null || from.length()==0) {
System.err.println("doSend: no FROM");
return false;
}
if (subject == null || subject.length()==0) {
System.err.println("doSend: no SUBJECT");
return false;
}
if (toList.size()==0) {
System.err.println("doSend: no recipients");
return false;
}
if (body == null || body.length()==0) {
System.err.println("doSend: no body");
return false;
}
if (mailHost == null || mailHost.length()==0) {
System.err.println("doSend: no server host");
return false;
}
return true;
}
public void setServer(String s) {
mailHost = s;
}
/** Send the message.
* @throws MessagingException if the message cannot be sent
*/
public synchronized void doSend() throws MessagingException {
if (!isComplete())
throw new IllegalArgumentException(
"doSend called before message was complete");
/** Properties object used to pass props into the MAIL API */
Properties props = new Properties();
props.put("mail.smtp.host", mailHost);
// Create the Session object
if (session == null) {
session = Session.getDefaultInstance(props, null);
if (verbose)
session.setDebug(true); // Verbose!
}
// create a message
final Message mesg = new MimeMessage(session);
InternetAddress[] addresses;
// TO Address list
addresses = new InternetAddress[toList.size()];
for (int i=0; i