Additional top-level class in compilation unit: MailPrintStream.
It can be used like this:
String mailhost = "localhost"; // or another mail host String from = "Mail Message Servlet <[email protected]>"; String to = "[email protected]"; String cc1 = "[email protected]"; String cc2 = "[email protected]"; String bcc = "[email protected]"; MailMessage msg = new MailMessage(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 recipient 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:
| Modifier and Type | Field and Description |
|---|---|
| private final Vector | cc
list of email addresses to cc to |
| public static final String | DEFAULT_HOST
default mailhost |
| public static final int | DEFAULT_PORT
default port for SMTP: 25 |
| private String | from
sender email address |
| private final Map | headers
headers to send in the mail |
| private String | host
host name for the mail server |
| private SmtpResponseReader | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private MailPrintStream | |
| private int | port
host port for the mail server |
| private final Vector | replyto
list of email addresses to reply to |
| private Socket | |
| private final Vector | to
list of email addresses to send to |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public | MailMessage(String
the mail server to use host, int the port to connect to port)Constructs a new MailMessage to send an email. |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| public void | |
| public PrintStream | Returns: a printstream containing the data and the headers of the emailReturns a PrintStream that can be used to write the body of the message. |
| pack-priv void | |
| pack-priv boolean | |
| public void | |
| pack-priv static String | |
| pack-priv void | |
| public void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| public void | |
| public void | |
| pack-priv void | |
| public void | |
| pack-priv void | |
| public void | |
| pack-priv String |
| cc | back to summary |
|---|---|
| private final Vector<String> cc list of email addresses to cc to | |
| DEFAULT_HOST | back to summary |
|---|---|
| public static final String DEFAULT_HOST default mailhost | |
| DEFAULT_PORT | back to summary |
|---|---|
| public static final int DEFAULT_PORT default port for SMTP: 25 | |
| from | back to summary |
|---|---|
| private String from sender email address | |
| headers | back to summary |
|---|---|
| private final Map<String, String> headers headers to send in the mail | |
| host | back to summary |
|---|---|
| private String host host name for the mail server | |
| in | back to summary |
|---|---|
| private SmtpResponseReader in | |
| OK_DATA | back to summary |
|---|---|
| private static final int OK_DATA | |
| OK_DOT | back to summary |
|---|---|
| private static final int OK_DOT | |
| OK_FROM | back to summary |
|---|---|
| private static final int OK_FROM | |
| OK_HELO | back to summary |
|---|---|
| private static final int OK_HELO | |
| OK_QUIT | back to summary |
|---|---|
| private static final int OK_QUIT | |
| OK_RCPT_1 | back to summary |
|---|---|
| private static final int OK_RCPT_1 | |
| OK_RCPT_2 | back to summary |
|---|---|
| private static final int OK_RCPT_2 | |
| OK_READY | back to summary |
|---|---|
| private static final int OK_READY | |
| out | back to summary |
|---|---|
| private MailPrintStream out | |
| port | back to summary |
|---|---|
| private int port host port for the mail server | |
| replyto | back to summary |
|---|---|
| private final Vector<String> replyto list of email addresses to reply to | |
| socket | back to summary |
|---|---|
| private Socket socket | |
| to | back to summary |
|---|---|
| private final Vector<String> to list of email addresses to send to | |
| MailMessage | back to summary |
|---|---|
| public MailMessage() throws IOException Constructs a new MailMessage to send an email. Use localhost as the mail server with port 25.
| |
| MailMessage | back to summary |
|---|---|
| public MailMessage(String host) throws IOException Constructs a new MailMessage to send an email. Use the given host as the mail server with port 25.
| |
| MailMessage | back to summary |
|---|---|
| public MailMessage(String host, int port) throws IOException Constructs a new MailMessage to send an email. Use the given host and port as the mail server.
| |
| bcc | back to summary |
|---|---|
| public void bcc(String bcc) throws IOException Sets the bcc address. Does NOT set any header since it's a *blind* copy. This method may be called multiple times.
| |
| cc | back to summary |
|---|---|
| public void cc(String cc) throws IOException Sets the cc address. Also sets the "Cc" header. This method may be called multiple times.
| |
| connect | back to summary |
|---|---|
| pack-priv void connect() throws IOException | |
| disconnect | back to summary |
|---|---|
| pack-priv void disconnect() throws IOException | |
| flushHeaders | back to summary |
|---|---|
| pack-priv void flushHeaders() throws IOException | |
| from | back to summary |
|---|---|
| public void from(String from) throws IOException Sets the from address. Also sets the "From" header. This method should be called only once.
| |
| getPrintStream | back to summary |
|---|---|
| public PrintStream getPrintStream() throws IOException Returns a PrintStream that can be used to write the body of the message. A stream is used since email bodies are byte-oriented. A writer can be wrapped on top if necessary for internationalization. This is actually done in Message.java
| |
| getReady | back to summary |
|---|---|
| pack-priv void getReady() throws IOException | |
| isResponseOK | back to summary |
|---|---|
| pack-priv boolean isResponseOK(String response, int[] ok) | |
| replyto | back to summary |
|---|---|
| public void replyto(String rto) Sets the replyto address This method may be called multiple times.
| |
| sanitizeAddress | back to summary |
|---|---|
| pack-priv static String sanitizeAddress(String s) | |
| send | back to summary |
|---|---|
| pack-priv void send(String msg, int[] ok) throws IOException | |
| sendAndClose | back to summary |
|---|---|
| public void sendAndClose() throws IOException Sends the message and closes the connection to the server. The MailMessage object cannot be reused.
| |
| sendData | back to summary |
|---|---|
| pack-priv void sendData() throws IOException | |
| sendDot | back to summary |
|---|---|
| pack-priv void sendDot() throws IOException | |
| sendFrom | back to summary |
|---|---|
| pack-priv void sendFrom(String from) throws IOException | |
| sendHelo | back to summary |
|---|---|
| pack-priv void sendHelo() throws IOException | |
| sendQuit | back to summary |
|---|---|
| pack-priv void sendQuit() throws IOException | |
| sendRcpt | back to summary |
|---|---|
| pack-priv void sendRcpt(String rcpt) throws IOException | |
| setCcHeader | back to summary |
|---|---|
| pack-priv void setCcHeader() | |
| setFromHeader | back to summary |
|---|---|
| pack-priv void setFromHeader() | |
| setHeader | back to summary |
|---|---|
| public void setHeader(String name, String value) Sets the named header to the given value. RFC 822 provides the rules for what text may constitue a header name and value. | |
| setPort | back to summary |
|---|---|
| public void setPort(int port) Set the port to connect to the SMTP host.
| |
| setReplyToHeader | back to summary |
|---|---|
| pack-priv void setReplyToHeader() | |
| setSubject | back to summary |
|---|---|
| public void setSubject(String subj) Sets the subject of the mail message. Actually sets the "Subject" header.
| |
| setToHeader | back to summary |
|---|---|
| pack-priv void setToHeader() | |
| to | back to summary |
|---|---|
| public void to(String to) throws IOException Sets the to address. Also sets the "To" header. This method may be called multiple times.
| |
| vectorToList | back to summary |
|---|---|
| pack-priv String vectorToList(Vector<String> v) | |