iaik.utils
Class SmtpMailer

java.lang.Object
  |
  +--iaik.utils.SmtpMailer

public class SmtpMailer
extends Object

This class implements a simple SMTP mailing tool. It can be used to send simple EMails over a SMTP mailserver. For further details check out RFC822 (STANDARD FOR THE FORMAT OF ARPA INTERNET TEXT MESSAGES) and RFC821 (SIMPLE MAIL TRANSFER PROTOCOL)

Version:
File Revision 12

Constructor Summary
SmtpMailer()
          Creates a new simple SmtpMailer.
SmtpMailer(String host)
          Creates a new simple SmtpMailer from a host name.
 
Method Summary
 void addAdditionalField(String line)
          Sets any additional field according to RFC822.
 void addBcc(String realName, String emailAddress)
          Sets the Bcc field.
 void addCc(String realName, String emailAddress)
          Sets the Cc field.
 void addText(String text)
          Adds text to message body of this EMail.
 void addTo(String realName, String emailAddress)
          Sets the To field.
protected  boolean checkResponse(int expected)
          Checks the response from the smtpd.
protected  boolean send(String message, PrintWriter writer, int expected)
          Prints a string using the specified PrintWriter.
protected  boolean sendCommands(PrintWriter writer)
          Sends the SMTP commands to the smtpd.
 boolean sendMail()
          Send the mail that was just created.
 void setDebug(boolean on)
          Switches debugging output on or off.
 void setFrom(String realName, String emailAddress)
          Sets the From field.
 void setReplyTo(String realName, String emailAddress)
          Sets the Reply-To field.
 void setSubject(String subject)
          Sets the Subject.
 void setText(String text)
          Sets the message body of this EMail.
 void setVerify(boolean on)
          If verify is switched on (default) SmtpMailer checks every response code from smtpd and compares it to the expected value.
 String toString()
          Returns a string representation of this SmtpMailer.
protected  void writeBody(PrintWriter writer)
          Uses this PrintWriter to print the message body.
protected  void writeHeaders(PrintWriter writer)
          Uses this PrintWriter to print the headers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SmtpMailer

public SmtpMailer(String host)
Creates a new simple SmtpMailer from a host name.
Parameters:
host - the host with the SMTP demon

SmtpMailer

public SmtpMailer()
Creates a new simple SmtpMailer. Tries to send the email to the localhost.
Method Detail

setFrom

public void setFrom(String realName,
                    String emailAddress)
Sets the From field. Only one From field is allowed. Subsequent calls will override this field.
Parameters:
realName - the real name of the sender
emailAddress - the email address of the sender

setReplyTo

public void setReplyTo(String realName,
                       String emailAddress)
Sets the Reply-To field. Can be called multiple times to send the email to several receivers.
Parameters:
realName - the real name of one receiver
emailAddress - the email address of one receiver

setVerify

public void setVerify(boolean on)
If verify is switched on (default) SmtpMailer checks every response code from smtpd and compares it to the expected value. You get the result of the verification at the method: @see sendMail. If this method returns false you can switch on debugging to see what's going wrong.
Parameters:
on - true to switch verification on

setDebug

public void setDebug(boolean on)
Switches debugging output on or off. Debug information is sent to System.err.
Parameters:
on - true switches debugging on

addTo

public void addTo(String realName,
                  String emailAddress)
Sets the To field. Can be called multiple times to send the email to several receivers.
Parameters:
realName - the real name of one receiver
emailAddress - the email address of one receiver

addCc

public void addCc(String realName,
                  String emailAddress)
Sets the Cc field. Can be called multiple times to send the email to several receivers.
Parameters:
realName - the real name of one receiver
emailAddress - the email address of one receiver

addBcc

public void addBcc(String realName,
                   String emailAddress)
Sets the Bcc field. Can be called multiple times to send the email to several receivers.
Parameters:
realName - the real name of one receiver
emailAddress - the email address of one receiver

addAdditionalField

public void addAdditionalField(String line)
Sets any additional field according to RFC822. For example: "MIME-Version: 1.0"
Parameters:
line - a complete line

setSubject

public void setSubject(String subject)
Sets the Subject.
Parameters:
subject - the subject

setText

public void setText(String text)
Sets the message body of this EMail. This method sets the message body to text.
Parameters:
text - the text to be appended

addText

public void addText(String text)
Adds text to message body of this EMail. This method just appends the text to a StringBuffer
Parameters:
text - the text to be appended

checkResponse

protected boolean checkResponse(int expected)
                         throws SmtpException
Checks the response from the smtpd.
Parameters:
expected - the expected response code
Returns:
true if the response code from the server was the expected, false otherwise
Throws:
SmtpException - if there was an error while reading the response

send

protected boolean send(String message,
                       PrintWriter writer,
                       int expected)
                throws SmtpException
Prints a string using the specified PrintWriter.
Parameters:
message - the string to print
writer - the PrintWriter to use
Returns:
true if the response code from the server was the expected, false otherwise
Throws:
SmtpException - if there was an error while sending this string

sendCommands

protected boolean sendCommands(PrintWriter writer)
                        throws SmtpException
Sends the SMTP commands to the smtpd.
Parameters:
writer - the PrintWriter to use
Returns:
true if the response codes from the server were the expected, false otherwise
Throws:
SmtpException - if there was an error while sending the commands

writeHeaders

protected void writeHeaders(PrintWriter writer)
Uses this PrintWriter to print the headers.
Parameters:
writer - the PrintWriter for printing the headers

writeBody

protected void writeBody(PrintWriter writer)
Uses this PrintWriter to print the message body.
Parameters:
writer - the PrintWriter for printing the message body

sendMail

public boolean sendMail()
                 throws SmtpException
Send the mail that was just created.
Returns:
true if the response codes from the server were the expected (if verify is switched on), false otherwise
Throws:
SmtpException - if there is a problem while sending the EMail

toString

public String toString()
Returns a string representation of this SmtpMailer.
Overrides:
toString in class Object
Returns:
the string representation

This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note).

IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK