All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.darwinsys.mail.Mailer Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
package com.darwinsys.mail;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

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. Sends an email message.
 * My old Sender class, recast as a Bean for use in JSP's and elsewhere.
 * 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 // package com.darwinsys.mail; 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; /** Get from */ public String getFrom() { return from; } /** Set from */ public void setFrom(String fm) { from = fm; } /** Get subject */ public String getSubject() { return subject; } /** Set subject */ public void setSubject(String subj) { subject = subj; } // SETTERS/GETTERS FOR TO: LIST /** Get tolist, as an array of Strings */ public List getToList() { return toList; } /** Set to list to an ArrayList of Strings */ public void setToList(ArrayList to) { toList = to; } /** Set to as a string like "tom, mary, robin@host". Loses any * previously-set values. */ public void setToList(String s) { toList = tokenize(s); } /** Add one "to" recipient */ public void addTo(String to) { toList.add(to); } // SETTERS/GETTERS FOR CC: LIST /** Get cclist, as an array of Strings */ public List getCcList() { return ccList; } /** Set cc list to an ArrayList of Strings */ public void setCcList(ArrayList cc) { ccList = cc; } /** Set cc as a string like "tom, mary, robin@host". Loses any * previously-set values. */ public void setCcList(String s) { ccList = tokenize(s); } /** Add one "cc" recipient */ public void addCc(String cc) { ccList.add(cc); } // SETTERS/GETTERS FOR BCC: LIST /** Get bcclist, as an array of Strings */ public List getBccList() { return bccList; } /** Set 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. */ public void setBccList(String s) { bccList = tokenize(s); } /** Add one "bcc" recipient */ public void addBcc(String bcc) { bccList.add(bcc); } // SETTER/GETTER FOR MESSAGE BODY /** Get message */ public String getBody() { return body; } /** Set message */ public void setBody(String text) { body = text; } // SETTER/GETTER FOR VERBOSITY /** Get verbose */ public boolean isVerbose() { return verbose; } /** Set verbose */ public void setVerbose(boolean v) { verbose = v; } /** Check if all required fields have been set before sending. * Normally called e.g., by a JSP before calling doSend. * Is also called by doSend for verification. */ 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. */ 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 tokenize(String addrs) { List al = new ArrayList(); if (addrs != null) { StringTokenizer tf = new StringTokenizer(addrs, ","); // For each word found in the line while (tf.hasMoreTokens()) { // trim blanks, and add to list. al.add(tf.nextToken().trim()); } } return al; } } // END main




© 2015 - 2024 Weber Informatics LLC | Privacy Policy