You can find STMP server configured in hostname, in ask to dns of domain who is mailserver, you need to find MX records :
- look a DNS config : NS (name server) , CNAME (Alias) , MX (mailexchange) etc...
import java.util.ArrayList;
import java.util.Hashtable;
import javax.naming.*;
import javax.naming.directory.*;
public class MXLookup
{
public static void main(String args[]) throws NamingException
{
System.out.println(getMX("altern.org") + " mail servers");
}
private static ArrayList getMX(String hostName) throws NamingException
{
// Perform a DNS lookup for MX records in the domain
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
DirContext ictx = new InitialDirContext(env);
Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
Attribute attr = attrs.get("MX");
// if we don't have an MX record, try the machine itself
if ((attr == null) || (attr.size() == 0))
{
attrs = ictx.getAttributes(hostName, new String[] { "A" });
attr = attrs.get("A");
if (attr == null) throw new NamingException("No match for name '" + hostName + "'");
}
ArrayList res = new ArrayList();
NamingEnumeration en = attr.getAll();
while (en.hasMore())
{
String x = (String) en.next();
String f[] = x.split(" ");
if (f[1].endsWith(".")) f[1] = f[1].substring(0, (f[1].length() - 1));
res.add(f[1]);
}
return res;
}
}