---
title: "电子邮件那点事儿"
author: "hwpok"
date: "2008-11-28T18:30:00Z"
tags: [".net", "SUN", "Socket"]
slug: "untitled"
draft: false
lastmod: "2008-11-28T18:30:00Z"
id: 615788
url: "https://hwpok.iteye.com/blog/615788"
---

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;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("@") &gt; 0)  
        {  
            domain = domain.substring(domain.indexOf("@") + 1);  
        }  
        Hashtable&lt;String, String&gt; properties = new Hashtable&lt;String, String&gt;();  
        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("--------&gt;" + recordMx + "&lt;-------");  
        return recordMx;  
    }  
      
      
      
    public static void main(String\[] args) throws NamingException  
    {  
        Smtp.getSmtpServer("10.1.3.210", "hwpok@163.com");   
    }  
}

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;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&gt;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&gt;0)  
        {  
            out.write(buffer);  
            out.flush();  
        }  
    }  
}