Reputation: 131
Suppose my email address is [email protected]
and I want to check if yahoo.com
is a valid domain or not.
Can anyone tell me which Java API I can use for this?
Upvotes: 13
Views: 14902
Reputation: 5448
Another possibility is to check the MX of the entered domain.
http://www.mxtoolbox.com/SuperTool.aspx
It is not Java API, but you can always parse the HTML response.
It means if the provider of the mail service is not blacklisted it could be safe and a real address.
But as already said, some server could always define security restriction to such service.
Another point, some services exist to provide temporary emails (mailinator.com, jetable.org, and so on...) You have to check these domains as well if you want to prevent a user to register with such an email.
UPDATE
Google provides a DNS check site which seems to be free.
An example: https://dns.google/resolve?name=amazon.com&type=MX returns a page with the following JSON:
{
"Status": 0,
"TC": false,
"RD": true,
"RA": true,
"AD": false,
"CD": false,
"Question": [
{
"name": "amazon.com.",
"type": 15
}
],
"Answer": [
{
"name": "amazon.com.",
"type": 15,
"TTL": 724,
"data": "5 amazon-smtp.amazon.com."
}
]
}
Upvotes: 3
Reputation: 341
You can use org.apache.commons.validator.routines.DomainValidator.
First add maven dependency
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
</dependency>
then:
boolean isValid = DomainValidator.getInstance().isValid("yahoo.com");
Upvotes: 1
Reputation: 90517
InetAddress
has getByName()
method to determine the IP address of a host, given the host's name.
If no IP address for the host could be found ( in case the given host name is not valid) , UnknownHostException
will be thrown.
So , you just try to catch an UnknownHostException
when calling InetAddress.getByName()
. If UnknownHostException
is caught , that means your input host name is invalid.
Upvotes: 12
Reputation: 13574
One thing that you could do is trying to resolve "yahoo.com". Something like this:
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddress = InetAddress.getByName("yahoo.com");
System.out.println(inetAddress.getHostName());
System.out.println(inetAddress.getHostAddress());
}
which outputs:
yahoo.com
67.195.160.76
Upvotes: 7
Reputation: 13434
I don't know if it's the best way to this, but I've done something similar for a VB.NET program:
I just pinged the domain, and if I didn't get a reply, the domain either was offline or didn't exist.
Upvotes: -2