1

I Want to verify Email Address without sending an Email because when we know it's invalid Email, So why i send Email

2 Answers2

2

You can't...

You can only check that the domain exists or not making a DNS MX request on the domain name, and even then this will depend on your OS name resolution system (and you can't do that using pure Java unless you use a DNS library); for instance you may be in a very tightly controlled network which will not give you access to DNS but will force you to use a SMTP relay (in the similar vein that in a large number of enterprise networks you have to use a proxy for HTTP: you just can't "access the web" directly since you can't resolve hostnames.

And even if you knew your name resolution system was reliable, you can't know whether the user actually exists before you send RCPT TO to the SMTP server.

Now, if this is the syntax of the mail address you want to check, use javax.mail's InternetAddress.

fge
  • 119,121
  • 33
  • 254
  • 329
1

You can try a pattern like this:

private static final Pattern mailPattern= Pattern.compile(
        "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$"
);

if (!mailPattern.matcher(email).matches()) {
    //TODO mail address is invalid.
}

Although this only checks for the email format, not pinging server or doing any other complex steps to handle complete verification.

Mephalay
  • 32
  • 2