JavaMail 讀取 Gmail 郵件的問題

來源:互聯網
上載者:User

最近在寫JavaMail接收Gmail郵件的NetBeans外掛程式,發現一個問題。。。。

下面是串連郵件賬戶與內送郵件的代碼片斷:

 /** 
     * conncect to the gmail account.
     * @param userName user name
     * @param userPassword user password
     * @throws Exception
     */
    private static void connect(String userName, String userPassword)
            throws Exception {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        Properties props = System.getProperties();
        props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.pop3.socketFactory.fallback", "false");
        props.setProperty("mail.pop3.port", "995");
        props.setProperty("mail.pop3.socketFactory.port", "995");

        props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.imap.socketFactory.fallback", "false");
        props.setProperty("mail.imap.port", "993");
        props.setProperty("mail.imap.socketFactory.port", "993");

        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(true); // set debug

//        URLName urln = new URLName("pop3s", "pop.gmail.com", 995, null,
//                userName, userPassword);
        URLName urln = new URLName("imap", "imap.gmail.com", 993, null, userName,
                userPassword);
        store = session.getStore(urln);
        store.connect();
    }

    /**
     * Retieve all new messages.
     * @param userName user name
     * @param userPassword user password
     * @return all new messages
     * @throws Exception
     */
    public Message[] getNewMails(String userName, String userPassword)
            throws Exception {
        if (!store.isConnected()) {
            connect(userName, userPassword);
        }

        Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        FetchProfile profile = new FetchProfile();
        profile.add(FetchProfile.Item.ENVELOPE);

        if (inbox.getUnreadMessageCount() > 0) {
            int fetchCount = inbox.getMessageCount() - inbox.getUnreadMessageCount();
            if (fetchCount == 0) {
                return inbox.getMessages();
            }

            Message[] messages = inbox.getMessages(1, 2);

            return messages;
        } else {
            System.out.println("No any new mail!");
            throw new RuntimeException("No any new mail!");
        }
    }

使用POP3/POP3s串連後統計出來的郵件數目都是不對的。而使用IMAP統計的就正確了。很鬱悶。。。。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.