梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
package org.hvp.test;

import java.util.Hashtable;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

/**
 * 
@author "惠万鹏"
 *
 
*/
public class Smtp
{
    
public static String getSmtpServer(String dns, String domain)
            
throws NamingException
    {
        
/** 如果domain传入的是一个email */
        
if (domain.indexOf("@"> 0)
        {
            domain 
= domain.substring(domain.indexOf("@"+ 1);
        }
        Hashtable
<String, String> properties = new Hashtable<String, String>();
        properties.put(
"java.naming.factory.initial","com.sun.jndi.dns.DnsContextFactory");
        properties.put(
"java.naming.provider.url""dns://" + dns);
        DirContext ctx 
= new InitialDirContext(properties);
        Attributes attributes 
= ctx.getAttributes(domain, new String[] { "MX" });
        String recordMx 
= (String) attributes.get("MX").get();
        
if (recordMx != null)
        {
            recordMx 
= recordMx.substring(recordMx.indexOf(" "+ 1);
        }
        System.out.println(
"-------->" + recordMx + "<-------");
        
return recordMx;
    }
    
    
    
    
public static void main(String[] args) throws NamingException
    {
        Smtp.getSmtpServer(
"10.1.3.210""hwpok@163.com"); 
    }
}


package org.hvp.test;

import java.net.*;
import java.io.*;

public class Test {

    
private static String END_FLAG = "\r\n";

    
public static void main(String[] args) throws Exception {
        String mx 
= "163mx00.mxmail.netease.com";
        InetAddress addr 
= InetAddress.getByName(mx);
        Socket socket 
= new Socket(addr, 25);

        InputStream in 
= socket.getInputStream();
        OutputStream out 
= socket.getOutputStream();

        
// 连接成功后服务器会响应:
        response(in);

        
// 首先发送HELO命令:
        send("HELO www.javasprite.com" + END_FLAG, out);
        response(in);

        
// 然后发送发件人地址:
        send("MAIL FROM: someone@somewhere.com" + END_FLAG, out);
        response(in);

        
// 设置收件人地址:
        send("RCPT TO: hwpok@163.com" + END_FLAG, out);
        response(in);

        
// 开始发送邮件正文:
        send("DATA" + END_FLAG, out);
        response(in);

        send(
"From: someone@somewhere.com" + END_FLAG, out);
        send(
"To: hwpok@163.com" + END_FLAG, out);
        send(
"Subject: Test without smtp server" + END_FLAG, out);
        send(
"Content-Type: text/plain;" + END_FLAG, out);
        send(END_FLAG 
+ END_FLAG, out);

        
// 发送邮件正文,如果用中文,需要BASE64编码:
        send("text message body!" + END_FLAG, out);
        
// 每行以\r\n结束,不可过长,可拆成多行。

        
// 以"\r\n.\r\n"作为结束标志:
        send(END_FLAG + "." + END_FLAG, out);
        response(in);

        
// 结束并确认发送:
        send("QUIT" + END_FLAG, out);
        response(in);
        in.close();
        out.close();
        socket.close();
    }

    
public static void response(InputStream in) throws Exception {
        
byte[] buffer = new byte[102400];
        
int n = in.read(buffer);
        
if(n>0){
            String s 
= new String(buffer, 0, n);
            System.out.println(s);
        }
    }

    
public static void send(String s, OutputStream out) throws Exception {
        
byte[] buffer = s.getBytes();
        
if(buffer != null && buffer.length>0)
        {
            out.write(buffer);
            out.flush();
        }
    }
}



posted on 2008-11-28 18:30 HUIKK 阅读(206) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: