import java.net.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.naming.Directory.*;public class Smtp {
public static void main(String[] args) throws Exception {
// DNS服务器,看看本机的DNS配置
String dns = "dns://192.168.1.1";
// 邮箱后缀:
String domain = "sohu.com";
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.PRovider.url", dns);
DirContext ctx = new InitialDirContext(env);
Attributes attr = ctx.getAttributes(domain, new String[]{"MX" });
NamingEnumeration servers = attr.getAll();
// 列出所有邮件服务器:
while(servers.hasMore()) {
System.out.println(servers.next());
}
}
}
第二步:直接连接邮件服务器的25端口,用SMTP协议发送邮件。
这里使用sohu信箱,邮件服务器为sohumx.sohu.com,收信人必须在此服务器上:
import java.net.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;public class Smtp {
private static String END_FLAG = "/r/n";
public static void main(String[] args) throws Exception {
String mx = "sohumx.sohu.com";
InetAddress addr = InetAddress.getByName(mx);
Socket socket = new Socket(addr, 25);InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
新闻热点
疑难解答