中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

利用Apache commons net 包實(shí)現(xiàn)簡(jiǎn)單的POP3郵件

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

Apache commons net中,對(duì)郵件的處理是非常強(qiáng)悍的,因此可以自己做一些郵件方面的工作。搭建郵件服務(wù)器的事情相對(duì)比較麻煩,我們還是直接利用現(xiàn)成的郵件服務(wù)器來(lái)使用,比如通過(guò)QQ郵箱收一些郵件。

在使用這個(gè)之前,要確保自己有一個(gè)郵箱,并且知道這個(gè)郵箱的POP3服務(wù)協(xié)議地址,以及這個(gè)郵箱對(duì)應(yīng)的用戶名和密碼。

利用net 包實(shí)現(xiàn)簡(jiǎn)單的POP3郵件代碼如下:

import java.io.BufferedReader;

import java.io.IOException;

import java.util.Locale;

 

import org.apache.commons.net.pop3.POP3Client;

import org.apache.commons.net.pop3.POP3MessageInfo;

 

public classEasyPOP3Mail {

    public static void main(String[] args) {

        POP3Clientpop3 = newPOP3Client();

        try {

            pop3.setDefaultPort(110);          

            // We want to timeout if a response takes longer than 60 seconds

            pop3.setDefaultTimeout(60000);

            pop3.connect("pop.qq.com");//QQ郵件~如果郵箱不可用,換一個(gè)可用的

            // 輸入你的QQ號(hào)作為名稱 QQ密碼作為郵箱密碼

            if (pop3.login("fanfangming","123456")){

                POP3MessageInfo[]messages = pop3.listMessages();

                if (messages == null)

                {

                    System.err.println("Could not retrieve message list.");

                    pop3.disconnect();

                    return;

                }

                else if (messages.length== 0)

                {

                    System.out.println("No messages");

                    pop3.logout();

                    pop3.disconnect();

                    return;

                }

 

                for (POP3MessageInfo msginfo : messages) {

                    BufferedReader reader =(BufferedReader) pop3.retrieveMessageTop(msginfo.number, 0);

 

                    if (reader == null) {

                        System.err.println("Could not retrieve message header.");

                        pop3.disconnect();

                        System.exit(1);

                    }

                    printMessageInfo(reader,msginfo.number);

                }

 

                pop3.logout();

                pop3.disconnect();

            }

 

        }catch(Exception e) {

            System.out.println("失敗");

            e.printStackTrace();

        }

 

    }

   

   public static final voidprintMessageInfo(BufferedReader reader, int id) throws IOException {

        String from = "";

        String subject = "";

        String line;

        while ((line = reader.readLine()) != null)

        {

        Stringlower = line.toLowerCase(Locale.CHINESE);

            if (lower.startsWith("from: ")) {

                from = line.substring(6).trim();

            } else if (lower.startsWith("subject: ")){

                subject =line.substring(9).trim();

            }

        }

        System.out.println(Integer.toString(id) + " From: "+ from + " Subject: " + subject);

   }

}

標(biāo)簽: 代碼 服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:java加密解密類

下一篇:JDBC工具類