SMTPConn Class

This class implements the SMTP (Simple Mail Transfer Protocol). Specifically this class handles the connection to and communication with an SMTP server. Note that this class only implements basic functionality and at present does not support authentication using the AUTH method, MD5 based encryption or the sending of file attachments. These features will be added at a later date.

Note: This class specifically relies on the SMTPMsg class.

An example of how to use this class is shown at the bottom of this page.

Works with:

JDK 1.5 >

See Also:

RFC 2821 - Simple Mail Transfer Protocol
SMTPMsg Class

Download:

You can download the SMTPConn and SMTPMsg classes from the sample page.



Class Overview

Constructor Summary

SMTPConn()
Initializes the class with the server IP as 127.0.0.1 (localhost) and port 25.

SMTPConn(String IP, String Port, String User, String Pass)
Initializes the class and sets the server IP and port to the values passed. Note that although this constructor accepts a username and password, these have not yet been implemented.

Method Summary

String getServerIP()
Returns the IP address or hostname that the sendMail() method will attempt to connect to. Default is 127.0.0.1 (localhost).

int getServerPort()
Returns the port number that the sendMail() method will attempt to connect to. Default is 25.

int sendMail(SMTPMsg Message, Boolean verbose)
Connects to the SMTP server and attempts to send the e-mail message passed. Returns 0 on success or the SMTP server response.

void setServerIP(String newIP)
Sets the server IP or hostname to the value passed in newIP.

void setServerPort(int newPort)
Sets the server port to the value passed in newPort.

void showSettings()
Prints the current IP and port settings to the console.



Constructor Detail

SMTPConn

public SMTPConn()

Initializes the class with the server IP as 127.0.0.1 (localhost) and port 25.

SMTPConn

public SMTPConn(String IP, String Port, String User, String Pass)

Initializes the class and sets the server IP and port to the values passed. Note that although this constructor accepts a username and password, these have not yet been implemented.

Parameters:

  • IP - The hostname or IP address of a SMTP mail server.
  • Port - The port number to connect on.
  • User - Not implemented - The username to pass to the server for authentication.
  • Pass - Not implemented - The password to pass to the server for authentication.

Throws:



Method Detail

getServerIP

public String getServerIP()

Returns the IP address or hostname specified by setServerIP() or by the SMTPConn constructor. If no hostname or IP address has been specified the default of 127.0.0.1 will be used.

getServerPort

public int getServerIP()

Returns the port specified by setServerPort() or by the SMTPConn constructor. If no port has been specified the default of 25 will be used.

sendMail

public int sendMail(SMTPMsg Message, Boolean verbose)

Connects to the SMTP server and attempts to send the e-mail message passed in Message returning 0 on success or the SMTP server response on failure.

Parameters:

  • Message - The e-mail message to send. This should be of the type SMTPMsg.
  • verbose - Specifies whether the method should print status output to the console while sending the message. When set to true, the server responses will be printed to the console and any errors highlighted.

Returns:

'0' if the message is sent successfully or '-1' if a connection error occurs. If the SMTP server reports an error then the last error code sent by the server is returned. See RFC 2821 for a full list.

setServerIP

public void setServerIP(String newIP)

Sets the server IP or hostname to the value passed in newIP.

Parameters:

  • newIP - The new IP/hostname to use.

setServerPort

public void setServerPort(int newPort)

Sets the server port to the value passed in newPort.

Parameters:

  • newPort - The port number to use, must be between 0 and 65534.

showSettings

public void showSettings()

Prints the current IP address and port number to the console.



Example & Download

You can download the SMTPConn and SMTPMsg classes along with a sample application on the SMTPConn & SMTPMsg Classes page.