簡體   English   中英

如何編寫Java程序以從任何emailid讀取新電子郵件

[英]How to write java program to read new emails from any emailid

嗨,我想編寫一個Java程序,在其中提供我的電子郵件ID和密碼。 我想閱讀到達該電子郵件ID的所有新未讀消息。 我不知道該怎么寫程序。

以下程序適用於gmail。 但它不適用於yahoomail,因為未配置yahoo pop3。 我想要一個適用於所有電子郵件ID的通用代碼。

import java.io.*;
import java.util.*;
import javax.mail.*;

public class ReadMail {

    public static void main(String args[]) throws Exception {



//        String host = "pop.gmail.com";
//        String user = "xyz";
//        String password = "12345";

        // Get system properties
       Properties properties = System.getProperties();

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties, null);

        // Get a Store object that implements the specified protocol.
        Store store = session.getStore("pop3s");

        //Connect to the current host using the specified username and password.
        store.connect(host, user, password);

        //Create a Folder object corresponding to the given name.
        Folder folder = store.getFolder("inbox");

        // Open the Folder.
        folder.open(Folder.READ_ONLY);

        Message[] message = folder.getMessages();

        // Display message.
        for (int i = 0; i < message.length; i++) {

            System.out.println("------------ Message " + (i + 1) + " ------------");

            System.out.println("SentDate : " + message[i].getSentDate());
            System.out.println("From : " + message[i].getFrom()[0]);
            System.out.println("Subject : " + message[i].getSubject());
            System.out.print("Message : ");

            InputStream stream = message[i].getInputStream();
            while (stream.available() != 0) {
                System.out.print((char) stream.read());
            }
            System.out.println();
        }

        folder.close(true);
        store.close();
    }
}

您需要javax.mail軟件包及其文檔。 閱讀文檔。 那你知道

您需要了解的不僅僅是登錄密碼。 諸如郵件服務器地址郵件服務器類型連接端口等內容。您可能應該查看Java Mail APICommons Email

UPD:

您創建一個Session使用Session.getDefaultInstance()方法(這需要連接Properties對象和認證),獲得了Store ,從這個Session使用Session.getStore()方法,得到一個Folder使用從商店Store.getFolder("FOLDER_NAME")方法,使用Folder.open(Folder.READ)方法打開該Folder ,並使用Message[] messages = inboxFolder.getMessages();類的方法獲取所有消息Message[] messages = inboxFolder.getMessages();

那是您要找的東西嗎?

UPD2:

根本沒有辦法編寫通用程序,該程序僅使用服務器路徑,用戶ID和密碼即可與任何郵件提供商一起使用。 因為不同的郵件服務器配置不同。 他們在不同的端口上使用不同的協議(imap / pop3 / pop3 ssl)。 總有一些人配置了他的郵件服務器,使其僅在31337端口上通過ssl對話imap,所有其他端口和協議均被禁止。 這個家伙破壞了你的程序。 因此,您必須在properties對象中指定所有這些屬性。 在這里查找屬性,您必須指定。

UPD3:

再三考慮,您實際上有一個選擇。 只需嘗試使用不同的協議連接到服務器即可。 如果那沒有幫助,請開始通過端口進行迭代。 合適的是您的配置。 如果那真的是您想要的。

有兩種方法可以做到這一點:

1)Google提供了API來訪問您可以使用該庫的郵件,該庫可以更好地控制您的郵件。 參見此處: http : //code.google.com/apis/gmail/ 以相同的方式嘗試其他電子郵件提供商。

2)簡單的郵件客戶端(您可以很容易地對其進行谷歌搜索),但是您需要查看標題以識別哪些郵件是已讀/未讀等。

您需要一個注冊表,您可以在其中獲取給定郵件服務的屬性。

例如,您可以指定包含主機,端口,協議等的.properties文件的名稱,而不是指定pop3主機。

如果您的.properties文件包含協議,例如mail.store.protocol = pop3,則可以使用session.getStore()(不帶參數),並且相同的代碼可以用於pop3,imap,pop3s和imaps。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM