验证Java中的电子邮件 [英] Validating email in Java

查看:86
本文介绍了验证Java中的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含姓名,电子邮件和密码的注册表.注册时,确认链接将发送到用户的电子邮件地址.但是在发送链接之前,我需要验证电子邮件地址.我用过:

I have a registration form which has name, email and password. A confirmation link is sent to user's email address on registering. But before sending links I need to validate email address. I used:

public static boolean verifyEmailAddress(String regEmail) {
    boolean result = true;
    try {
        InternetAddress emailAddr = new InternetAddress(regEmail);
        emailAddr.validate();
    } catch (AddressException ex) {
        result = false;
    }
    return result;
}

任何显示有效的电子邮件,例如yutr6767868@huymail.comk999////@sdmail.com

Any of the email it shows valid, example yutr6767868@huymail.com, k999////@sdmail.com

我从request.getParameter收到的电子邮件并将其存储在regEmail中.

The email I am getting from request.getParameter and storing it in regEmail.

我到底需要做些什么才能使其正常工作?

What exactly i need to do to make it work ?

推荐答案

使用 Apache Commons Validator .检查 EmailValidator

您可以像以下示例一样使用它:

You can use it like this example:

 EmailValidator validator = EmailValidator.getInstance();
 boolean validEmail = validator.isValid(email);

这篇关于验证Java中的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆