com.plexobject.mail
Class EmailValidater

java.lang.Object
  |
  +--com.plexobject.mail.EmailValidater
All Implemented Interfaces:
java.io.Serializable

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

See Also:
Serialized Form

Field Summary
static int VERIFY_EMAIL
          Verify using SMTP's VRFY, EXPN or RCPT TO
static int VERIFY_SYNTAX
          Check syntax of email
 
Constructor Summary
EmailValidater(java.io.File white, java.io.File black)
           
EmailValidater(java.io.InputStream white, java.io.InputStream black)
           
EmailValidater(java.io.Reader white, java.io.Reader black)
           
 
Method Summary
 void addTrustedDomain(java.lang.String domain)
          it adds regular expression that matches regular expression for all emails at specified domain to white list
 void addTrustedEmail(java.lang.String email)
          addTrustedEmail - adds email to white list
 void blockDomain(java.lang.String domain)
          it adds regular expression that matches regular expression all emails at specified domain to black list
 void blockEmail(java.lang.String email)
          blockEmail - adds email to black list
 java.util.ArrayList getBlackList()
           
 int getCheckLevel(int level)
          getCheckLevel returns level for validating emails
static java.lang.String[] getListSubscribers(java.lang.String address)
          returns list of subscribers for given mailing list if his/her email server supports EXPN command of SMTP
static java.lang.String getUserName(java.lang.String address)
          returns user-name from given email address if his/her email server supports VRFY command of SMTP
 java.util.ArrayList getWhiteList()
           
 boolean isBlackListed(java.lang.String email)
          isBlackListed - checks email address in black list.
 boolean isWhiteListed(java.lang.String email)
          isWhiteListed - checks email address in white list.
static void main(java.lang.String[] args)
           
 void removeBlockDomain(java.lang.String domain)
          it removes regular expression that matches regular expression all emails at specified domain from black list
 void removeBlockEmail(java.lang.String email)
          removeBlockEmail - remove email from black list
 void removeTrustedDomain(java.lang.String domain)
          it removes regular expression that matches regular expression all emails at specified domain from white list
 void removeTrustedEmail(java.lang.String email)
          removeTrustedEmail - remove email from white list
 void saveBlackList(java.io.File file)
          saveBlackList -saves list of trusted emails to a file
 void saveWhiteList(java.io.File file)
          saveWhiteList -saves list of trusted emails to a file
 void setCheckLevel(int level)
          setCheckLevel sets level for validating emails
 void validate(java.lang.String email)
          validate - verifies email based on check level previously set by the user
 void validate(java.lang.String email, boolean updateBlackList)
          validate - verifies email based on check level previously set by the user
 void validate(java.lang.String email, int check, boolean updateBlackList)
          validate - verifies email based on check level specified
static void validateSyntax(java.lang.String address)
          validateSyntax - checks email's syntax
 void verifyEmail(java.lang.String email, boolean updateBlackList)
          verifyEmail - connects to the mail server and verifies email address
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERIFY_SYNTAX

public static final int VERIFY_SYNTAX
Check syntax of email

See Also:
Constant Field Values

VERIFY_EMAIL

public static final int VERIFY_EMAIL
Verify using SMTP's VRFY, EXPN or RCPT TO

See Also:
Constant Field Values
Constructor Detail

EmailValidater

public EmailValidater(java.io.File white,
                      java.io.File black)
               throws java.io.IOException
Parameters:
white - specifies file that stores list of trusted email addresses or regular expressions. If email address matches whitelist, it is trusted as validated and is not verified against mail server of the domain.
black - specifies file that stores list of untrusted email addresses or regular expressions. If email address matches blacklist, it is rejected without verifying against mail server of the domain. Note: Both white and black list files specify an email address or regular expression one per line. You can contain comments if the line begins with #, e.g. ### White List .*@myhost.com ... ### Black List userxxx@somehost.com .*@hotmail.com ...

EmailValidater

public EmailValidater(java.io.InputStream white,
                      java.io.InputStream black)
               throws java.io.IOException
Parameters:
white - specifies file that stores list of trusted email addresses or regular expressions. If email address matches whitelist, it is trusted as validated and is not verified against mail server of the domain.
black - specifies file that stores list of untrusted email addresses or regular expressions. If email address matches blacklist, it is rejected without verifying against mail server of the domain. Note: Both white and black list files specify an email address or regular expression one per line. You can contain comments if the line begins with #, e.g. ### White List .*@myhost.com ... ### Black List userxxx@somehost.com .*@hotmail.com ...

EmailValidater

public EmailValidater(java.io.Reader white,
                      java.io.Reader black)
               throws java.io.IOException
Parameters:
white - specifies file that stores list of trusted email addresses or regular expressions. If email address matches whitelist, it is trusted as validated and is not verified against mail server of the domain.
black - specifies file that stores list of untrusted email addresses or regular expressions. If email address matches blacklist, it is rejected without verifying against mail server of the domain. Note: Both white and black list files specify an email address or regular expression one per line. You can contain comments if the line begins with #, e.g. ### White List .*@myhost.com ... ### Black List userxxx@somehost.com .*@hotmail.com ...
Method Detail

setCheckLevel

public void setCheckLevel(int level)
setCheckLevel sets level for validating emails

Parameters:
level - - indicates new checking level for email verification

getCheckLevel

public int getCheckLevel(int level)
getCheckLevel returns level for validating emails

Returns:
- checking level for email verification

validate

public void validate(java.lang.String email)
              throws IllegalEmailException
validate - verifies email based on check level previously set by the user

Parameters:
email - - email address to verify
Throws:
IllegalEmailException - upon verification failure

validate

public void validate(java.lang.String email,
                     boolean updateBlackList)
              throws IllegalEmailException
validate - verifies email based on check level previously set by the user

Parameters:
email - - email address to verify
updateBlackList - - updates black list if email address is invalid. However, it will not updae blacklist if there is an I/O error while communicating with the mail server.
Throws:
IllegalEmailException - upon verification failure

validate

public void validate(java.lang.String email,
                     int check,
                     boolean updateBlackList)
              throws IllegalEmailException
validate - verifies email based on check level specified

Parameters:
email - - email address to verify
updateBlackList - - updates black list if email address cannot be verified. However, it will not updae blacklist if there is a syntax error or if there is an I/O error while communicating with the mail server.
Throws:
IllegalEmailException - upon verification failure

validateSyntax

public static void validateSyntax(java.lang.String address)
                           throws IllegalEmailException
validateSyntax - checks email's syntax

Throws:
IllegalEmailException - if syntax is incorrect.

verifyEmail

public void verifyEmail(java.lang.String email,
                        boolean updateBlackList)
                 throws IllegalEmailException
verifyEmail - connects to the mail server and verifies email address

Parameters:
email - - email address to check
Throws:
IllegalEmailException - if mail server cannot be connected or email address does not exist.

isWhiteListed

public boolean isWhiteListed(java.lang.String email)
isWhiteListed - checks email address in white list. White list can be list of email addresses or any regular expressions such as .*@myhost.com

Parameters:
email - address to check
Returns:
true if email matches any of the email addresses or regular expressions in white list, false otherwise.

addTrustedEmail

public void addTrustedEmail(java.lang.String email)
addTrustedEmail - adds email to white list

Parameters:
email - address or regular expression

addTrustedDomain

public void addTrustedDomain(java.lang.String domain)
it adds regular expression that matches regular expression for all emails at specified domain to white list

Parameters:
domain - or regular expression

removeTrustedEmail

public void removeTrustedEmail(java.lang.String email)
removeTrustedEmail - remove email from white list

Parameters:
email - address or regular expression

removeTrustedDomain

public void removeTrustedDomain(java.lang.String domain)
it removes regular expression that matches regular expression all emails at specified domain from white list

Parameters:
domain - or regular expression

isBlackListed

public boolean isBlackListed(java.lang.String email)
isBlackListed - checks email address in black list. Black list can be list of email addresses or any regular expressions such as .*@myhost.com

Parameters:
email - address to check
Returns:
true if email matches any of the email addresses or regular expressions in black list, false otherwise.

blockEmail

public void blockEmail(java.lang.String email)
blockEmail - adds email to black list

Parameters:
email - address or regular expression

blockDomain

public void blockDomain(java.lang.String domain)
it adds regular expression that matches regular expression all emails at specified domain to black list

Parameters:
domain - or regular expression

removeBlockEmail

public void removeBlockEmail(java.lang.String email)
removeBlockEmail - remove email from black list

Parameters:
email - address or regular expression

removeBlockDomain

public void removeBlockDomain(java.lang.String domain)
it removes regular expression that matches regular expression all emails at specified domain from black list

Parameters:
domain - or regular expression

getBlackList

public java.util.ArrayList getBlackList()

getWhiteList

public java.util.ArrayList getWhiteList()

saveWhiteList

public void saveWhiteList(java.io.File file)
                   throws java.io.IOException
saveWhiteList -saves list of trusted emails to a file

Parameters:
file - - output file
Throws:
java.io.IOException - - is thrown if there is an I/O error

saveBlackList

public void saveBlackList(java.io.File file)
                   throws java.io.IOException
saveBlackList -saves list of trusted emails to a file

Parameters:
file - - output file
Throws:
java.io.IOException - - is thrown if there is an I/O error

getUserName

public static java.lang.String getUserName(java.lang.String address)
                                    throws IllegalEmailException
returns user-name from given email address if his/her email server supports VRFY command of SMTP

Parameters:
address - - email address
Returns:
- user-name if email address exist on the server, - null if user does not exist
Throws:
IllegalEmailException - if there is I/O error or SMTP server does not support VRFY command.

getListSubscribers

public static java.lang.String[] getListSubscribers(java.lang.String address)
                                             throws IllegalEmailException
returns list of subscribers for given mailing list if his/her email server supports EXPN command of SMTP

Parameters:
address - - email address
Returns:
- user-name if email address exist on the server, - null if user does not exist
Throws:
IllegalEmailException - if there is I/O error or SMTP server does not support EXPN command.

main

public static void main(java.lang.String[] args)