Fields Summary |
---|
protected Session | sessionThe javamail session object. |
protected String | fromThe sender's email address |
protected String | subjectThe subject of the message. |
protected ArrayList | toListThe recipient ("To:"), as Strings. |
protected ArrayList | ccListThe CC list, as Strings. |
protected ArrayList | bccListThe BCC list, as Strings. |
protected String | bodyThe text of the message. |
protected String | mailHostThe SMTP relay host |
protected boolean | verboseThe verbosity setting |
Methods Summary |
---|
public void | addBcc(java.lang.String bcc)Add one "bcc" recipient
bccList.add(bcc);
|
public void | addCc(java.lang.String cc)Add one "cc" recipient
ccList.add(cc);
|
public void | addTo(java.lang.String to)Add one "to" recipient
toList.add(to);
|
public synchronized void | doSend()Send the message.
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<addresses.length; i++)
addresses[i] = new InternetAddress((String)toList.get(i));
mesg.setRecipients(Message.RecipientType.TO, addresses);
// From Address
mesg.setFrom(new InternetAddress(from));
// CC Address list
addresses = new InternetAddress[ccList.size()];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)ccList.get(i));
mesg.setRecipients(Message.RecipientType.CC, addresses);
// BCC Address list
addresses = new InternetAddress[bccList.size()];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)bccList.get(i));
mesg.setRecipients(Message.RecipientType.BCC, addresses);
// The Subject
mesg.setSubject(subject);
// Now the message body.
mesg.setText(body);
// Finally, send the message! (use static Transport method)
// Do this in a Thread as it sometimes is too slow for JServ
// new Thread() {
// public void run() {
// try {
Transport.send(mesg);
// } catch (MessagingException e) {
// throw new IllegalArgumentException(
// "Transport.send() threw: " + e.toString());
// }
// }
// }.start();
|
public java.util.ArrayList | getBccList()Get bcclist, as an array of Strings
return bccList;
|
public java.lang.String | getBody()Get message
return body;
|
public java.util.ArrayList | getCcList()Get cclist, as an array of Strings
return ccList;
|
public java.lang.String | getFrom()Get from
return from;
|
public java.lang.String | getSubject()Get subject
return subject;
|
public java.util.ArrayList | getToList()Get tolist, as an array of Strings
return toList;
|
public boolean | isComplete()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.
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 boolean | isVerbose()Get verbose
return verbose;
|
public static void | send(java.lang.String mailhost, java.lang.String recipient, java.lang.String sender, java.lang.String subject, java.lang.String message)Convenience method that does it all with one call.
Mailer m = new Mailer();
m.setServer(mailhost);
m.addTo(recipient);
m.setFrom(sender);
m.setSubject(subject);
m.setBody(message);
m.doSend();
|
public void | setBccList(java.util.ArrayList bcc)Set bcc list to an ArrayList of Strings
bccList = bcc;
|
public void | setBccList(java.lang.String s)Set bcc as a string like "tom, mary, robin@host". Loses any
previously-set values.
bccList = tokenize(s);
|
public void | setBody(java.lang.String text)Set message
body = text;
|
public void | setCcList(java.util.ArrayList cc)Set cc list to an ArrayList of Strings
ccList = cc;
|
public void | setCcList(java.lang.String s)Set cc as a string like "tom, mary, robin@host". Loses any
previously-set values.
ccList = tokenize(s);
|
public void | setFrom(java.lang.String fm)Set from
from = fm;
|
public void | setServer(java.lang.String s)
mailHost = s;
|
public void | setSubject(java.lang.String subj)Set subject
subject = subj;
|
public void | setToList(java.util.ArrayList to)Set to list to an ArrayList of Strings
toList = to;
|
public void | setToList(java.lang.String s)Set to as a string like "tom, mary, robin@host". Loses any
previously-set values.
toList = tokenize(s);
|
public void | setVerbose(boolean v)Set verbose
verbose = v;
|
protected java.util.ArrayList | tokenize(java.lang.String s)Convert a list of addresses to an ArrayList. This will work
for simple names like "tom, mary@foo.com, 123.45@c$.com"
but will fail on certain complex (but RFC-valid) names like
"(Darwin, Ian) ".
Or even "Ian Darwin ".
ArrayList al = new ArrayList();
StringTokenizer tf = new StringTokenizer(s, ",");
// For each word found in the line
while (tf.hasMoreTokens()) {
// trim blanks, and add to list.
al.add(tf.nextToken().trim());
}
return al;
|