package
com.roadway.edserver.util;
import java.util.HashMap;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class FetchSMTP {
private static FetchSMTP smtpFetcher = new FetchSMTP();
public static HashMap<String, String> smtps = new HashMap<String, String>();
private FetchSMTP(){};
public static FetchSMTP getInstance(){
return smtpFetcher;
}
private String getSmtpByAtBackside(String atBackside) {
String smtpServer = null;
try {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.dns.DnsContextFactory");
DirContext ctx = new InitialDirContext(env);
Attributes attrsAll = ctx.getAttributes(atBackside);
Attribute attrMx = attrsAll.get("MX");
String recordMx = (String) attrMx.get();
smtpServer = recordMx.substring(recordMx.indexOf(" ") + 1);
} catch (Exception e) {}
return smtpServer;
}
public String getSmtpByEmail(String email) {
String smtp = null;
if (email != null ) {
String atBackside = email.substring(email.indexOf("@") + 1, email.length());
smtp = smtps.get(atBackside);
if (smtp == null) {
smtp = this.getSmtpByAtBackside(atBackside);
if (smtp != null) {
FetchSMTP.smtps.put(atBackside, smtp);
}
}
}
return smtp;
}
public static void main(String args[]){
FetchSMTP f = FetchSMTP.getInstance();
System.out.println(f.getSmtpByEmail("hwpok@163.com"));
}
}
import java.util.HashMap;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class FetchSMTP {
private static FetchSMTP smtpFetcher = new FetchSMTP();
public static HashMap<String, String> smtps = new HashMap<String, String>();
private FetchSMTP(){};
public static FetchSMTP getInstance(){
return smtpFetcher;
}
private String getSmtpByAtBackside(String atBackside) {
String smtpServer = null;
try {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.dns.DnsContextFactory");
DirContext ctx = new InitialDirContext(env);
Attributes attrsAll = ctx.getAttributes(atBackside);
Attribute attrMx = attrsAll.get("MX");
String recordMx = (String) attrMx.get();
smtpServer = recordMx.substring(recordMx.indexOf(" ") + 1);
} catch (Exception e) {}
return smtpServer;
}
public String getSmtpByEmail(String email) {
String smtp = null;
if (email != null ) {
String atBackside = email.substring(email.indexOf("@") + 1, email.length());
smtp = smtps.get(atBackside);
if (smtp == null) {
smtp = this.getSmtpByAtBackside(atBackside);
if (smtp != null) {
FetchSMTP.smtps.put(atBackside, smtp);
}
}
}
return smtp;
}
public static void main(String args[]){
FetchSMTP f = FetchSMTP.getInstance();
System.out.println(f.getSmtpByEmail("hwpok@163.com"));
}
}