Example usage for org.apache.commons.validator EmailValidator isValid

List of usage examples for org.apache.commons.validator EmailValidator isValid

Introduction

In this page you can find the example usage for org.apache.commons.validator EmailValidator isValid.

Prototype

public boolean isValid(String email) 

Source Link

Document

Checks if a field has a valid e-mail address.

Usage

From source file:org.sakaiproject.contentreview.impl.urkund.UrkundReviewServiceImpl.java

/**
 * Is this a valid email the service will recognize
 *
 * @param email/*from  w  ww. j  a  v  a2 s.c o  m*/
 * @return
 */
private boolean isValidEmail(String email) {

    if (email == null || email.equals("")) {
        return false;
    }

    email = email.trim();
    //must contain @
    if (!email.contains("@")) {
        return false;
    }

    //an email can't contain spaces
    if (email.indexOf(" ") > 0) {
        return false;
    }

    //use commons-validator
    EmailValidator validator = EmailValidator.getInstance();
    return validator.isValid(email);
}

From source file:org.sakaiproject.qna.logic.impl.NotificationLogicImpl.java

/**
 * @see NotificationLogic#sendNewAnswerNotification(String[], QnaQuestion,
 *      String)/*from  w  w  w.  j a  va2s.  c o m*/
 */
public void sendNewQuestionNotification(String[] emails, QnaQuestion question, String fromUserId) {
    EmailValidator emailValidator = EmailValidator.getInstance();
    String fromEmail = externalLogic.getUserEmail(fromUserId);
    if (emailValidator.isValid(fromEmail)) {
        externalLogic.sendEmails(buildFrom(externalLogic.getUserDisplayName(fromUserId), fromEmail), emails,
                buildNewQuestionSubject(), buildNewQuestionMessage(question));
    } else {
        sendNewQuestionNotification(emails, question);
    }
}

From source file:org.sakaiproject.qna.logic.impl.OptionsLogicImpl.java

/**
 * Processes a comma separated list of emails and adds it to a {@link QnaOptions} object
 * //from  w w w .  j a  v  a  2s.  c  o m
 * @param mailList String of comma separated values of emails
 * @param options The {@link QnaOptions} object custom mails must be added to
 * @param userId Sakai user id to use
 * @return boolean if there were any invalid mails
 */
private boolean processCustomMailList(String mailList, QnaOptions options, String userId) {
    EmailValidator emailValidator = EmailValidator.getInstance();
    boolean invalidEmail = false;

    if (mailList != null && !mailList.trim().equals("")) {
        String[] emails = mailList.split(",");
        for (int i = 0; i < emails.length; i++) {
            if (!emailValidator.isValid(emails[i].trim())) {
                invalidEmail = true;
            } else {
                QnaCustomEmail customEmail = new QnaCustomEmail(userId, emails[i].trim(), new Date());
                options.addCustomEmail(customEmail);
            }
        }
    }
    return invalidEmail;
}

From source file:util.Check.java

/**
 * Checks if a string is a valid email address.
 * /*from   w w  w  .j  a v  a  2s . c o  m*/
 * @param email
 * @return
 */
public boolean isEmail(final String email) {
    boolean check = false;
    try {
        final InternetAddress ia = new InternetAddress();
        ia.setAddress(email);
        try {
            ia.validate(); // throws an error for invalid addresses and null input, but does not catch all errors
            // we are using apache commons email validator
            final EmailValidator validator = EmailValidator.getInstance();
            check = validator.isValid(email);

            // EmailValidator is agnostic about valid domain names...
            // ...we do an additional check
            final Pattern p = Pattern.compile("@[a-z0-9-]+(\\.[a-z0-9-]+)*\\"
                    + ".([a-z]{2}|aero|arpa|asia|biz|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|nato|net|"
                    + "org|pro|tel|travel|xxx)$\\b");
            // we need to match against lower case for the above regex
            final Matcher m = p.matcher(email.toLowerCase());
            if (!m.find()) {
                // reset to false, if we do not have a match
                check = false;
            }

        } catch (final AddressException e1) {
            LOG.info("isEmail: " + email + " " + e1.toString());
        }
    } catch (final Exception e) {
        LOG.error("isEmail: " + email + " " + e.toString());
    }

    return check;
}