【问题标题】:Counting the number of Emails in the Gmail INBOX计算 Gmail 收件箱中的电子邮件数量
【发布时间】:2011-10-11 22:21:38
【问题描述】:

这是计算gmail收件箱中邮件数量的代码。

Properties props = new Properties();
    props.put("mail.pop3.host" , "pop.gmail.com");
    props.put("mail.pop3.user" , "username");
    props.put("mail.pop3.socketFactory" , 995 );
    props.put("mail.pop3.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
    props.put("mail.pop3.port" , 995);
    Session session = Session.getDefaultInstance(props , new Authenticator() {
        @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication( "username" , "password");
                }
    });
    try {
        Store store  = session.getStore("pop3");
        store.connect("pop.gmail.com" , "username" , "password");
        Folder fldr = store.getFolder("INBOX");
        fldr.open(Folder.HOLDS_MESSAGES);
        int count = fldr.getMessageCount();
        System.out.println(count);
    } catch(Exception exc) {
        System.out.println(exc + " error");
    }

我得到的计数等于 7,但我应该得到 3,因为我的收件箱中只有 3 条消息。

【问题讨论】:

  • 如果您连接到电子邮件程序会发生什么?存档文件夹中有电子邮件吗?我记得当我尝试使用 Thunderbird 下载我的邮件时,它开始下载我从一开始收到的每封电子邮件,而不是查看收件箱中的内容。
  • @Boris Treukhov 是的!然后计数是 7。但我想在INBOX 中计算

标签: java gmail jakarta-mail pop3 gmail-pop


【解决方案1】:

这是从收件箱中读取电子邮件的示例。

http://bharatonjava.wordpress.com/2012/09/15/read-emails-form-inbox/

这里是代码sn-p

public static void main(String[] args){
    Properties props = new Properties();
    try {
        props.load(new FileInputStream(new File("settings.properties")));
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

Session session = Session.getDefaultInstance(props, null);

Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", "yourEmailId@gmail.com",
                    "put your password here");

Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_WRITE); // Folder.READ_ONLY
int messageCount = inbox.getMessageCount();
System.out.println("Total Messages" + messageCount);
}

您必须将电子邮件设置保存在名为 settings.properties 的属性文件中,如下所示。

mail.smtp.host=smtp.gmail.com
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.auth=true
mail.smtp.port=465

【讨论】:

    【解决方案2】:

    为此使用IMAP 协议。

    Properties props = new Properties();
        props.put("mail.imap.host" , "imap.gmail.com");
        props.put("mail.imap.user" , "username");
        props.put("mail.imap.socketFactory" , 993 );
        props.put("mail.imap.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
        props.put("mail.imap.port" , 993);
        Session session = Session.getDefaultInstance(props , new Authenticator() {
            @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication( "username" , "password");
                    }
        });
        try {
            Store store  = session.getStore("imap");
            store.connect("imap.gmail.com" , "username" , "password");
            Folder fldr = store.getFolder("Inbox");
            fldr.open(Folder.HOLDS_MESSAGES);
            int count = fldr.getMessageCount();
            System.out.println(count);
        } catch(Exception exc) {
            System.out.println(exc + "error");
        }
    

    【讨论】:

    • 不起作用。它没有响应 store.connect("imap.gmail.com" , "username" , "password");声明
    • @nithinreddy 你得到什么异常?
    • 嘿,修复它..thanks :) 我在 session.getStore() 中使用了 imaps 并且有效。我在 store.connect() 中有连接超时异常。你知道是什么原因吗?
    • @nithinreddy 可能有很多。 用户名/密码错误、防火墙问题等。我无法告诉你确切的原因
    • 好的..用户名密码相同。防火墙是一样的......不确定,我只是使用 imaps 并且它有效
    【解决方案3】:

    在 GMAIL POP3 设置中,您应该只对当前收到的电子邮件启用 POP 访问,这是标准的 GMAIL 行为。

    启用 POP 后,所有邮件都会下载到您的客户端,但垃圾邮件、垃圾邮件和聊天邮件除外。如果您不希望从 Web 界面发送的消息下载到您的邮件客户端的收件箱,我们建议在您的客户端中创建一个过滤器。您可能需要联系您的邮件客户端的客户服务部门,了解如何对下载的邮件进行分类。

    See the GMAIL troubleshooting article

    GMAIL 中的 AFAIK 选择性同步仅适用于 IMAP 协议。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2017-05-15
    相关资源
    最近更新 更多