Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to validate MX record (#125)
- Loading branch information
1 parent
a0daa5b
commit 4b765dc
Showing
9 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.sanctionco.jmail.dns; | ||
|
||
import java.util.Hashtable; | ||
|
||
import javax.naming.NamingException; | ||
import javax.naming.directory.Attribute; | ||
import javax.naming.directory.DirContext; | ||
import javax.naming.directory.InitialDirContext; | ||
|
||
/** | ||
* Utility class that provides static methods for DNS related operations. | ||
*/ | ||
public final class DNSLookupUtil { | ||
|
||
/** | ||
* Private constructor to prevent instantiation. | ||
*/ | ||
private DNSLookupUtil() { | ||
} | ||
|
||
/** | ||
* Determine if the given domain has a valid MX record. | ||
* | ||
* @param domain the domain whose MX record to check | ||
* @return true if the domain has a valid MX record, or false if it does not | ||
*/ | ||
public static boolean hasMXRecord(String domain) { | ||
Hashtable<String, String> env = new Hashtable<>(); | ||
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); | ||
|
||
try { | ||
DirContext ctx = new InitialDirContext(env); | ||
Attribute attr = ctx.getAttributes(domain, new String[]{"MX"}).get("MX"); | ||
|
||
return attr != null && attr.size() > 0; | ||
} catch (NamingException e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/test/java/com/sanctionco/jmail/dns/DNSLookupUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.sanctionco.jmail.dns; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class DNSLookupUtilTest { | ||
|
||
@Test | ||
void canLookupValidMXRecord() { | ||
assertThat(DNSLookupUtil.hasMXRecord("gmail.com")).isTrue(); | ||
assertThat(DNSLookupUtil.hasMXRecord("yahoo.com")).isTrue(); | ||
assertThat(DNSLookupUtil.hasMXRecord("hotmail.com")).isTrue(); | ||
assertThat(DNSLookupUtil.hasMXRecord("utexas.edu")).isTrue(); | ||
} | ||
|
||
@Test | ||
void failsToFindInvalidMXRecord() { | ||
assertThat(DNSLookupUtil.hasMXRecord("a.com")).isFalse(); | ||
assertThat(DNSLookupUtil.hasMXRecord("whatis.hello")).isFalse(); | ||
} | ||
} |