org.apache.mailet
Class MailAddress

java.lang.Object
  extended by org.apache.mailet.MailAddress
All Implemented Interfaces:
java.io.Serializable

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

A representation of an email address.

This class encapsulates functionalities to access to different parts of an email address without dealing with its parsing.

A MailAddress is an address specified in the MAIL FROM and RCPT TO commands in SMTP sessions. These are either passed by an external server to the mailet-compliant SMTP server, or they are created programmatically by the mailet-compliant server to send to another (external) SMTP server. Mailets and matchers use the MailAddress for the purpose of evaluating the sender and recipient(s) of a message.

MailAddress parses an email address as defined in RFC 821 (SMTP) p. 30 and 31 where addresses are defined in BNF convention. As the mailet API does not support the aged "SMTP-relayed mail" addressing protocol, this leaves all addresses to be a , as per the spec. The MailAddress's "user" is the of the and "host" is the of the mailbox.

This class is a good way to validate email addresses as there are some valid addresses which would fail with a simpler approach to parsing address. It also removes parsing burden from mailets and matchers that might not realize the flexibility of an SMTP address. For instance, "serge@home"@lokitech.com is a valid SMTP address (the quoted text serge@home is the user and lokitech.com is the host). This means all current parsing to date is incorrect as we just find the first @ and use that to separate user from host.

This parses an address as per the BNF specification for from RFC 821 on page 30 and 31, section 4.1.2. COMMAND SYNTAX. http://www.freesoft.org/CIE/RFC/821/15.htm

Version:
1.0
See Also:
Serialized Form

Field Summary
static long serialVersionUID
           
 
Constructor Summary
MailAddress(javax.mail.internet.InternetAddress address)
          Constructs a MailAddress from a JavaMail InternetAddress, using only the email address portion, discarding the personal name.
MailAddress(java.lang.String address)
          Construct a MailAddress parsing the provided String object.
MailAddress(java.lang.String newUser, java.lang.String newHost)
          Construct a MailAddress with the provided personal name and email address.
 
Method Summary
 boolean equals(java.lang.Object obj)
           
 java.lang.String getHost()
          Return the host part.
 java.lang.String getUser()
          Return the user part.
 int hashCode()
          Return a hashCode for this object which should be identical for addresses which are equivalent.
 javax.mail.internet.InternetAddress toInternetAddress()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

public static final long serialVersionUID
See Also:
Constant Field Values
Constructor Detail

MailAddress

public MailAddress(java.lang.String address)
            throws javax.mail.internet.ParseException

Construct a MailAddress parsing the provided String object.

The personal variable is left empty.

Parameters:
address - the email address compliant to the RFC822 format
Throws:
javax.mail.internet.ParseException - if the parse failed

MailAddress

public MailAddress(java.lang.String newUser,
                   java.lang.String newHost)
            throws javax.mail.internet.ParseException
Construct a MailAddress with the provided personal name and email address.

Parameters:
user - the username or account name on the mail server
host - the server that should accept messages for this user
Throws:
javax.mail.internet.ParseException - if the parse failed

MailAddress

public MailAddress(javax.mail.internet.InternetAddress address)
            throws javax.mail.internet.ParseException
Constructs a MailAddress from a JavaMail InternetAddress, using only the email address portion, discarding the personal name.

Throws:
javax.mail.internet.ParseException
Method Detail

getHost

public java.lang.String getHost()
Return the host part.

Returns:
a String object representing the host part of this email address. If the host is of the dotNum form (e.g. [yyy.yyy.yyy.yyy]) then strip the braces first.

getUser

public java.lang.String getUser()
Return the user part.

Returns:
a String object representing the user part of this email address.
Throws:
AddressException - if the parse failed

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toInternetAddress

public javax.mail.internet.InternetAddress toInternetAddress()

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Return a hashCode for this object which should be identical for addresses which are equivalent. This is implemented by obtaining the default hashcode of the String representation of the MailAddress. Without this explicit definition, the default hashCode will create different hashcodes for separate object instances.

Overrides:
hashCode in class java.lang.Object
Returns:
the hashcode.


Copyright © 2002-2007 The Apache Software Foundation. All Rights Reserved.