俊星的BLOG

          #

          JAVA MAIL之試用JAMES

          JAMES是一個純java的郵件服務器,APACHE出品,關于JAMES的更多內容請查看官方網站的說明。
          1)、配置config.xml(apps\james\SAR-INF)文件:
          <servernames autodetect="true" autodetectIP="true">
              
          <servername>test.com</servername>
          </servernames>

          <dnsserver>
              
          <servers>
                  
          <!--Enter ip address of your DNS server, one IP address per server -->
                  
          <server>127.0.0.1</server>
                  
          <server>211.148.192.136</server>
              
          </servers>
          </dnsserver>
          2)、啟動JAMSES 服務器:
          運行bin\run.bat,輸出如下:
          Using PHOENIX_HOME:   F:\dev\james-2.3.1
          Using PHOENIX_TMPDIR: F:\dev\james-
          2.3.1\temp
          Using JAVA_HOME:      D:\Program Files\Java\jdk1
          .5.0_16

          Phoenix 
          4.2

          James Mail Server 
          2.3.1
          Remote Manager Service started plain:
          4555
          POP3 Service started plain:
          110
          SMTP Service started plain:
          25
          NNTP Service started plain:
          119
          FetchMail Disabled

          3)創建一個用戶test:
          命令“telnet localhost 4555”,交互過程如下:
          JAMES Remote Administration Tool 2.3.1
          Please enter your login and password
          Login id:
          root
          Password:
          root
          Welcome root. HELP for a list of commands
          adduser test test
          User test added
          quit
          Bye

          4)發送一封郵件:
          命令“telnet localhost 25”,交互過程:
          220 kinkding-d1d01d SMTP Server (JAMES SMTP Server 2.3.1) ready Sat, 25 Apr 2009 11:07:52 +0800 (CS
          ehlo test.com
          250-kinkding-d1d01d Hello test.com (localhost [127.0.0.1])
          250-PIPELINING
          250 ENHANCEDSTATUSCODES
          mail from:<foo@test.com>
          250 2.1.0 Sender <foo@test.com> OK
          rcpt to:<test@test.com>
          250 2.1.5 Recipient <test@test.com> OK
          data
          354 Ok Send data ending with <CRLF>.<CRLF>
          subject:this is hello world email
          har ^_^ good haha
          .
          250 2.6.0 Message received
          quit
          221 2.0.0 kinkding-d1d01d Service closing transmission channel

          5)到服務器查看郵件內容:
          F:\dev\james-2.3.1\apps\james\var\mail\inboxes\test>ls
          4D61696C313234303632393039333837352D30.Repository.FileObjectStore
          4D61696C313234303632393039333837352D30.Repository.FileStreamStore
          F:\dev\james-
          2.3.1\apps\james\var\mail\inboxes\test>more 4D61696C313234303632393039333837352D30.Repos*StreamStore
          Return-Path: <foo@test.com>
          Message-ID: <
          24964246.01240629093921.JavaMail.kinkding@kinkding-d1d01d>
          MIME-Version: 
          1.0
          Content-Type: text/plain
          ; charset=us-ascii
          Content-Transfer-Encoding: 7bit
          Delivered-To: test@test.com
          Received: from localhost (
          [127.0.0.1])
                    by kinkding-d1d01d (JAMES SMTP Server 
          2.3.1) with SMTP ID 760
                    for <test@test.com>
          ;
                    Sat, 25 Apr 2009 11:11:33 +0800 (CST)
          Date: Sat
          , 25 Apr 2009 11:11:33 +0800 (CST)
          From: foo@test.com
          subject:this is hello world email
          har ^_^ good haha

          posted @ 2009-04-25 11:51 俊星 閱讀(640) | 評論 (0)編輯 收藏

          JAVA MAIL之我的搜索

               摘要: JAVAMAIL中提供搜索郵件的API,具體用法可以參考之前的博文,下面是我對其源代碼的學習: 1、運行實例: package mysearch; import java.util.ArrayList; import java.util.Date; import java.util.List; public class ...  閱讀全文

          posted @ 2009-04-24 22:09 俊星 閱讀(249) | 評論 (0)編輯 收藏

          JAVA MAIL之BASE64編碼解碼

               摘要: 關于BASE64編碼,建議參看WIKI中的相關說明。 1、編碼:     public static byte[] base64encode(byte[] inbuf) {         int size&n...  閱讀全文

          posted @ 2009-04-23 23:29 俊星 閱讀(775) | 評論 (0)編輯 收藏

          JAVA MAIL之email地址處理

               摘要: 對于email地址的定義,可以參考RFC822,里面有詳細的說明。 1、采用正則表達式的方式來驗證email地址: JS處理方式(來自javascript.internet.com的Sandeep V. Tamhankar): function checkEmail(emailStr) {    if (emailStr.leng...  閱讀全文

          posted @ 2009-04-23 21:16 俊星 閱讀(681) | 評論 (0)編輯 收藏

          JAVA MAIL之搜索郵件

          1、主要代碼:

              /**
               * 搜索郵件
               
          */

              
          public static void search(String subject, String from, boolean or) throws Exception {
                  Session session 
          = Session.getDefaultInstance(System.getProperties(), null);
                  
          // session.setDebug(true);
                  Store store = session.getStore(new URLName("imap://test:test@127.0.0.1"));
                  store.connect();
                  Folder folder 
          = store.getDefaultFolder();
                  
          // 在收件箱中搜索
                  folder = folder.getFolder("INBOX");
                  folder.open(Folder.READ_ONLY);
                  List
          <SearchTerm> terms = new ArrayList<SearchTerm>();
                  
          // 按主題查詢
                  terms.add(new SubjectTerm(subject));
                  
          // 按發件人查詢
                  terms.add(new FromStringTerm(from));
                  
          // 一個小時內的郵件(我本地的Megic Winmail郵件服務器查不到內容)
                  
          // long time = System.currentTimeMillis();
                  
          // SentDateTerm dateTerm = new SentDateTerm(ComparisonTerm.GE, new Date(
                  
          // time - 60 * 60 * 1000));
                  
          // terms.add(dateTerm);
                  SearchTerm arrays[] = new SearchTerm[terms.size()];
                  terms.toArray(arrays);
                  SearchTerm term 
          = or ? new OrTerm(arrays) : new AndTerm(arrays);
                  Message[] msgs 
          = folder.search(term);
                  System.out.println(
          "FOUND " + msgs.length + " MESSAGES");
                  
          for (int i = 0; i < msgs.length; i++{
                      System.out.println(
          "--------------------------");
                      System.out.println(
          "MESSAGE #" + (i + 1+ ":");
                      dumpEnvelope(msgs[i]);
                  }

              }


              
          /**
               * 打印郵件的內容
               * 
               * 
          @param m
               * 
          @throws Exception
               
          */

              
          public static void dumpEnvelope(Message m) throws Exception {
                  Address[] a;
                  
          if ((a = m.getFrom()) != null{
                      
          for (int j = 0; j < a.length; j++)
                          System.out.println(
          "FROM: " + a[j].toString());
                  }

                  
          if ((a = m.getRecipients(Message.RecipientType.TO)) != null{
                      
          for (int j = 0; j < a.length; j++{
                          System.out.println(
          "TO: " + a[j].toString());
                      }

                  }

                  System.out.println(
          "SUBJECT: " + m.getSubject());
                  Date d 
          = m.getSentDate();
                  System.out.println(
          "SendDate: " + (d != null ? d.toString() : "UNKNOWN"));
              }


              
          public static void main(String[] args) {
                  
          try {
                      search(
          "subject""test2@test.com"false);
                      System.out.println(
          "\n");
                      search(
          "Fw: test""test2@test.com"false);
                      System.out.println(
          "\n");
                      search(
          "null""test2@test.com"true);
                  }
           catch (Exception e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

              }

          2、測試輸出:
          FOUND 0 MESSAGES


          FOUND 
          1 MESSAGES
          --------------------------
          MESSAGE #
          1:
          FROM: test2 
          <test2@test.com>
          TO: test 
          <test@test.com>
          SUBJECT: Fw: test
          SendDate: Tue Apr 
          21 20:38:23 CST 2009


          FOUND 
          2 MESSAGES
          --------------------------
          MESSAGE #
          1:
          FROM: test2 
          <test2@test.com>
          TO: test 
          <test@test.com>
          SUBJECT: 測試郵件
          SendDate: Mon Apr 
          20 21:42:53 CST 2009
          --------------------------
          MESSAGE #
          2:
          FROM: test2 
          <test2@test.com>
          TO: test 
          <test@test.com>
          SUBJECT: Fw: test
          SendDate: Tue Apr 
          21 20:38:23 CST 2009

          3、相關說明:
          如果采用debug模式的話,可以看到調用和搜索串之間的對應關系:
          第一次:SEARCH SUBJECT subject FROM test2@test.com ALL
          第二次:SEARCH SUBJECT "Fw: test" FROM test2@test.com ALL
          第三次:SEARCH OR SUBJECT null FROM test2@test.com ALL

          posted @ 2009-04-21 20:33 俊星 閱讀(261) | 評論 (0)編輯 收藏

          僅列出標題
          共10頁: First 上一頁 2 3 4 5 6 7 8 9 10 
          主站蜘蛛池模板: 区。| 凤山县| 辽宁省| 海伦市| 南投市| 景泰县| 姚安县| 吴忠市| 武乡县| 合肥市| 兴业县| 新蔡县| 峨眉山市| 吉木萨尔县| 大悟县| 桐梓县| 邵阳县| 蚌埠市| 泽州县| 永昌县| 江永县| 象山县| 阿拉善盟| 阳原县| 大理市| 茌平县| 门头沟区| 宾阳县| 容城县| 辉县市| 军事| 新丰县| 介休市| 宁德市| 洱源县| 平利县| 噶尔县| 宣化县| 正蓝旗| 定南县| 荣成市|