org.apache.geronimo.javamail.transport.smtp
Class SMTPConnection

java.lang.Object
  extended by org.apache.geronimo.javamail.util.MailConnection
      extended by org.apache.geronimo.javamail.transport.smtp.SMTPConnection

public class SMTPConnection
extends MailConnection

Simple implementation of SMTP transport. Just does plain RFC977-ish delivery.

Version:
$Rev: 673649 $ $Date: 2008-07-03 06:37:56 -0400 (Thu, 03 Jul 2008) $

Nested Class Summary
static class SMTPConnection.SendStatus
          Simple holder class for the address/send status duple, as we can have mixed success for a set of addresses and a message
 
Field Summary
protected static int DEFAULT_NNTP_PORT
          property keys for protocol properties.
protected  org.apache.geronimo.javamail.transport.smtp.SMTPReply lastServerResponse
           
protected static String MAIL_SMTP_ALLOW8BITMIME
           
protected static String MAIL_SMTP_AUTH
           
protected static String MAIL_SMTP_DSN_RET
           
protected static String MAIL_SMTP_EHLO
           
protected static String MAIL_SMTP_EXTENSION
           
protected static String MAIL_SMTP_FROM
           
protected static String MAIL_SMTP_QUITWAIT
           
protected static String MAIL_SMTP_REPORT_SUCCESS
           
protected static String MAIL_SMTP_STARTTLS_ENABLE
           
protected static String MAIL_SMTP_SUBMITTER
           
protected  BufferedReader reader
           
protected  boolean reportSuccess
           
protected  boolean serverTLS
           
protected  boolean use8bit
           
protected  boolean useTLS
           
protected  PrintWriter writer
           
 
Fields inherited from class org.apache.geronimo.javamail.util.MailConnection
AUTHENTICATION_CRAMMD5, AUTHENTICATION_DIGESTMD5, AUTHENTICATION_LOGIN, AUTHENTICATION_PLAIN, authentications, authid, capabilities, CAPABILITY_STARTTLS, CR, debug, debugStream, DEFAULT_MAIL_HOST, defaultPort, inputStream, LF, localAddress, localHost, localPort, MAIL_AUTH, MAIL_AUTHORIZATIONID, MAIL_ENCODE_TRACE, MAIL_FACTORY_CLASS, MAIL_FACTORY_FALLBACK, MAIL_FACTORY_PORT, MAIL_LOCALADDRESS, MAIL_LOCALHOST, MAIL_LOCALPORT, MAIL_LOGIN_DISABLE, MAIL_PLAIN_DISABLE, MAIL_PORT, MAIL_SASL_ENABLE, MAIL_SASL_MECHANISMS, MAIL_SASL_REALM, MAIL_SSL_CIPHERSUITES, MAIL_SSL_ENABLE, MAIL_SSL_PROTOCOLS, MAIL_STARTTLS_ENABLE, MAIL_TIMEOUT, mechanisms, MIN_MILLIS, outputStream, password, props, protocol, realm, serverHost, serverPort, session, socket, sslConnection, timeout, TIMEOUT, username
 
Constructor Summary
SMTPConnection(ProtocolProperties props)
          Normal constructor for an SMTPConnection() object.
 
Method Summary
 void close()
          Close the connection.
protected  boolean convertTransferEncoding(MimePart bodyPart)
          Check to see if a MIME body part can have its encoding changed from quoted-printable or base64 encoding to 8bit encoding.
 String extensionParameter(String name)
          Retrieve any argument information associated with a extension reported back by the server on the EHLO command.
protected  String fixEmailAddress(String mail)
          Convert an InternetAddress into a form sendable on an SMTP mail command.
protected  void getConnectedTLSSocket()
          Switch the connection to using TLS level security, switching to an SSL socket.
 org.apache.geronimo.javamail.transport.smtp.SMTPReply getLastServerResponse()
          Retrieve the last response received from the SMTP server.
protected  org.apache.geronimo.javamail.transport.smtp.SMTPReply getReply()
          Get a reply line for an SMTP command.
 boolean getReportSuccess()
          Return the current reportSuccess property.
protected  ClientAuthenticator getSaslAuthenticator()
          Attempt to retrieve a SASL authenticator for this protocol.
protected  int getSizeEstimate(Message msg)
          Get an estimate of the transmission size for this message.
 boolean getStartTLS()
          Return the current startTLS property.
protected  boolean getWelcome()
          Get the server's welcome blob from the wire....
protected  boolean isValid8bit(InputStream inStream)
          Read the bytes in a stream a test to see if this conforms to the RFC 2045 rules for 8bit encoding. 1) No more than 998 bytes long 2) All lines are terminated with CRLF sequences 3) CR and LF characters only occur in properly formed line separators 4) No null characters are allowed.
protected  boolean processAuthentication()
          Authenticate with the server, if necessary (or possible).
protected  void processExtension(String extension)
          Process an extension string passed back as the EHLP response.
 boolean protocolConnect(String host, int port, String username, String password)
          Connect to the server and do the initial handshaking.
protected  String receiveLine()
          Receives one line from the server.
protected  String receiveLine(int delayMillis)
          Receives one line from the server.
 void resetConnection()
          Reset the server connection after an error.
protected  org.apache.geronimo.javamail.transport.smtp.SMTPReply sendCommand(String data)
          Send a command to the server, returning the first response line back as a reply.
protected  void sendData(Message msg)
          Sends the data in the message down the socket.
protected  boolean sendEhlo()
          Send the EHLO command to the SMTP server.
protected  boolean sendHandshake()
          Start the handshake process with the server, including setting up and TLS-level work.
protected  void sendHelo()
          Send the HELO command to the SMTP server.
protected  void sendLine(String data)
          Sends a message down the socket and terminates with the appropriate CRLF
protected  boolean sendMailFrom(Message message)
          Set the sender for this mail.
protected  void sendQuit()
          Sends the QUIT message and receieves the response
 SMTPConnection.SendStatus sendRcptTo(InternetAddress addr, String dsn)
          Sets a receiver address for the current message
 void setReportSuccess(boolean report)
          Set a new value for the reportSuccess property.
 void setStartTLS(boolean start)
          Set a new value for the startTLS property.
 boolean supportsExtension(String name)
          Tests whether the target server supports a named extension.
 String toString()
           
 
Methods inherited from class org.apache.geronimo.javamail.util.MailConnection
checkConnected, closeServerConnection, connect, debugOut, debugOut, getCapabilities, getConnectedSocket, getConnectedSSLSocket, getConnection, getConnectionProperties, getConnectionStreams, getHost, getLocalHost, getLoginAuthenticator, getSaslMechanisms, getSASLRealm, getServerMechanisms, hasCapability, selectSaslMechanisms, setLocalHost, setSASLRealm, supportsMechanism
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MAIL_SMTP_QUITWAIT

protected static final String MAIL_SMTP_QUITWAIT
See Also:
Constant Field Values

MAIL_SMTP_EXTENSION

protected static final String MAIL_SMTP_EXTENSION
See Also:
Constant Field Values

MAIL_SMTP_EHLO

protected static final String MAIL_SMTP_EHLO
See Also:
Constant Field Values

MAIL_SMTP_ALLOW8BITMIME

protected static final String MAIL_SMTP_ALLOW8BITMIME
See Also:
Constant Field Values

MAIL_SMTP_REPORT_SUCCESS

protected static final String MAIL_SMTP_REPORT_SUCCESS
See Also:
Constant Field Values

MAIL_SMTP_STARTTLS_ENABLE

protected static final String MAIL_SMTP_STARTTLS_ENABLE
See Also:
Constant Field Values

MAIL_SMTP_AUTH

protected static final String MAIL_SMTP_AUTH
See Also:
Constant Field Values

MAIL_SMTP_FROM

protected static final String MAIL_SMTP_FROM
See Also:
Constant Field Values

MAIL_SMTP_DSN_RET

protected static final String MAIL_SMTP_DSN_RET
See Also:
Constant Field Values

MAIL_SMTP_SUBMITTER

protected static final String MAIL_SMTP_SUBMITTER
See Also:
Constant Field Values

DEFAULT_NNTP_PORT

protected static final int DEFAULT_NNTP_PORT
property keys for protocol properties.

See Also:
Constant Field Values

lastServerResponse

protected org.apache.geronimo.javamail.transport.smtp.SMTPReply lastServerResponse

reader

protected BufferedReader reader

writer

protected PrintWriter writer

reportSuccess

protected boolean reportSuccess

serverTLS

protected boolean serverTLS

useTLS

protected boolean useTLS

use8bit

protected boolean use8bit
Constructor Detail

SMTPConnection

public SMTPConnection(ProtocolProperties props)
Normal constructor for an SMTPConnection() object.

Parameters:
props - The property bundle for this protocol instance.
Method Detail

protocolConnect

public boolean protocolConnect(String host,
                               int port,
                               String username,
                               String password)
                        throws MessagingException
Connect to the server and do the initial handshaking.

Overrides:
protocolConnect in class MailConnection
Parameters:
host - The target host name.
port - The target port
username - The connection username (can be null)
password - The authentication password (can be null).
Returns:
true if we were able to obtain a connection and authenticate.
Throws:
MessagingException

close

public void close()
           throws MessagingException
Close the connection. On completion, we'll be disconnected from the server and unable to send more data.

Throws:
MessagingException

toString

public String toString()
Overrides:
toString in class Object

sendMailFrom

protected boolean sendMailFrom(Message message)
                        throws MessagingException
Set the sender for this mail.

Parameters:
message - The message we're sending.
Returns:
True if the command was accepted, false otherwise.
Throws:
MessagingException

convertTransferEncoding

protected boolean convertTransferEncoding(MimePart bodyPart)
Check to see if a MIME body part can have its encoding changed from quoted-printable or base64 encoding to 8bit encoding. In order for this to work, it must follow the rules laid out in RFC 2045. To qualify for conversion, the text must be: 1) No more than 998 bytes long 2) All lines are terminated with CRLF sequences 3) CR and LF characters only occur in properly formed line separators 4) No null characters are allowed. The conversion will only be applied to text elements, and this will recurse through the different elements of MultiPart content.

Parameters:
bodyPart - The bodyPart to convert. Initially, this will be the message itself.
Returns:
true if any conversion was performed, false if nothing was converted.

getWelcome

protected boolean getWelcome()
                      throws MessagingException
Get the server's welcome blob from the wire....

Throws:
MessagingException

getSizeEstimate

protected int getSizeEstimate(Message msg)
Get an estimate of the transmission size for this message. This size is the complete message as it is encoded and transmitted on the DATA command, not counting the terminating ".CRLF".

Parameters:
msg - The message we're sending.
Returns:
The count of bytes, if it can be calculated.

sendData

protected void sendData(Message msg)
                 throws MessagingException
Sends the data in the message down the socket. This presumes the server is in the right place and ready for getting the DATA message and the data right place in the sequence

Throws:
MessagingException

sendQuit

protected void sendQuit()
                 throws MessagingException
Sends the QUIT message and receieves the response

Throws:
MessagingException

sendRcptTo

public SMTPConnection.SendStatus sendRcptTo(InternetAddress addr,
                                            String dsn)
                                     throws MessagingException
Sets a receiver address for the current message

Parameters:
addr - The target address.
dsn - An optional DSN option appended to the RCPT TO command.
Returns:
The status for this particular send operation.
Throws:
MessagingException

sendCommand

protected org.apache.geronimo.javamail.transport.smtp.SMTPReply sendCommand(String data)
                                                                     throws MessagingException
Send a command to the server, returning the first response line back as a reply.

Parameters:
data - The data to send.
Returns:
A reply object with the reply line.
Throws:
MessagingException

sendLine

protected void sendLine(String data)
                 throws MessagingException
Sends a message down the socket and terminates with the appropriate CRLF

Throws:
MessagingException

receiveLine

protected String receiveLine()
                      throws MessagingException
Receives one line from the server. A line is a sequence of bytes terminated by a CRLF

Returns:
the line from the server as String
Throws:
MessagingException

getReply

protected org.apache.geronimo.javamail.transport.smtp.SMTPReply getReply()
                                                                  throws MessagingException
Get a reply line for an SMTP command.

Returns:
An SMTP reply object from the stream.
Throws:
MessagingException

getLastServerResponse

public org.apache.geronimo.javamail.transport.smtp.SMTPReply getLastServerResponse()
Retrieve the last response received from the SMTP server.

Returns:
The raw response string (including the error code) returned from the SMTP server.

receiveLine

protected String receiveLine(int delayMillis)
                      throws MessagingException
Receives one line from the server. A line is a sequence of bytes terminated by a CRLF

Returns:
the line from the server as String
Throws:
MessagingException

fixEmailAddress

protected String fixEmailAddress(String mail)
Convert an InternetAddress into a form sendable on an SMTP mail command. InternetAddress.getAddress() generally returns just the address portion of the full address, minus route address markers. We need to ensure we have an address with '<' and '>' delimiters.

Parameters:
mail - The mail address returned from InternetAddress.getAddress().
Returns:
A string formatted for sending.

sendHandshake

protected boolean sendHandshake()
                         throws MessagingException
Start the handshake process with the server, including setting up and TLS-level work. At the completion of this task, we should be ready to authenticate with the server, if needed.

Throws:
MessagingException

getConnectedTLSSocket

protected void getConnectedTLSSocket()
                              throws MessagingException
Switch the connection to using TLS level security, switching to an SSL socket.

Overrides:
getConnectedTLSSocket in class MailConnection
Throws:
MessagingException

sendEhlo

protected boolean sendEhlo()
                    throws MessagingException
Send the EHLO command to the SMTP server.

Returns:
True if the command was accepted ok, false for any errors.
Throws:
SMTPTransportException
MalformedSMTPReplyException
MessagingException

sendHelo

protected void sendHelo()
                 throws MessagingException
Send the HELO command to the SMTP server.

Throws:
MessagingException

getStartTLS

public boolean getStartTLS()
Return the current startTLS property.

Returns:
The current startTLS property.

setStartTLS

public void setStartTLS(boolean start)
Set a new value for the startTLS property.

Parameters:
start - The new setting.

processExtension

protected void processExtension(String extension)
Process an extension string passed back as the EHLP response.

Parameters:
extension - The string value of the extension (which will be of the form "NAME arguments").

extensionParameter

public String extensionParameter(String name)
Retrieve any argument information associated with a extension reported back by the server on the EHLO command.

Parameters:
name - The name of the target server extension.
Returns:
Any argument passed on a server extension. Returns null if the extension did not include an argument or the extension was not supported.

supportsExtension

public boolean supportsExtension(String name)
Tests whether the target server supports a named extension.

Parameters:
name - The target extension name.
Returns:
true if the target server reported on the EHLO command that is supports the targer server, false if the extension was not supported.

processAuthentication

protected boolean processAuthentication()
                                 throws MessagingException
Authenticate with the server, if necessary (or possible).

Returns:
true if we are ok to proceed, false for an authentication failures.
Throws:
MessagingException

getSaslAuthenticator

protected ClientAuthenticator getSaslAuthenticator()
Attempt to retrieve a SASL authenticator for this protocol.

Returns:
A SASL authenticator, or null if a suitable one was not located.

isValid8bit

protected boolean isValid8bit(InputStream inStream)
Read the bytes in a stream a test to see if this conforms to the RFC 2045 rules for 8bit encoding. 1) No more than 998 bytes long 2) All lines are terminated with CRLF sequences 3) CR and LF characters only occur in properly formed line separators 4) No null characters are allowed.

Parameters:
inStream - The source input stream.
Returns:
true if this can be transmitted successfully using 8bit encoding, false if an alternate encoding will be required.

resetConnection

public void resetConnection()
                     throws MessagingException
Reset the server connection after an error.

Throws:
MessagingException

getReportSuccess

public boolean getReportSuccess()
Return the current reportSuccess property.

Returns:
The current reportSuccess property.

setReportSuccess

public void setReportSuccess(boolean report)
Set a new value for the reportSuccess property.

Parameters:
report - The new setting.


Copyright © 2003-2009 The Apache Software Foundation. All Rights Reserved.