public class MailMessage extends Object
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.
It can be used like this:
String mailhost = "localhost"; // or another mail host String from = "Mail Message Servlet"; String to = "to@somedomain.com"; String cc1 = "cc1@somedomain.com"; String cc2 = "cc2@somedomain.com"; String bcc = "bcc@somedomain.com"; MailMessage msg = new MailMessage(mailhost); msg.from(from); msg.to(to); msg.cc(cc1); msg.cc(cc2); msg.bcc(bcc); msg.setSubject("Test subject"); PrintStream out = msg.getPrintStream(); Enumeration myEnum = req.getParameterNames(); while (myEnum.hasMoreElements()) { String name = (String)myEnum.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:
Modifier and Type | Field and Description |
---|---|
(package private) Vector |
cc |
(package private) String |
from |
(package private) Hashtable |
headers |
(package private) String |
host |
(package private) BufferedReader |
in |
(package private) MailPrintStream |
out |
(package private) Socket |
socket |
(package private) Vector |
to |
Constructor and Description |
---|
MailMessage()
Constructs a new MailMessage to send an email.
|
MailMessage(String host)
Constructs a new MailMessage to send an email.
|
Modifier and Type | Method and Description |
---|---|
void |
bcc(String bcc)
Sets the bcc address.
|
void |
cc(String cc)
Sets the cc address.
|
(package private) void |
connect() |
(package private) void |
disconnect() |
(package private) void |
flushHeaders() |
void |
from(String from)
Sets the from address.
|
PrintStream |
getPrintStream()
Returns a PrintStream that can be used to write the body of the message.
|
(package private) void |
getReady() |
(package private) boolean |
isResponseOK(String response,
int[] ok) |
(package private) static String |
sanitizeAddress(String s) |
(package private) void |
send(String msg,
int[] ok) |
void |
sendAndClose()
Sends the message and closes the connection to the server.
|
(package private) void |
sendData() |
(package private) void |
sendDot() |
(package private) void |
sendFrom(String from) |
(package private) void |
sendHelo() |
(package private) void |
sendQuit() |
(package private) void |
sendRcpt(String rcpt) |
(package private) void |
setCcHeader() |
(package private) void |
setFromHeader() |
void |
setHeader(String name,
String value)
Sets the named header to the given value.
|
void |
setSubject(String subj)
Sets the subject of the mail message.
|
(package private) void |
setToHeader() |
void |
to(String to)
Sets the to address.
|
(package private) String |
vectorToList(Vector v) |
MailPrintStream out
BufferedReader in
public MailMessage() throws IOException
IOException
- if there's any problem contacting the mail serverpublic MailMessage(String host) throws IOException
host
- the mail server to useIOException
- if there's any problem contacting the mail serverpublic void from(String from) throws IOException
IOException
- if there's any problem reported by the mail serverpublic void to(String to) throws IOException
IOException
- if there's any problem reported by the mail serverpublic void cc(String cc) throws IOException
IOException
- if there's any problem reported by the mail serverpublic void bcc(String bcc) throws IOException
IOException
- if there's any problem reported by the mail serverpublic void setSubject(String subj)
public void setHeader(String name, String value)
public PrintStream getPrintStream() throws IOException
IOException
- if there's any problem reported by the mail servervoid setFromHeader()
void setToHeader()
void setCcHeader()
String vectorToList(Vector v)
void flushHeaders() throws IOException
IOException
public void sendAndClose() throws IOException
IOException
- if there's any problem reported by the mail serverstatic String sanitizeAddress(String s)
void connect() throws IOException
IOException
void getReady() throws IOException
IOException
void sendHelo() throws IOException
IOException
void sendFrom(String from) throws IOException
IOException
void sendRcpt(String rcpt) throws IOException
IOException
void sendData() throws IOException
IOException
void sendDot() throws IOException
IOException
void sendQuit() throws IOException
IOException
void send(String msg, int[] ok) throws IOException
IOException
boolean isResponseOK(String response, int[] ok)
void disconnect() throws IOException
IOException
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses