com.precisonline.smtp
Class SMTPClient

java.lang.Object
  |
  +--com.precisonline.smtp.SMTPClient

public class SMTPClient
extends java.lang.Object

This lightweight class is used for sending email via SMTP from any Java application. It provides a very simple wrapper around the SMTP protocol.


Field Summary
protected  boolean anonymous
          If true, the recipients of the message will be masked by the value of anonymousText.
protected  java.lang.String anonymousText
          If anonymous is true, the list of message recipients will be masked by the text given here.
protected  java.util.Vector bccAddresses
          A list of addresses that define those to be blind cc'd on this message.
protected  java.util.Vector ccAddresses
          A list of addresses that define those to be cc'd on this message.
protected  SMTPAddress fromAddress
          An address that defines the sender of the message.
protected  java.lang.String helloName
          The name that this application will use to identify itself when it establishes the connection to the server.
protected  int port
          The port number where the SMTP server is listening for connections.
protected  int priority
          The priority of the message.
protected  java.lang.String replyMsg
          The last message received from the SMTP server.
protected  int sensitivity
          The sensitivity/confidentiality of the message.
protected  java.lang.String server
          The name or IP address of an SMTP server who will send the message.
protected  java.lang.String subject
          The subject line of the message to be sent.
protected  java.lang.String text
          The text of the message to be sent.
protected  java.util.Vector toAddresses
          A list of addresses that define where the message is going to.
 
Constructor Summary
SMTPClient()
          Creates new SMTPClient, connecting to localhost:25, normal priority, normal sensitivity, non-anonymous recipients.
SMTPClient(java.lang.String server, int port)
          Creates new SMTPClient, connecting the the server and port as named.
 
Method Summary
 boolean addBccAddress(SMTPAddress address)
          Adds a new address to the list of blind-cc addresses.
 boolean addBccAddress(java.lang.String address, java.lang.String name)
          Adds a new address to the list of blind-cc addresses.
 boolean addCcAddress(SMTPAddress address)
          Adds a new address to the list of cc addresses.
 boolean addCcAddress(java.lang.String address, java.lang.String name)
          Adds a new address to the list of cc addresses.
 boolean addText(java.lang.String text)
          Appends new text to the pre-existing message text.
 boolean addToAddress(SMTPAddress address)
          Adds a new address to the list of recipient addresses.
 boolean addToAddress(java.lang.String address, java.lang.String name)
          Adds a new address to the list of recipient addresses.
 java.util.Vector getBccAddresses()
          Returns the blind-cc addresses assigned to the message.
 java.util.Vector getCcAddresses()
          Returns the cc addresses assigned to the message.
 SMTPAddress getFromAddress()
          Returns the address of the message sender.
 java.lang.String getSubject()
          Returns the subject of the message as set by setSubject().
 java.lang.String getText()
          Returns the text of the message.
 java.util.Vector getToAddresses()
          Returns the addresses of the recipients.
 boolean send()
          Sends the message to the SMTP server.
 boolean setAnonymousRecipients()
          If set to true, the list of recipients will be masked by the contents of anonymousText.
 boolean setAnonymousText(java.lang.String text)
          If setAnonymousRecipients() is used, this method can be used to override the name of the anonymous recipients.
 boolean setBccAddresses(java.util.Vector addresses)
          Sets the blind-cc addresses for the message.
 boolean setCcAddresses(java.util.Vector addresses)
          Sets the cc addresses for the message.
 boolean setFromAddress(SMTPAddress address)
          Sets the address of the sender for the message.
 boolean setFromAddress(java.lang.String address, java.lang.String name)
          Sets the address of the sender of the message.
 boolean setHelloName(java.lang.String name)
          Changes the HELO name used as the first communication in the SMTP protocol.
 boolean setPort(int port)
          Changes the port number for connecting to the SMTP server.
 boolean setPriority(int priority)
          Sets the priority of the message.
 boolean setSensitivity(int sensitivity)
          Sets the sensitivity of the message.
 boolean setServer(java.lang.String server)
          Changes the name of the SMTP server.
 boolean setSubject(java.lang.String subject)
          Sets the subject line of the message.
 boolean setText(java.lang.String text)
          Sets the text of the message.
 boolean setToAddresses(java.util.Vector addresses)
          Sets the destination addresses for the message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

server

protected java.lang.String server
The name or IP address of an SMTP server who will send the message. By default, the server name is "localhost".

helloName

protected java.lang.String helloName
The name that this application will use to identify itself when it establishes the connection to the server.

subject

protected java.lang.String subject
The subject line of the message to be sent.

text

protected java.lang.String text
The text of the message to be sent.

anonymousText

protected java.lang.String anonymousText
If anonymous is true, the list of message recipients will be masked by the text given here. This is used for mailing lists, where the names of the list participants should be hidden from each other.

fromAddress

protected SMTPAddress fromAddress
An address that defines the sender of the message.

toAddresses

protected java.util.Vector toAddresses
A list of addresses that define where the message is going to.

ccAddresses

protected java.util.Vector ccAddresses
A list of addresses that define those to be cc'd on this message.

bccAddresses

protected java.util.Vector bccAddresses
A list of addresses that define those to be blind cc'd on this message. Blind CC'ing sends the message to the recipient, but does not include a header line identifying them as being a recipient.

replyMsg

protected java.lang.String replyMsg
The last message received from the SMTP server.

port

protected int port
The port number where the SMTP server is listening for connections. Typically this is port 25, unless overridden by the constructor or setPort().

priority

protected int priority
The priority of the message. PRIO_LOW, PRIO_NORMAL, and PRIO_HIGH are valid choices.

sensitivity

protected int sensitivity
The sensitivity/confidentiality of the message. SENS_NORMAL is normal, SENS_PERSONAL is a personal message (as defined by Microsoft Outlook), SENS_PRIVATE is a private message, and SENS_CONFIDENTIAL is a confidential message.

anonymous

protected boolean anonymous
If true, the recipients of the message will be masked by the value of anonymousText. If anonymousText is not reset, the default is "undisclosed-recipients".
Constructor Detail

SMTPClient

public SMTPClient()
Creates new SMTPClient, connecting to localhost:25, normal priority, normal sensitivity, non-anonymous recipients.

SMTPClient

public SMTPClient(java.lang.String server,
                  int port)
Creates new SMTPClient, connecting the the server and port as named.
Method Detail

setServer

public boolean setServer(java.lang.String server)
Changes the name of the SMTP server. The name of the server can be changed up until the message is sent with the send() method.

setPort

public boolean setPort(int port)
Changes the port number for connecting to the SMTP server. The port number can be changed until the message is sent with the send() method.

setHelloName

public boolean setHelloName(java.lang.String name)
Changes the HELO name used as the first communication in the SMTP protocol. This name identifies the sender of the message, and can be just about anything. Note, however, that certain servers will reject the communication if the HELO name isn't valid for their domain.

setFromAddress

public boolean setFromAddress(SMTPAddress address)
Sets the address of the sender for the message.
See Also:
SMTPAddress

setFromAddress

public boolean setFromAddress(java.lang.String address,
                              java.lang.String name)
Sets the address of the sender of the message.

setToAddresses

public boolean setToAddresses(java.util.Vector addresses)
Sets the destination addresses for the message. Any addresses that were been previously assigned will be overwritten.

addToAddress

public boolean addToAddress(SMTPAddress address)
Adds a new address to the list of recipient addresses.

addToAddress

public boolean addToAddress(java.lang.String address,
                            java.lang.String name)
Adds a new address to the list of recipient addresses.

setCcAddresses

public boolean setCcAddresses(java.util.Vector addresses)
Sets the cc addresses for the message. Any addresses that were previously assigned will be overwritten.

addCcAddress

public boolean addCcAddress(SMTPAddress address)
Adds a new address to the list of cc addresses.

addCcAddress

public boolean addCcAddress(java.lang.String address,
                            java.lang.String name)
Adds a new address to the list of cc addresses.

setBccAddresses

public boolean setBccAddresses(java.util.Vector addresses)
Sets the blind-cc addresses for the message. Any addresses that were previously assigned will be overwritten.

addBccAddress

public boolean addBccAddress(SMTPAddress address)
Adds a new address to the list of blind-cc addresses.

addBccAddress

public boolean addBccAddress(java.lang.String address,
                             java.lang.String name)
Adds a new address to the list of blind-cc addresses.

setSubject

public boolean setSubject(java.lang.String subject)
Sets the subject line of the message.

setText

public boolean setText(java.lang.String text)
Sets the text of the message. If the message has any text in it prior to this call, that text will be discarded.

addText

public boolean addText(java.lang.String text)
Appends new text to the pre-existing message text.

setPriority

public boolean setPriority(int priority)
Sets the priority of the message. Parameter can be expressed numerically or via SMTPClient.PRIO_LOW or SMTPClient.PRIO_HIGH.

setSensitivity

public boolean setSensitivity(int sensitivity)
Sets the sensitivity of the message. Parameter can be expressed numerically or via SMTPClient.SENS_NORMAL, SMTPClient.SENS_PERSONAL, SMTPClient.SENS_PRIVATE, or SMTPClient.SENS_CONFIDENTIAL.

setAnonymousRecipients

public boolean setAnonymousRecipients()
If set to true, the list of recipients will be masked by the contents of anonymousText. This is used for mailing lists where the names of the individual recipients should be private.

setAnonymousText

public boolean setAnonymousText(java.lang.String text)
If setAnonymousRecipients() is used, this method can be used to override the name of the anonymous recipients. If not overrridden, "undisclosed-recipients" is used.

getSubject

public java.lang.String getSubject()
Returns the subject of the message as set by setSubject().

getText

public java.lang.String getText()
Returns the text of the message.

getFromAddress

public SMTPAddress getFromAddress()
Returns the address of the message sender.
See Also:
SMTPAddress

getToAddresses

public java.util.Vector getToAddresses()
Returns the addresses of the recipients.

getCcAddresses

public java.util.Vector getCcAddresses()
Returns the cc addresses assigned to the message.

getBccAddresses

public java.util.Vector getBccAddresses()
Returns the blind-cc addresses assigned to the message.

send

public boolean send()
Sends the message to the SMTP server. This implementation establishes a simple client socket connection and performs the required discussion to send the message. If there are ANY errors, such as an improperly formatted email address, the send will fail and return false.