代码之家  ›  专栏  ›  技术社区  ›  Sunil Kumar Sahoo

如何编写java程序从任何emailid读取新邮件

  •  0
  • Sunil Kumar Sahoo  · 技术社区  · 14 年前

    嗨,我想写一个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();
        }
    }
    
    4 回复  |  直到 14 年前
        1
  •  1
  •   George    14 年前

    你需要知道的不仅仅是登录密码。比如 邮件服务器地址 , 邮件服务器类型 , 连接端口 Java Mail API ,或 Commons Email .

    升级版本:

    你创造了一个 Session 使用 Session.getDefaultInstance() Properties 对象和验证器),获取 Store 从这个 会话 Session.getStore() 方法,获取 Folder 从那家商店用 Store.getFolder("FOLDER_NAME") 文件夹 ,使用 Folder.open(Folder.READ) 方法,并使用 Message[] messages = inboxFolder.getMessages();

    这就是你要找的吗?

    升级2:

    根本没有办法编写一个通用程序,它只使用服务器路径、用户ID和密码就可以与任何邮件提供商一起工作。因为不同的邮件服务器配置不同。他们在不同的端口上使用不同的协议(imap/pop3/pop3ssl)。总有人,谁配置了他的邮件服务器,通过ssl的31337端口只谈imap,所有其他端口和协议被禁止。这家伙破坏了你的程序。因此,您必须在 properties here 对于属性,您必须指定。

    升级3:

    仔细想想,你其实有一个选择。试着用不同的协议连接到服务器。如果这没有帮助,就开始遍历端口。最适合你的是你的配置。 如果那真的是你想要的。

        2
  •  1
  •   Daniel    14 年前

    您需要javax.mail包及其文档。阅读文档。那你就知道了。

        3
  •  0
  •   Phani    14 年前

    有两种方法:

    http://code.google.com/apis/gmail/

    2) 简单的邮件客户端(你可以很容易地在google上找到),但是你需要查看邮件头来识别哪些邮件是读的/未读的等等。

        4
  •  0
  •   Maurice Perry    14 年前

    您需要一个注册表,在那里可以获取给定邮件服务的属性。

    例如,您可以指定.properties文件的名称,而不是指定pop3主机,该文件将包含主机、端口、协议等。。。

    如果.properties文件包含协议,例如mail.store.protocol=pop3,则可以使用session.getStore()(不带参数),相同的代码可以用于pop3、imap、pop3s和imaps。

    推荐文章