利用jakarta Commons組件收發郵件

          發郵件:

          Telnet to port 25 of a SMTP server, and you will see the server respond with a numeric code (220). SMTPReply.isPositiveCompletion( ) returns true if the response code of the previously executed command is between 200 and 299; the value of the initial response code, 220, is equal to the public static variable SMTPReply.SERVICE_READY. The following example uses getReplyCode( ) and SMTPReply.isPositiveCompletion() to test the connection to the SMTP server:

          import org.apache.commons.net.smtp.SMTP;
          import org.apache.commons.net.smtp.SMTPClient;
          import org.apache.commons.net.smtp.SMTPReply;
          SMTPClient client = new SMTPClient( );
          client.connect("www.discursive.com");
          int response = client.getReplyCode( );
          if( SMTPReply.isPositiveCompletion( response ) ) {
          // Set the sender and the recipients
          client.setSender( "tobrien@discursive.com" );
          client.addRecipient( "president@whitehouse.gov" );
          client.addRecipient( "vicepresident@whitehouse.gov" );
          // Supply the message via a Writer
          Writer message = client.sendMessageData( );
          message.write( "Spend more money on energy research.  Thanks." );
          message.close( );
          // Send the message and print a confirmation
          boolean success = client.completePendingCommand( );
          if( success ) {
          System.out.println( "Message sent" );
          }
          } else {
          System.out.println( "Error communicating with SMTP server" );
          }
          client.disconnect( );

          Instead of sendSimpleMessage( ), the previous example sets a sender address and two recipient addresses using setSender( ) and addRecipient(). The message body is then written to a Writer returned by sendMessageData(). When the Writer is closed, the message is sent by calling completePendingCommand(). completePendingCommand( ) returns true if the message has been queued for delivery.

          收郵件:

          Use Commons Net POP3Client to check a POP3 mailbox for incoming mail. The following example connects to the POP3 server www.discursive.com, logs in as tobrien@discursive.com, and prints each message in the mailbox:

          import org.apache.commons.io.CopyUtils;
          import org.apache.commons.io.IOUtils;
          import org.apache.commons.net.pop3.POP3Client;
          import org.apache.commons.net.pop3.POP3MessageInfo;
          POP3Client client = new POP3Client( );
          client.connect("www.discursive.com");
          client.login("tobrien@discursive.com", "secretpassword");
          POP3MessageInfo[] messages = client.listMessages( );
          for (int i = 0; i < messages.length; i++) {
          int messageNum = messages[i].number;
          System.out.println( "************* Message number: " + messageNum );
          Reader reader = client.retrieveMessage( messageNum );
          System.out.println( "Message:\n" + IOUtils.toString( reader ) );
          IOUtils.closeQuietly( reader );
          }
          client.logout( );
          client.disconnect( );

          posted on 2007-11-23 10:13 劉錚 閱讀(762) 評論(0)  編輯  收藏 所屬分類: JAVA General

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 始兴县| 石阡县| 寿阳县| 浏阳市| 珲春市| 昌宁县| 阿城市| 大连市| 大安市| 武邑县| 嘉善县| 禄劝| 江川县| 海丰县| 呼玛县| 恭城| 江永县| 铁岭市| 德安县| 宝兴县| 金坛市| 丽江市| 任丘市| 友谊县| 英德市| 六枝特区| 郁南县| 青铜峡市| 呼伦贝尔市| 盐源县| 普洱| 四平市| 葫芦岛市| 南安市| 五莲县| 张家界市| 鹤山市| 永川市| 新乡市| 新绛县| 昌黎县|