夢幻之旅

          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 閱讀(218) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 吉隆县| 哈巴河县| 高邮市| 十堰市| 昭平县| 怀远县| 伊通| 宣化县| 金沙县| 松潘县| 陇南市| 赤城县| 焉耆| 白城市| 内黄县| 崇明县| 莱阳市| 泰顺县| 崇仁县| 调兵山市| 新干县| 赞皇县| 金川县| 庆元县| 永年县| 长沙县| 芜湖市| 自贡市| 聊城市| 武山县| 阿坝县| 家居| 安图县| 始兴县| 桃园市| 乌拉特后旗| 玉龙| 景泰县| 马边| 闻喜县| 侯马市|