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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2825887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:19:01  来源:igfitidea点击:

How to write java program to read new emails from any emailid

javaemailjavamail

提问by Sunil Kumar Sahoo

Hi I want to write a java program where I will provide my email id and password. and I want to read all new unread messages that arrived to that email id. I donot know how to write program for that.

嗨,我想编写一个 java 程序,我将在其中提供我的电子邮件 ID 和密码。我想阅读到达该电子邮件 ID 的所有新的未读邮件。我不知道如何为此编写程序。

The below program works fine for gmail. but it does not work for yahoomail because for yahoo pop3 is not configured. I want a generic code which will work for all email id.

下面的程序适用于 gmail。但它不适用于雅虎邮箱,因为雅虎 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();
    }
}

采纳答案by folone

You need to know more than just login-pass. Things like mail server address, mail server type, port for connections, etc. You should probably check out Java Mail API, or Commons Email.

您需要了解的不仅仅是登录通行证。诸如邮件服务器地址邮件服务器类型连接端口等。您可能应该查看Java Mail APICommons Email

UPD:

更新:

You create a Sessionusing Session.getDefaultInstance()method (which takes connection Propertiesobject and authenticator), get a Storefrom this Sessionusing Session.getStore()method, get a Folderfrom that store using Store.getFolder("FOLDER_NAME")method, open that Folder, using Folder.open(Folder.READ)method, and get all messages, using something like Message[] messages = inboxFolder.getMessages();

您创建一个SessionusingSession.getDefaultInstance()方法(它采用连接Properties对象和身份验证器),Store从这个SessionusingSession.getStore()方法中获取一个,Folder从那个存储中获取一个usingStore.getFolder("FOLDER_NAME")方法,打开那个Folder, usingFolder.open(Folder.READ)方法,并使用类似的方法获取所有消息Message[] messages = inboxFolder.getMessages();

Is that what you were looking for?

这就是你要找的吗?

UPD2:

UPD2:

There is simply no way to write a generic program, which will work with any mail provider, using just server path, userID and password. Because different mail servers are configured differently. They talk differen protocols (imap/pop3/pop3 ssl) on different ports. There's always some guy, who has configured his mail server to talk imap over ssl on 31337 port only, all the other ports and protocols are banned. And this guy breaks your program. So, you'll have to specify all this properties in your propertiesobject. Look herefor properties, you'll have to specify.

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

UPD3:

UPD3:

On second thought, you actually have one option. Just try connecting to the server using different protocols. If that does not help, start iterating through ports. The one that fits is your configuration. If that's really what you want.

转念一想,您实际上只有一种选择。只需尝试使用不同的协议连接到服务器。如果这没有帮助,请开始遍历端口。适合的就是您的配置。如果这真的是你想要的。

回答by Daniel

You need the javax.mail package, and the documentation of it. Read the documentation. Then you know.

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

回答by Phani

There are two ways to do it:

有两种方法可以做到:

1) Google provides API's to access mail you could use that library which provides more control over your mails. See here: http://code.google.com/apis/gmail/. In the same way try for other email providers.

1) Google 提供 API 来访问邮件,您可以使用该库来更好地控制您的邮件。请参阅此处:http: //code.google.com/apis/gmail/。以同样的方式尝试其他电子邮件提供商。

2) Simple mail client(you could find it easily googling), but you need to look at headers to identify which mails are read/unread etc.

2)简单的邮件客户端(你可以很容易地在谷歌上找到它),但你需要查看标题来确定哪些邮件是已读/未读等。

回答by Maurice Perry

You need a registry where you can get the properties for a given mail service.

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

For instance, instead of specifying a pop3 host, you could specify the name of a .properties file that would contain the host, the port, the protocol, etc...

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

If your .properties file contains the protocol, for instance mail.store.protocol=pop3, you could use session.getStore() (with no argument), and the same code could be used for pop3, imap, pop3s, imaps.

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