Home
 

public class SmtpMail

public class .SmtpMail

This class implements the SMTP protocol

Public Methods

[more] SmtpMail()
Create new Object
[more]void debugMode(boolean debug)
Sets the debug mode
[more]void simpleSend(String serverName, int port, MailAddress sender, MailAddress receiver, String subject, String msg) throws SmtpException,UnknownHostException,IOException
Minimalistic send method


Protected Methods

[more]void trace(String t)
Outputs the String to stderr if debug is turned on
[more]void send() throws SmtpException,IOException
Sends a message to the SMTP server
[more]void sendCommand(PrintWriter out, String cmd)
Sends a single command to the SMTP server
[more]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
[more]void sendData(PrintWriter out)
Sends the data to the SMTP server
[more]String msg2data(String msg)
Converts a message to the data format
[more]int getResponse(BufferedReader in) throws IOException
Gets a response from the SMTP server


Private Fields

[more]InetAddress serverAddress
The internet address of the SMTP server
[more]int serverPort
The port on the host the SMTP server is running
[more]MailAddress sender
Sender address
[more]Vector rcpts
Receivers
[more]String subject
Mail subject
[more]String message
The message
[more]String lastResponseText
Text of the last received response of server
[more]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.
oSmtpMail()
Create new Object.

ovoid debugMode(boolean debug)
Sets the debug mode.
Parameters:
debug - true for debug mode, otherwise false (default).

ovoid trace(String t)
Outputs the String to stderr if debug is turned on.
Parameters:
t - String to trace.

ovoid 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

ovoid send() throws SmtpException,IOException
Sends a message to the SMTP server.

Sending is done via the following sequence:

  1. Connect to server
  2. HELO
  3. MAIL FROM
  4. RCPT TO
  5. DATA
  6. ...data...
  7. 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

ovoid 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>.

ovoid 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

ovoid 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

oString 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

oint 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

oInetAddress serverAddress
The internet address of the SMTP server

oint serverPort
The port on the host the SMTP server is running.

oMailAddress sender
Sender address.

oVector rcpts
Receivers.

oString subject
Mail subject.

oString message
The message. The expected message format is: lines separated by newline '\n' characters.

oString lastResponseText
Text of the last received response of server.

oboolean 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++