產(chǎn)品需求
判斷用戶輸入的郵箱地址其域名是否真實存在霸株,比如zhangsan@163.com
郵箱域名163.com
是否是一個真實存在的郵箱域名。
實現(xiàn)方案
可以通過DNS域名系統(tǒng)查詢mx交互記錄
仿贬,引入 maven:
<!-- https://mvnrepository.com/artifact/dnsjava/dnsjava -->
<dependency>
<groupId>dnsjava</groupId>
<artifactId>dnsjava</artifactId>
<version>2.1.8</version>
</dependency>
代碼示例:
package com.yzy.test;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.Type;
public class DNSTest {
public static void main(String[] args) throws Exception {
Lookup lookup = new Lookup("163.com", Type.MX);
lookup.run();
if (lookup.getResult() != Lookup.SUCCESSFUL) {
System.err.println("ERROR: " + lookup.getErrorString());
return;
}
Record[] answers = lookup.getAnswers();
for (Record rec : answers) {
System.out.println(rec.toString());
}
}
}
控制臺輸出:
163.com. 316 IN MX 10 163mx02.mxmail.netease.com.
163.com. 316 IN MX 10 163mx03.mxmail.netease.com.
163.com. 316 IN MX 50 163mx00.mxmail.netease.com.
163.com. 316 IN MX 10 163mx01.mxmail.netease.com.
說明163.com
是真實可用的郵箱域名