使用javamail按域过滤电子邮件

时间:2022-01-28 18:15:56

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

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

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

我尝试使用FromStringTerm但我无法知道过滤它的正确模式。

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

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

            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?

我如何在此时按域过滤?

1 个解决方案

#1


2  

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

FromStringTerm继承了javax.mail.search.SearchTerm中描述的行为:

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

该类实现了Strings的匹配方法。当前实现仅提供子字符串匹配。我们可以添加比较(如strcmp ......)。

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

所以没有模式,因为它开箱即可执行子串匹配。

Here is a test case:

这是一个测试用例:

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:

使用javax.mail.Folder :: search文档,您可以编写如下内容:

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"));

#1


2  

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

FromStringTerm继承了javax.mail.search.SearchTerm中描述的行为:

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

该类实现了Strings的匹配方法。当前实现仅提供子字符串匹配。我们可以添加比较(如strcmp ......)。

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

所以没有模式,因为它开箱即可执行子串匹配。

Here is a test case:

这是一个测试用例:

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:

使用javax.mail.Folder :: search文档,您可以编写如下内容:

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"));