乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      JAVA實(shí)現(xiàn)簡易的郵件客戶端程序

       燮羽 2010-12-10
      Java代碼 
      1. /** 
      2.  * 簡易的郵件客戶端程序,后期將逐步完善提供友好UI 
      3.  * 測試發(fā)現(xiàn)不支持Gmail,Gmail SMTP采用了TSL/SSL協(xié)議 
      4.  * 已知JavaMail可實(shí)現(xiàn)SSL協(xié)議。。。 
      5.  */  
      6. package mail;  
      7.   
      8. /** 
      9.  * @author Daniel Cheng 
      10.  * 
      11.  */  
      12. import java.io.BufferedReader;  
      13. import java.io.BufferedWriter;  
      14. import java.io.IOException;  
      15. import java.io.InputStreamReader;  
      16. import java.io.OutputStreamWriter;  
      17. import java.net.Socket;  
      18. import java.net.SocketException;  
      19. import java.net.UnknownHostException;  
      20. import java.util.StringTokenizer;  
      21.   
      22. public class SMTPClient {  
      23.   
      24.     private boolean debug = true;  
      25.     BASE64Encoder encode = new BASE64Encoder();// 自定義BASE64Encoder工具類用于加密字符串  
      26.   
      27.     public static void main(String[] args) throws UnknownHostException,  
      28.             IOException {  
      29.         MailMessage message = new MailMessage();  
      30.         message.setFrom("test@163.com");// 發(fā)件人  
      31.         message.setTo("test@hotmail.com,test@gmail.com,test@sina.com");// 多個(gè)收件人地址間用逗號隔開  
      32.         String server = "smtp.163.com";// SMTP郵件服務(wù)器  
      33.         message.setSubject("Java Mail Test");// 郵件主題  
      34.         message.setContent("你好!這里是系統(tǒng)發(fā)出的JAVA MAIL測試,請勿回復(fù)。");// 郵件內(nèi)容  
      35.         message.setDataFrom("Sender");// 發(fā)件人,在郵件的發(fā)件人欄目中顯示  
      36.         message.setDataTo("Receiver");// 收件人,在郵件的收件人欄目中顯示  
      37.         message.setUser("test@163.com");// 登陸的郵箱賬號  
      38.         message.setPassword("********");// 登陸郵箱的密碼,建議自己保密好,公開傳播會泄密  
      39.   
      40.         SMTPClient smtp = new SMTPClient(server, 25);  
      41.         boolean flag;  
      42.         flag = smtp.sendMail(message, server);  
      43.         if (flag) {  
      44.             System.out.println("郵件發(fā)送成功!");  
      45.         } else {  
      46.             System.out.println("郵件發(fā)送失敗!");  
      47.         }  
      48.     }  
      49.   
      50.     private Socket socket;  
      51.   
      52.     public SMTPClient(String server, int port) throws UnknownHostException,  
      53.             IOException {  
      54.         try {  
      55.             socket = new Socket(server, 25);  
      56.         } catch (SocketException e) {  
      57.             System.out.println(e.getMessage());  
      58.         } catch (Exception e) {  
      59.             e.printStackTrace();  
      60.         } finally {  
      61.             System.out.println("已經(jīng)建立連接!");  
      62.         }  
      63.   
      64.     }  
      65.   
      66.     // 注冊到郵件服務(wù)器  
      67.     public void helo(String server, BufferedReader in, BufferedWriter out)  
      68.             throws IOException {  
      69.         int result;  
      70.         result = getResult(in);  
      71.         // 連接上郵件服務(wù)后,服務(wù)器給出220應(yīng)答  
      72.         if (result != 220) {  
      73.             throw new IOException("連接服務(wù)器失敗");  
      74.         }  
      75.         result = sendServer("HELO " + server, in, out);  
      76.         // HELO命令成功后返回250  
      77.         if (result != 250) {  
      78.             throw new IOException("注冊郵件服務(wù)器失??!");  
      79.         }  
      80.     }  
      81.   
      82.     private int sendServer(String str, BufferedReader in, BufferedWriter out)  
      83.             throws IOException {  
      84.         out.write(str);  
      85.         out.newLine();  
      86.         out.flush();  
      87.         if (debug) {  
      88.             System.out.println("已發(fā)送命令:" + str);  
      89.         }  
      90.         return getResult(in);  
      91.     }  
      92.   
      93.     public int getResult(BufferedReader in) {  
      94.         String line = "";  
      95.         try {  
      96.             line = in.readLine();  
      97.             if (debug) {  
      98.                 System.out.println("服務(wù)器返回狀態(tài):" + line);  
      99.             }  
      100.         } catch (Exception e) {  
      101.             e.printStackTrace();  
      102.         }  
      103.         // 從服務(wù)器返回消息中讀出狀態(tài)碼,將其轉(zhuǎn)換成整數(shù)返回  
      104.         StringTokenizer st = new StringTokenizer(line, " ");  
      105.         return Integer.parseInt(st.nextToken());  
      106.     }  
      107.   
      108.     public void authLogin(MailMessage message, BufferedReader in,  
      109.             BufferedWriter out) throws IOException {  
      110.         int result;  
      111.         result = sendServer("AUTH LOGIN", in, out);  
      112.         if (result != 334) {  
      113.             throw new IOException("用戶驗(yàn)證失?。?);  
      114.         }  
      115.   
      116.         result = sendServer(encode.encode(message.getUser().getBytes()), in,  
      117.                 out);  
      118.         if (result != 334) {  
      119.             throw new IOException("錯(cuò)誤!");  
      120.         }  
      121.         result = sendServer(encode.encode(message.getPassword().getBytes()),  
      122.                 in, out);  
      123.   
      124.         if (result != 235) {  
      125.             throw new IOException("驗(yàn)證失??!");  
      126.         }  
      127.     }  
      128.   
      129.     // 開始發(fā)送消息,郵件源地址  
      130.     public void mailFrom(String source, BufferedReader in, BufferedWriter out)  
      131.             throws IOException {  
      132.         int result;  
      133.         result = sendServer("MAIL FROM:<" + source + ">", in, out);  
      134.         if (result != 250) {  
      135.             throw new IOException("指定源地址錯(cuò)誤");  
      136.         }  
      137.     }  
      138.   
      139.     // 設(shè)置郵件收件人。多郵件發(fā)送用","隔開  
      140.     public void rcpt(String touchman, BufferedReader in, BufferedWriter out)  
      141.             throws IOException {  
      142.   
      143.         String[] mailList = touchman.split(",");  
      144.         if (mailList.length > 1) {  
      145.             for (String touch : mailList) {  
      146.                 connectionServer(touch,in,out);  
      147.             }  
      148.         }else  
      149.             connectionServer(touchman,in,out);  
      150.   
      151.     }  
      152.   
      153.     private void connectionServer(String touch, BufferedReader in, BufferedWriter out)  
      154.             throws IOException {  
      155.         int result;  
      156.         result = sendServer("RCPT TO:<" + touch + ">", in, out);  
      157.         if (result != 250) {  
      158.             throw new IOException("指定目的地址錯(cuò)誤!");  
      159.         }  
      160.     }  
      161.   
      162.     // 郵件體  
      163.     public void data(String from, String to, String subject, String content,  
      164.             BufferedReader in, BufferedWriter out) throws IOException {  
      165.         int result;  
      166.         result = sendServer("DATA", in, out);  
      167.         // 輸入DATA回車后,若收到354應(yīng)答后,繼續(xù)輸入郵件內(nèi)容  
      168.         if (result != 354) {  
      169.             throw new IOException("不能發(fā)送數(shù)據(jù)");  
      170.         }  
      171.         out.write("From: " + from);  
      172.         out.newLine();  
      173.         out.write("To: " + to);  
      174.         out.newLine();  
      175.         out.write("Subject: " + subject);  
      176.         out.newLine();  
      177.         out.newLine();  
      178.         out.write(content);  
      179.         out.newLine();  
      180.         // 句號加回車結(jié)束郵件內(nèi)容輸入  
      181.         result = sendServer(".", in, out);  
      182.         System.out.println(result);  
      183.         if (result != 250) {  
      184.             throw new IOException("發(fā)送數(shù)據(jù)錯(cuò)誤");  
      185.         }  
      186.     }  
      187.   
      188.     // 退出  
      189.     public void quit(BufferedReader in, BufferedWriter out) throws IOException {  
      190.         int result;  
      191.         result = sendServer("QUIT", in, out);  
      192.         if (result != 221) {  
      193.             throw new IOException("未能正確退出");  
      194.         }  
      195.     }  
      196.   
      197.     // 發(fā)送郵件主程序  
      198.     public boolean sendMail(MailMessage message, String server) {  
      199.         try {  
      200.             BufferedReader in = new BufferedReader(new InputStreamReader(socket  
      201.                     .getInputStream()));  
      202.             BufferedWriter out = new BufferedWriter(new OutputStreamWriter(  
      203.                     socket.getOutputStream()));  
      204.             helo(server, in, out);// HELO命令  
      205.             authLogin(message, in, out);// AUTH LOGIN命令  
      206.             mailFrom(message.getFrom(), in, out);// MAIL FROM  
      207.             rcpt(message.getTo(), in, out);// RCPT  
      208.             data(message.getDataFrom(), message.getDataTo(), message  
      209.                     .getSubject(), message.getContent(), in, out);// DATA  
      210.             quit(in, out);// QUIT  
      211.         } catch (Exception e) {  
      212.             e.printStackTrace();  
      213.             return false;  
      214.   
      215.         }  
      216.         return true;  
      217.     }  
      218.   
      219. }  

       

      Java代碼 
      1. /** 
      2.  * 郵件實(shí)體POJO類 
      3.  */  
      4. package mail;  
      5.   
      6. /** 
      7.  * @author Daniel Cheng 
      8.  * 
      9.  */  
      10. public class MailMessage {  
      11.     private String from;  
      12.     private String to;  
      13.     private String subject;  
      14.     private String content;  
      15.     private String dataFrom;  
      16.     private String dataTo;  
      17.     private String user;  
      18.     private String password;  
      19.     /** 
      20.      *  
      21.      */  
      22.     public MailMessage() {  
      23.         super();  
      24.         // TODO Auto-generated constructor stub  
      25.     }  
      26.     /** 
      27.      * @param from 
      28.      * @param to 
      29.      * @param subject 
      30.      * @param content 
      31.      * @param dataFrom 
      32.      * @param dataTo 
      33.      * @param user 
      34.      * @param password 
      35.      */  
      36.     public MailMessage(String from, String to, String subject, String content,  
      37.             String dataFrom, String dataTo, String user, String password) {  
      38.         super();  
      39.         this.from = from;  
      40.         this.to = to;  
      41.         this.subject = subject;  
      42.         this.content = content;  
      43.         this.dataFrom = dataFrom;  
      44.         this.dataTo = dataTo;  
      45.         this.user = user;  
      46.         this.password = password;  
      47.     }  
      48.     public String getFrom() {  
      49.         return from;  
      50.     }  
      51.     public void setFrom(String from) {  
      52.         this.from = from;  
      53.     }  
      54.     public String getTo() {  
      55.         return to;  
      56.     }  
      57.     public void setTo(String to) {  
      58.         this.to = to;  
      59.     }  
      60.     public String getSubject() {  
      61.         return subject;  
      62.     }  
      63.     public void setSubject(String subject) {  
      64.         this.subject = subject;  
      65.     }  
      66.     public String getContent() {  
      67.         return content;  
      68.     }  
      69.     public void setContent(String content) {  
      70.         this.content = content;  
      71.     }  
      72.     public String getDataFrom() {  
      73.         return dataFrom;  
      74.     }  
      75.     public void setDataFrom(String dataFrom) {  
      76.         this.dataFrom = dataFrom;  
      77.     }  
      78.     public String getDataTo() {  
      79.         return dataTo;  
      80.     }  
      81.     public void setDataTo(String dataTo) {  
      82.         this.dataTo = dataTo;  
      83.     }  
      84.     public String getUser() {  
      85.         return user;  
      86.     }  
      87.     public void setUser(String user) {  
      88.         this.user = user;  
      89.     }  
      90.     public String getPassword() {  
      91.         return password;  
      92.     }  
      93.     public void setPassword(String password) {  
      94.         this.password = password;  
      95.     }  
      96.       
      97.   
      98. }  

       

      Java代碼 
      1. /** 
      2.  * BASE64Encoder加密類 
      3.  */  
      4. package mail;  
      5.   
      6. /** 
      7.  * @author Daniel Cheng 
      8.  * 
      9.  */  
      10. public class BASE64Encoder {  
      11.     private static char[] codec_table = { 'A''B''C''D''E''F''G',  
      12.         'H''I''J''K''L''M''N''O''P''Q''R''S''T',  
      13.         'U''V''W''X''Y''Z''a''b''c''d''e''f''g',  
      14.         'h''i''j''k''l''m''n''o''p''q''r''s''t',  
      15.         'u''v''w''x''y''z''0''1''2''3''4''5''6',  
      16.         '7''8''9''+''/' };  
      17.   
      18. public BASE64Encoder() {  
      19.   
      20. }  
      21.   
      22. public String encode(byte[] a) {  
      23.     int totalBits = a.length * 8;  
      24.     int nn = totalBits % 6;  
      25.     int curPos = 0;// process bits  
      26.     StringBuffer toReturn = new StringBuffer();  
      27.     while (curPos < totalBits) {  
      28.         int bytePos = curPos / 8;  
      29.         switch (curPos % 8) {  
      30.         case 0:  
      31.             toReturn.append(codec_table[(a[bytePos] & 0xfc) >> 2]);  
      32.             break;  
      33.         case 2:  
      34.   
      35.             toReturn.append(codec_table[(a[bytePos] & 0x3f)]);  
      36.             break;  
      37.         case 4:  
      38.             if (bytePos == a.length - 1) {  
      39.                 toReturn  
      40.                         .append(codec_table[((a[bytePos] & 0x0f) << 2) & 0x3f]);  
      41.             } else {  
      42.                 int pos = (((a[bytePos] & 0x0f) << 2) | ((a[bytePos + 1] & 0xc0) >> 6)) & 0x3f;  
      43.                 toReturn.append(codec_table[pos]);  
      44.             }  
      45.             break;  
      46.         case 6:  
      47.             if (bytePos == a.length - 1) {  
      48.                 toReturn  
      49.                         .append(codec_table[((a[bytePos] & 0x03) << 4) & 0x3f]);  
      50.             } else {  
      51.                 int pos = (((a[bytePos] & 0x03) << 4) | ((a[bytePos + 1] & 0xf0) >> 4)) & 0x3f;  
      52.                 toReturn.append(codec_table[pos]);  
      53.             }  
      54.             break;  
      55.         default:  
      56.             //never hanppen  
      57.             break;  
      58.         }  
      59.         curPos+=6;  
      60.     }  
      61.     if(nn==2)  
      62.     {  
      63.         toReturn.append("==");  
      64.     }  
      65.     else if(nn==4)  
      66.     {  
      67.         toReturn.append("=");  
      68.     }  
      69.     return toReturn.toString();  
      70.   
      71. }  
      72.   
      73. }  

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多