com.lamatek.protobeans
Class SMTPBean

java.lang.Object
  extended by com.lamatek.protobeans.SMTPBean
All Implemented Interfaces:
java.io.Serializable

public class SMTPBean
extends java.lang.Object
implements java.io.Serializable

This bean provides a simple, easy-to-use SMTP interface for use in sending standard emails (without attachments) from Java applets.

The entire process of sending an email can be completed with just a few lines of code within a single try/catch block.

There are several ways to utilize the SMTPBean.

This first method does the whole email transmission with one line of code. After the email is sent, the connection is closed. Additional emails must be sent with a new SMTPBean:
(All examples assume useage from an Applet.)

try {
  new SMTPBean(getCodeBase().getHost(), 25, "myemail@adress.com", "receipent@address.com", "Message Subject", "This is the actual message");
}
catch(SMTPException ex) {
  ex.printStackTrace();
}
This example creates an SMTPBean (initially connected and hailed) that sends multiple emails to the same recipient:
try {
  SMTPBean bean = new SMTPBean(getCodeBase().getHost(), 25, "my@address.com", "recipient@address.com");
  bean.sendMessage("my@address.com", "recipient@address.com", "Subject for first message", "This is message one.");
  bean.sendMessage("my@address.com", "recipient@address.com", "Subject for second message", "This is message two.");
  bean.logout();
}
catch(SMTPException ex) {
  ex.printStackTrace();
}
This example creates an SMTPBean (initially connected) that performs multiple hail and sendMessage calls to send email to many different recipients using the same connection.
try {
  SMTPBean bean = new SMTPBean(getCodeBase().getHost(), 25);
  bean.hailAndSendMessage("my@address.com", "recipient@address.com", "Subject Header", "This is the message.");
  bean.hailAndSendMessage("my@address.com", "next.recipient@address.com", "Subject Header", "This is the message.");
  bean.logout(); }
catch(SMTPException ex) {
  ex.printStackTrace();
}
This method creates a new SMTPBean (not connected) that sends email to one or more users.
try {   SMTPBean smtp = new SMTPBean();
  smtp.connect(getCodeBase().getHost(), 25);
  smtp.hail("my@address.com", "recipient@address.com");
  smtp.sendMessage("my@address.com", "recipient@address.com", "Email Subject", "This is the message.");
  smtp.logout();
}
catch(SMTPException e) {
  System.out.println(e.getMessage());
}

See Also:
Serialized Form

Constructor Summary
SMTPBean()
          Creates an initially disconnected SMTPBean.
SMTPBean(java.lang.String server, int port)
          Creates a SMTPBean that attempts to establish the Socket connection to the SMTP server.
SMTPBean(java.lang.String server, int port, java.lang.String mailfrom, java.lang.String mailto)
          Creates a SMTPBean that attempts to establish the Socket connection to the SMTP server and sends the hailing commands HELP, MAIL and RCPT.
SMTPBean(java.lang.String server, int port, java.lang.String mailfrom, java.lang.String mailto, java.lang.String subject, java.lang.String message)
          Creates a SMTPBean that attempts to establish the Socket connection to the SMTP server, sends the hailing commands HELP, MAIL and RCPT and sends the message all in one step.
 
Method Summary
 void connect(java.lang.String server, int port)
          This method establishes the header portion of the SMTP transaction, sending the HELP, MAIL and RCPT commands.
 java.lang.String getLastResponse()
          This method returns the last reponse received from the SMTP Server.
 void hail(java.lang.String mailfrom, java.lang.String mailto)
          This method makes the necessary SMTP hailing calls to the SMTP server, including HELO, MAIL and RCPT.
 void hailAndSendMessage(java.lang.String mailfrom, java.lang.String mailto, java.lang.String subject, java.lang.String message)
          This method performs the hail and sendMessage in one convenient method.
 void logout()
          This method closes all existing data streams and sockets to the SMTP server.
 void sendMessage(java.lang.String mailfrom, java.lang.String mailto, java.lang.String subject, java.lang.String message)
          This method performs the actual email transmission function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SMTPBean

public SMTPBean()
Creates an initially disconnected SMTPBean. Before email can be sent the connect, hail and sendMessage methods must be successfully called, in that order.


SMTPBean

public SMTPBean(java.lang.String server,
                int port)
         throws SMTPException
Creates a SMTPBean that attempts to establish the Socket connection to the SMTP server. Before email can be sent the hail and sendMessage methods must be successfully called, in that order.

Throws:
SMTPException - If an error is received while connecting to the SMTP server.

SMTPBean

public SMTPBean(java.lang.String server,
                int port,
                java.lang.String mailfrom,
                java.lang.String mailto)
         throws SMTPException
Creates a SMTPBean that attempts to establish the Socket connection to the SMTP server and sends the hailing commands HELP, MAIL and RCPT. To send a message call sendMessage.

Throws:
SMTPException - If an error code is received from the Server anytime during the connection and hailing phases.

SMTPBean

public SMTPBean(java.lang.String server,
                int port,
                java.lang.String mailfrom,
                java.lang.String mailto,
                java.lang.String subject,
                java.lang.String message)
         throws SMTPException
Creates a SMTPBean that attempts to establish the Socket connection to the SMTP server, sends the hailing commands HELP, MAIL and RCPT and sends the message all in one step.

NOTE: This constructor assumes that only one message is to be sent and closes the Socket connection once the message is transmitted by making a call to logout. This is the only constructor of SMTPBean that performs this method call.

Throws:
SMTPException - If an error code is received from the Server anytime during the connection, hailing or mail transmission phases.
Method Detail

connect

public void connect(java.lang.String server,
                    int port)
             throws SMTPException
This method establishes the header portion of the SMTP transaction, sending the HELP, MAIL and RCPT commands.

Throws:
SMTPException - if an error code is returned by the SMTP server.

hail

public void hail(java.lang.String mailfrom,
                 java.lang.String mailto)
          throws SMTPException
This method makes the necessary SMTP hailing calls to the SMTP server, including HELO, MAIL and RCPT. Upon a successful call to this method, only the sendMessage method is required to send mail.

Before a call to this method can be made, a successful call to either connect or the SMTPBean(String, int) constructor.

Throws:
SMTPException - If any error codes are returned during the hailing phase.

sendMessage

public void sendMessage(java.lang.String mailfrom,
                        java.lang.String mailto,
                        java.lang.String subject,
                        java.lang.String message)
                 throws SMTPException
This method performs the actual email transmission function.

Before a call to this method can be made, a successful call to connect and hail or the SMTPBean(String, int, String, String) constructor must be made.

Throws:
SMTPException - If an error message is received from the server during email transmission.

hailAndSendMessage

public void hailAndSendMessage(java.lang.String mailfrom,
                               java.lang.String mailto,
                               java.lang.String subject,
                               java.lang.String message)
                        throws SMTPException
This method performs the hail and sendMessage in one convenient method. This method is most useful when an SMTPBean is going to send multiple emails to different recipients.

Throws:
SMTPException - If an error code is received from the SMTP server at any time during the hailing or transmission phases.

getLastResponse

public java.lang.String getLastResponse()
This method returns the last reponse received from the SMTP Server. This method is useful after receipt of an SMTPException, to view the server's actual rejected response.

Returns:
The last reponse from the SMTP Server.

logout

public void logout()
            throws SMTPException
This method closes all existing data streams and sockets to the SMTP server. This method should be called by the SMTPBean once completed sending messages, unless the bean was created using the SMTPBean(String, int, String, String, String, String) constructor, since this construct performs the logout functions automatically.

Throws:
SMTPException