|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codehaus.plexus.mailsender.simple.SimpleMailMessage
public class SimpleMailMessage
A class to help send SMTP email. This class is an improvement on the sun.net.smtp.SmtpClient class found in the JDK. This version has extra functionality, and can be used with JVMs that did not extend from the JDK. It's not as robust as the JavaMail Standard Extension classes, but it's easier to use and easier to install, and has an Open Source license.
It can be used like this:
String mailhost = "localhost"; // or another mail host String from = "Mail Message Servlet <MailMessage@server.com>"; String to = "to@you.com"; String cc1 = "cc1@you.com"; String cc2 = "cc2@you.com"; String bcc = "bcc@you.com"; SimpleMailMessage msg = new SimpleMailMessage(mailhost); msg.setPort(25); msg.from(from); msg.to(to); msg.cc(cc1); msg.cc(cc2); msg.bcc(bcc); msg.setSubject("Test subject"); PrintStream out = msg.getPrintStream(); Enumeration enum = req.getParameterNames(); while (enum.hasMoreElements()) { String name = (String)enum.nextElement(); String value = req.getParameter(name); out.println(name + " = " + value); } msg.sendAndClose();
Be sure to set the from address, then set the recepient addresses, then set the subject and other headers, then get the PrintStream, then write the message, and finally send and close. The class does minimal error checking internally; it counts on the mail host to complain if there's any malformatted input or out of order execution.
An attachment mechanism based on RFC 1521 could be implemented on top of this class. In the meanwhile, JavaMail is the best solution for sending email with attachments.
Still to do:
Nested Class Summary | |
---|---|
private static class |
SimpleMailMessage.MailPrintStream
This PrintStream subclass makes sure that |
Field Summary | |
---|---|
private java.util.Vector |
cc
list of email addresses to cc to |
static int |
DEFAULT_PORT
default port for SMTP: 25 |
private java.lang.String |
from
sender email address |
private java.util.Hashtable |
headers
headers to send in the mail |
private java.lang.String |
host
host name for the mail server |
private SmtpResponseReader |
in
|
private SimpleMailMessage.MailPrintStream |
out
|
private int |
port
host port for the mail server |
private java.net.Socket |
socket
|
private java.util.Vector |
to
list of email addresses to send to |
Constructor Summary | |
---|---|
SimpleMailMessage()
Constructs a new SimpleMailMessage to send an email. |
|
SimpleMailMessage(java.lang.String host)
Constructs a new SimpleMailMessage to send an email. |
|
SimpleMailMessage(java.lang.String host,
int port)
Constructs a new SimpleMailMessage to send an email. |
Method Summary | |
---|---|
void |
bcc(java.lang.String bcc)
Sets the bcc address. |
void |
cc(java.lang.String cc)
Sets the cc address. |
private void |
connect()
|
private void |
disconnect()
|
private void |
flushHeaders()
|
void |
from(java.lang.String from)
Sets the from address. |
java.io.PrintStream |
getPrintStream()
Returns a PrintStream that can be used to write the body of the message. |
private void |
getReady()
|
private boolean |
isResponseOK(java.lang.String response,
int[] ok)
|
private static java.lang.String |
sanitizeAddress(java.lang.String s)
|
private void |
send(java.lang.String msg,
int[] ok)
|
void |
sendAndClose()
Sends the message and closes the connection to the server. |
private void |
sendData()
|
private void |
sendDot()
|
private void |
sendFrom(java.lang.String from)
|
private void |
sendHelo()
|
private void |
sendQuit()
|
private void |
sendRcpt(java.lang.String rcpt)
|
private void |
setCcHeader()
|
private void |
setFromHeader()
|
void |
setHeader(java.lang.String name,
java.lang.String value)
Sets the named header to the given value. |
void |
setPort(int port)
Set the port to connect to the SMTP host. |
void |
setSubject(java.lang.String subj)
Sets the subject of the mail message. |
private void |
setToHeader()
|
void |
to(java.lang.String to)
Sets the to address. |
private java.lang.String |
vectorToList(java.util.Vector v)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int DEFAULT_PORT
private java.lang.String host
private int port
private java.lang.String from
private java.util.Vector to
private java.util.Vector cc
private java.util.Hashtable headers
private SimpleMailMessage.MailPrintStream out
private SmtpResponseReader in
private java.net.Socket socket
Constructor Detail |
---|
public SimpleMailMessage() throws java.io.IOException
java.io.IOException
- if there's any problem contacting the mail serverpublic SimpleMailMessage(java.lang.String host) throws java.io.IOException
host
- the mail server to use
java.io.IOException
- if there's any problem contacting the mail serverpublic SimpleMailMessage(java.lang.String host, int port) throws java.io.IOException
host
- the mail server to useport
- the port to connect to
java.io.IOException
- if there's any problem contacting the mail serverMethod Detail |
---|
public void setPort(int port)
port
- the port to use for connection.DEFAULT_PORT
public void from(java.lang.String from) throws java.io.IOException
from
- The value of the From:
field
java.io.IOException
- if there's any problem reported by the mail serverpublic void to(java.lang.String to) throws java.io.IOException
java.io.IOException
- if there's any problem reported by the mail serverpublic void cc(java.lang.String cc) throws java.io.IOException
java.io.IOException
- if there's any problem reported by the mail serverpublic void bcc(java.lang.String bcc) throws java.io.IOException
java.io.IOException
- if there's any problem reported by the mail serverpublic void setSubject(java.lang.String subj)
public void setHeader(java.lang.String name, java.lang.String value)
public java.io.PrintStream getPrintStream() throws java.io.IOException
java.io.IOException
- if there's any problem reported by the mail serverprivate void setFromHeader()
private void setToHeader()
private void setCcHeader()
private java.lang.String vectorToList(java.util.Vector v)
private void flushHeaders() throws java.io.IOException
java.io.IOException
public void sendAndClose() throws java.io.IOException
java.io.IOException
- if there's any problem reported by the mail serverprivate static java.lang.String sanitizeAddress(java.lang.String s)
private void connect() throws java.io.IOException
java.io.IOException
private void getReady() throws java.io.IOException
java.io.IOException
private void sendHelo() throws java.io.IOException
java.io.IOException
private void sendFrom(java.lang.String from) throws java.io.IOException
java.io.IOException
private void sendRcpt(java.lang.String rcpt) throws java.io.IOException
java.io.IOException
private void sendData() throws java.io.IOException
java.io.IOException
private void sendDot() throws java.io.IOException
java.io.IOException
private void sendQuit() throws java.io.IOException
java.io.IOException
private void send(java.lang.String msg, int[] ok) throws java.io.IOException
java.io.IOException
private boolean isResponseOK(java.lang.String response, int[] ok)
private void disconnect() throws java.io.IOException
java.io.IOException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |