使用javamail按域过滤电子邮件 [英] Filter emails by domain using javamail

查看:86
本文介绍了使用javamail按域过滤电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javamail下载电子邮件,但是现在我需要按域过滤一些电子邮件地址.

I'm using javamail to download emails, but now I need to filter some email address by domain.

我尝试使用FromStringTerm,但是我不知道正确的模式来过滤它.

I try to use the FromStringTerm but I can't know the correct pattern to filter it.

编辑1 : jmehrens 部分解决了我的问题.我想从文件夹中获取消息时进行过滤,例如:

edit 1: the jmehrens solved partially my problem. I would like to filter when i get messages from folder, something like:

            Store store = session.getStore("imap");
            store.connect(configc.host, configc.email, configc.pass);
            Folder folderInbox = store.getFolder("INBOX");
            folderInbox.open(Folder.READ_ONLY);

            Message[] arrayMessages1 = folderInbox.search(??);

此时我如何按域进行过滤?

How can I filter by domain at this point?

推荐答案

FromStringTerm继承了

The FromStringTerm inherits the behavior described in javax.mail.search.SearchTerm:

此类实现String的match方法.当前实现仅提供子字符串匹配.我们可以添加比较(例如strcmp ...).

This class implements the match method for Strings. The current implementation provides only for substring matching. We could add comparisons (like strcmp ...).

因此没有模式,因为它开箱即用地执行子字符串匹配.

So there is no pattern as it performs substring matching out of the box.

这是一个测试用例:

public class DomainMatch {

    public static void main(String[] args) throws Exception {
        MimeMessage msg = new MimeMessage((Session) null);
        msg.addFrom(InternetAddress.parse("foo@bar.org"));
        msg.saveChanges();

        System.out.println(new FromStringTerm("@bar.org").match(msg));
        System.out.println(new FromStringTerm("@spam.org").match(msg));
    }
}

使用

Using the javax.mail.Folder::search documentation you would write something like:

Store store = session.getStore("imap");
store.connect(configc.host, configc.email, configc.pass);
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_ONLY);

Message[] onlyBarOrg = folderInbox.search(new FromStringTerm("@bar.org"));

这篇关于使用javamail按域过滤电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆