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 functionality to access 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 <mailbox>, as per the spec.

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 the 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 local-part and lokitech.com is the domain). This means all current parsing to date is incorrect as we just find the first '@' and use that to separate local-part from domain.

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
          We hardcode the serialVersionUID This version (2779163542539434916L) retains compatibility back to Mailet version 1.2 (James 1.2) so that MailAddress will be deserializable and mail doesn't get lost after an upgrade.
 
Constructor Summary
MailAddress(javax.mail.internet.InternetAddress address)
          Constructs a MailAddress from an InternetAddress, using only the email address portion (an "addr-spec", not "name-addr", as defined in the RFC2822 3.4.
MailAddress(java.lang.String address)
          Constructs a MailAddress by parsing the provided address.
MailAddress(java.lang.String localPart, java.lang.String domain)
          Constructs a MailAddress with the provided local part and domain.
 
Method Summary
 boolean equals(java.lang.Object obj)
          Indicates whether some other object is "equal to" this one.
 java.lang.String getDomain()
          Returns the domain part per RFC2822 3.4.1.
 java.lang.String getHost()
          Deprecated. use getDomain(), whose name was changed to align with RFC2822 3.4.1. addr-spec specification
 java.lang.String getLocalPart()
          Returns the local-part per RFC2822 3.4.1.
 java.lang.String getUser()
          Deprecated. use getLocalPart(), whose name was changed to align with the RFC2822 3.4.1. addr-spec specification
 int hashCode()
          Returns a hash code value for this object.
 javax.mail.internet.InternetAddress toInternetAddress()
          Returns an InternetAddress representing the same address as this MailAddress.
 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
We hardcode the serialVersionUID This version (2779163542539434916L) retains compatibility back to Mailet version 1.2 (James 1.2) so that MailAddress will be deserializable and mail doesn't get lost after an upgrade.

See Also:
Constant Field Values
Constructor Detail

MailAddress

public MailAddress(java.lang.String address)
            throws javax.mail.internet.AddressException
Constructs a MailAddress by parsing the provided address.

Parameters:
address - the email address, compliant to the RFC2822 3.4.1. addr-spec specification
Throws:
javax.mail.internet.AddressException - if the parse failed

MailAddress

public MailAddress(java.lang.String localPart,
                   java.lang.String domain)
            throws javax.mail.internet.AddressException
Constructs a MailAddress with the provided local part and domain.

Parameters:
localPart - the local-part portion. This is a domain dependent string. In addresses, it is simply interpreted on the particular host as a name of a particular mailbox. per RFC2822 3.4.1. addr-spec specification
domain - the domain portion. This identifies the point to which the mail is delivered per RFC2822 3.4.1. addr-spec specification
Throws:
javax.mail.internet.AddressException - if the parse failed

MailAddress

public MailAddress(javax.mail.internet.InternetAddress address)
            throws javax.mail.internet.AddressException
Constructs a MailAddress from an InternetAddress, using only the email address portion (an "addr-spec", not "name-addr", as defined in the RFC2822 3.4. Address Specification)

Parameters:
address - the address
Throws:
javax.mail.internet.AddressException - if the parse failed
Method Detail

getHost

public java.lang.String getHost()
Deprecated. use getDomain(), whose name was changed to align with RFC2822 3.4.1. addr-spec specification

Returns the host part.

Returns:
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.

getDomain

public java.lang.String getDomain()
Returns the domain part per RFC2822 3.4.1. addr-spec specification.

Returns:
the domain part of this email address. If the domain is of the domain-literal form (e.g. [yyy.yyy.yyy.yyy]), the braces will have been stripped returning the raw IP address.

getUser

public java.lang.String getUser()
Deprecated. use getLocalPart(), whose name was changed to align with the RFC2822 3.4.1. addr-spec specification

Returns the user part.

Returns:
the user part of this email address

getLocalPart

public java.lang.String getLocalPart()
Returns the local-part per RFC2822 3.4.1. addr-spec specification.

Returns:
the local-part of this email address as defined by the RFC2822 3.4.1. addr-spec specification. The local-part portion is a domain dependent string. In addresses, it is simply interpreted on the particular host as a name of a particular mailbox (the part before the "@" character)

toString

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

toInternetAddress

public javax.mail.internet.InternetAddress toInternetAddress()
Returns an InternetAddress representing the same address as this MailAddress.

Returns:
the address

equals

public boolean equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this one. Note that this implementation breaks the general contract of the equals method by allowing an instance to equal to a String. It is recommended that implementations avoid relying on this design which may be removed in a future release.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Returns a hash code value for this object.

This method is implemented by returning the hash code of the canonical string representation of this address, so that all instances representing the same address will return an identical hash code.

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


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