public class SmtpMail
public class .SmtpMail
This class implements the SMTP protocol
-
-
Public Methods
- SmtpMail()
- Create new Object
- void debugMode(boolean debug)
- Sets the debug mode
- void simpleSend(String serverName, int port, MailAddress sender, MailAddress receiver, String subject, String msg) throws SmtpException,UnknownHostException,IOException
- Minimalistic send method
-
Protected Methods
- void trace(String t)
- Outputs the String to stderr if debug is turned on
- void send() throws SmtpException,IOException
- Sends a message to the SMTP server
- void sendCommand(PrintWriter out, String cmd)
- Sends a single command to the SMTP server
- void sendCommand(PrintWriter out, String cmd, BufferedReader in, int OkClass) throws SmtpException,IOException
- Sends a command and throws a SMTPException if the returncode is not in the expected class, ie the first digit of the returncode matches
- void sendData(PrintWriter out)
- Sends the data to the SMTP server
- String msg2data(String msg)
- Converts a message to the data format
- int getResponse(BufferedReader in) throws IOException
- Gets a response from the SMTP server
-
Private Fields
- InetAddress serverAddress
- The internet address of the SMTP server
- int serverPort
- The port on the host the SMTP server is running
- MailAddress sender
- Sender address
- Vector rcpts
- Receivers
- String subject
- Mail subject
- String message
- The message
- String lastResponseText
- Text of the last received response of server
- boolean debug
- Debugging?
-
Documentation
This class implements the SMTP protocol. This implementation is very limited. The only way to send is simpleSend; see method description for details.The expected message format is: lines separated by newline '\n' characters.You may turn on the debug flag via debugMode(true); debug output goes to stderr.
- SmtpMail()
- Create new Object.
- void debugMode(boolean debug)
- Sets the debug mode.
- Parameters:
-
debug - true for debug mode, otherwise false (default).
- void trace(String t)
- Outputs the String to stderr if debug is turned on.
- Parameters:
-
t - String to trace.
- void simpleSend(String serverName, int port, MailAddress sender, MailAddress receiver, String subject, String msg) throws SmtpException,UnknownHostException,IOException
- Minimalistic send method.
- Throws:
-
UnknownHostException Unknown host
IOException Error while communicating
SmtpException Error returned by SMTP server
- Parameters:
-
serverName - Address of SMTP server (numerical or by name)
port - SMTP port number (should be 25)
sender - Address of mail sender
receiver - Address of mail receiver
subject - Subject of message
msg - The message; line seperator is '\n'.
- See Also:
-
send
- void send() throws SmtpException,IOException
- Sends a message to the SMTP server.
Sending is done via the following sequence:
- Connect to server
- HELO
- MAIL FROM
- RCPT TO
- DATA
- ...data...
- QUIT
The following limitations apply:
- No retry logic; all failures are considered as errors, although some may be recovered.
- Message is sent to the first receiver only.
- Header should contain more that From, To and Subject (eg DATE, MIME type).
- Throws:
-
IOException Error while communicating
SmtpException Error returned by SMTP server
- void sendCommand(PrintWriter out, String cmd)
- Sends a single command to the SMTP server.
- Parameters:
-
out - Outputstream to SMTP server
cmd - Command to send, without trailing <CR/LF>.
- void sendCommand(PrintWriter out, String cmd, BufferedReader in, int OkClass) throws SmtpException,IOException
- Sends a command and throws a SMTPException if the returncode is not in the expected class, ie the first digit of the returncode matches.
- Throws:
-
SmtpException Thrown if answer not in expected returncode class.
IOException Error while communicating
- Parameters:
-
out - Outputstream to SMTP server
cmd - Command to send, without trailing <CR/LF>.
in - Inputstream to SMTP server
OkClass - Class of returncodes that is ok
- void sendData(PrintWriter out)
- Sends the data to the SMTP server.
First a simple header, containing the From and To field is sent. The follows the Subject. After that we convert the message to the data format and send it. We finish the data with a <CR/LF>.<CR/LF> sequence.
- Parameters:
-
out - Outputstream to SMTP server
- String msg2data(String msg)
- Converts a message to the data format.
The expected message format is: lines separated by newline ('\n') characters. The SMTP data format is: lines separated by <CR/LF>; if a message line begins with a period another period is added in front of this line.
- Returns:
- The converted message
- Parameters:
-
msg - Message to convert
- int getResponse(BufferedReader in) throws IOException
- Gets a response from the SMTP server.
Each line begins with a returncode, followed by either a space or a dash ('-'). A dash indicates that there are following lines, a space that this is the last line. The rest of a line is addition text. Lines are separated by <CR/LF>.
- Throws:
-
IOException Error while reading from server
- Returns:
- Returncode from SMTP server
- Parameters:
-
in - Inputstream to SMTP server
- InetAddress serverAddress
- The internet address of the SMTP server
- int serverPort
- The port on the host the SMTP server is running.
- MailAddress sender
- Sender address.
- Vector rcpts
- Receivers.
- String subject
- Mail subject.
- String message
- The message. The expected message format is: lines separated by newline '\n' characters.
- String lastResponseText
- Text of the last received response of server.
- boolean debug
- Debugging?
- This class has no child classes.
- Author:
- Ronald Blaschke <rblasch@cs.bgsu.edu>
- Version:
- 1.0
- See Also:
- RFC821, RFC974, RFC822
Alphabetic index HTML hierarchy of classes or Java
DOC++