利用Apache commons net 包實現簡單的POP3郵件

來源:互聯網
上載者:User

標籤:apache   java   code   原始碼   net   

Apache commons net中,對郵件的處理是非常強悍的,因此可以自己做一些郵件方面的工作。搭建郵件伺服器的事情相對比較麻煩,我們還是直接利用現成的郵件伺服器來使用,比如通過QQ郵箱收一些郵件。

在使用這個之前,要確保自己有一個郵箱,並且知道這個郵箱的POP3服務合約地址,以及這個郵箱對應的使用者名稱和密碼。

利用net 包實現簡單的POP3郵件代碼如下:

package test.ffm83.commons.net.mail;

 

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郵件~如果郵箱不可用,換一個可用的

            // 輸入你的QQ號作為名稱 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);

   }

}

運行結果如下:

No messages

 

當前沒有郵件。

 

利用Apache commons net 包實現簡單的POP3郵件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.