JavaEE5学习笔记04-JavaMail使用总结---3

1.      接收邮件实例

/**

 * 简单的接收邮件例子

 *

 * @author liuyan

 *

 */

public class ReciveMail {

 

    private final static String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

 

    private String account;

 

    private String password;

 

    private String pop3Host;

 

    private int pop3Port;

 

    private Store store;

 

 //省略setter、getter

 

    public Store getStore() throws MessagingException {

 

       if (this.store == null || !this.store.isConnected()) {

           Properties properties = new Properties();

 

           // gmail需要特殊处理SSL验证

           if (isGmail()) {

              properties.setProperty("mail.pop3.socketFactory.class",

                     SSL_FACTORY);

           }

 

           Session session = Session.getDefaultInstance(properties);

 

           // 使用pop3协议接收邮件

           URLName url = new URLName("pop3", getPop3Host(), getPop3Port(),

                  null, getAccount(), getPassword());

 

           // 获得邮件的存储对象

           Store store = session.getStore(url);

 

           // 连接上

           store.connect();

 

           this.store = store;

       }

 

       return store;

    }

 

 

  

    public ReciveMail() {

 

    }

 

    /**

     * 获得消息

     *

     * @throws MessagingException

     * @throws IOException

     */

    public void getMessages() throws MessagingException, IOException {

 

       // 获取INBOX文件夹,收件箱内

       Folder inbox = getStore().getFolder("INBOX");

 

       // 打开可读写的文件夹

       inbox.open(Folder.READ_WRITE);

 

       // 获得邮件实体对象数组

       Message[] messages = inbox.getMessages();

 

       // 遍历邮件

       for (int i = 0; i < messages.length; i++) {

 

           int j = i + 1;

 

           System.out.println("第【" + j + "】封邮件");

 

           // 邮件文本内容

           System.out

                  .println(getMailContent(messages[i], new StringBuffer(""))

                         .toString());

 

           // 获取邮件的附件

           getFiles(messages[i]);

 

           System.out

                  .println("===============================================");

       }

 

    }

 

    /**

     * 邮件的内容

     *

     * @param part

     * @param result

     * @return

     * @throws IOException

     * @throws MessagingException

     * @throws MessagingException

     * @throws IOException

     */

    private StringBuffer getMailContent(Part part, StringBuffer result)

           throws MessagingException, IOException {

       if (part.isMimeType("multipart/*")) {// 邮件是复杂类型的

           Multipart mp = (Multipart) part.getContent();

 

           // 邮件的整体部分body数量

           int sum = mp.getCount();

 

           // 第一部分是text/plain

           // 第二部分是text/html

           if (sum > 1)

              sum = 1;

 

           for (int i = 0; i < sum; i++) {

              BodyPart bp = mp.getBodyPart(i);

 

              // 递归

              getMailContent(bp, result);

           }

 

       } else if (part.isMimeType("text/*")) {// 邮件是纯文本类型的

           try {

              String str = (String) part.getContent();

 

              result.append(str);

 

           } catch (IOException e) {

 

              // gb2312需要异常处理

              InputStream is = part.getInputStream();

 

              byte[] b = new byte[1024];

             

              //因为gb2312的内容需要在异常中特殊处理一下,所以直接从字节数组中获得String内容

              is.read(b);

 

              String newStr = new String(b);

 

              result.append(newStr);

 

              // e.printStackTrace();

           } catch (MessagingException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           }

       }

       return result;

 

    }

 

你可能感兴趣的:(html,.net,J#,Gmail)