Example usage for javax.mail.search RecipientStringTerm RecipientStringTerm

List of usage examples for javax.mail.search RecipientStringTerm RecipientStringTerm

Introduction

In this page you can find the example usage for javax.mail.search RecipientStringTerm RecipientStringTerm.

Prototype

public RecipientStringTerm(Message.RecipientType type, String pattern) 

Source Link

Document

Constructor.

Usage

From source file:com.robin.utilities.Utilities.java

/**
 * A utility that waits for a gmail mailbox to receive a new message which
 * subject contains a specific string and the TO recipient is also given.
 * @param username the mailbox owner's user name (no @gmail.com required).
 * @param pass the user password protecting this mailbox
 * @param recepient the recipient given as a TO type
 * @param subject the string that must be contained in the new mail
 * @param timeout The maximum amount of time to wait in milliseconds.
 * @return a last from the new messages thats subject contains the specified
 *         string//  w  ww .jav a2 s  .c  o m
 */
public EMail waitForMailToAndSubjectContains(final String username, final String pass, final String recepient,
        final String subject, final long timeout) {
    SearchTerm st = new AndTerm(new RecipientStringTerm(RecipientType.TO, recepient), new SubjectTerm(subject));
    return waitForMailWithSearchTerm(username, pass, st,
            "No new mail arrived to '" + recepient + "' with subject containing '" + subject + "'.", timeout);
}

From source file:com.sonicle.webtop.mail.bol.model.ImapQuery.java

public static SearchTerm toSearchTerm(String allFlagStrings[], List<Tag> atags, QueryObj query,
        DateTimeZone timezone) {/*from   w w  w  .ja  va 2  s.c  om*/
    SearchTerm searchTerm = null;
    ArrayList<SearchTerm> terms = new ArrayList<SearchTerm>();

    if (query != null) {
        ArrayList<Condition> conditionsList = query.conditions;
        String allText = query.allText;

        if (allText != null && allText.trim().length() > 0) {
            SearchTerm defaultterms[] = toDefaultSearchTerm(allText);
            terms.add(new OrTerm(defaultterms));
        }

        conditionsList.forEach(condition -> {
            String key = condition.keyword;
            String value = condition.value;

            if (key.equals(FROM)) {
                terms.add(new FromStringTerm(value));
            } else if (key.equals(TO)) {
                terms.add(new RecipientStringTerm(Message.RecipientType.TO, value));
            } else if (key.equals(SUBJECT)) {
                terms.add(new SubjectTerm(value));
            } else if (key.equals(MESSAGE)) {
                terms.add(new BodyTerm(value));
            } else if (key.equals(EVERYWHERE)) {
                SearchTerm anyterms[] = toAnySearchTerm(value);
                terms.add(new OrTerm(anyterms));
            } else if (key.equals(AFTER)) {
                Date afterDate = ImapQuery.parseDate(value, timezone);
                terms.add(new ReceivedDateTerm(DateTerm.GE, afterDate));
                terms.add(new SentDateTerm(DateTerm.GE, afterDate));
            } else if (key.equals(BEFORE)) {
                Date beforeDate = ImapQuery.parseDate(value, timezone);
                terms.add(new ReceivedDateTerm(DateTerm.LE, beforeDate));
                terms.add(new SentDateTerm(DateTerm.LE, beforeDate));

            } else if (value.equals(ATTACHMENT)) {

            } else if (value.equals(UNREAD)) {
                terms.add(new FlagTerm(new Flags(Flag.SEEN), false));
            } else if (value.equals(FLAGGED)) {
                FlagTerm fts[] = new FlagTerm[allFlagStrings.length + 1];
                fts[0] = new FlagTerm(new Flags(Flag.FLAGGED), true);
                for (int i = 0; i < allFlagStrings.length; ++i)
                    fts[i + 1] = new FlagTerm(new Flags(allFlagStrings[i]), true);
                terms.add(new OrTerm(fts));
            } else if (value.equals(TAGGED)) {
                FlagTerm fts[] = new FlagTerm[atags.size()];
                int i = 0;
                for (Tag tag : atags)
                    fts[i++] = new FlagTerm(new Flags(tag.getTagId()), true);
                terms.add(new OrTerm(fts));
            } else if (value.equals(UNANSWERED)) {
                terms.add(new FlagTerm(new Flags(Flag.ANSWERED), false));
            } else if (value.equals(PRIORITY)) {
                HeaderTerm p1 = new HeaderTerm("X-Priority", "1");
                HeaderTerm p2 = new HeaderTerm("X-Priority", "2");
                terms.add(new OrTerm(p1, p2));
            }

        });
    }

    int n = terms.size();
    if (n == 1) {
        searchTerm = terms.get(0);
    } else if (n > 1) {
        SearchTerm vterms[] = new SearchTerm[n];
        terms.toArray(vterms);
        searchTerm = new AndTerm(vterms);
    }
    return searchTerm;
}

From source file:com.sonicle.webtop.mail.bol.model.ImapQuery.java

public static SearchTerm[] toAnySearchTerm(String value) {
    SearchTerm anyterms[] = new SearchTerm[4];
    anyterms[0] = new SubjectTerm(value);
    anyterms[1] = new RecipientStringTerm(Message.RecipientType.TO, value);
    anyterms[2] = new FromStringTerm(value);
    anyterms[3] = new BodyTerm(value);
    return anyterms;
}

From source file:com.sonicle.webtop.mail.bol.model.ImapQuery.java

public static SearchTerm[] toDefaultSearchTerm(String value) {
    SearchTerm anyterms[] = new SearchTerm[3];
    anyterms[0] = new SubjectTerm(value);
    anyterms[1] = new RecipientStringTerm(Message.RecipientType.TO, value);
    anyterms[2] = new FromStringTerm(value);
    return anyterms;
}

From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java

@Override
public GmailMessageList getMessagesBy(EmailSearchStrategy strategy, String value) {
    SearchTerm seekStrategy = null;//from   w  ww .j  a  va  2 s  . co m
    switch (strategy) {
    case SUBJECT:
        seekStrategy = new SubjectTerm(value);
        LOG.debug("Fetching emails with a subject of \"" + value + "\"");
        break;
    case TO:
        seekStrategy = new RecipientStringTerm(Message.RecipientType.TO, value);
        LOG.debug("Fetching emails sent to \"" + value + "\"");
        break;
    case FROM:
        seekStrategy = new FromStringTerm(value);
        LOG.debug("Fetching emails sent from \"" + value + "\"");
        break;
    case CC:
        seekStrategy = new RecipientStringTerm(Message.RecipientType.CC, value);
        LOG.debug("Fetching emails CC'd to \"" + value + "\"");
        break;
    case DATE_GT:
        seekStrategy = new SentDateTerm(SentDateTerm.GT, parseDate(value));
        LOG.debug("Fetching emails with a send date newer than \"" + value + "\"");
        break;
    case DATE_LT:
        seekStrategy = new SentDateTerm(SentDateTerm.LT, parseDate(value));
        LOG.debug("Fetching emails with a send date newer than \"" + value + "\"");
        break;
    case DATE_EQ:
        seekStrategy = new SentDateTerm(SentDateTerm.EQ, parseDate(value));
        LOG.debug("Fetching emails with a send date newer than \"" + value + "\"");
        break;
    case KEYWORD:
        seekStrategy = new BodyTerm(value);
        LOG.debug("Fetching emails containing the keyword \"" + value + "\"");
        break;
    case UNREAD:
        seekStrategy = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        LOG.debug("Fetching all unread emails");
        break;
    }
    try {
        final GmailMessageList found = new GmailMessageList();
        final Store store = openGmailStore();
        final Folder folder = getFolder(this.srcFolder, store);
        folder.open(Folder.READ_ONLY);
        for (final Message msg : folder.search(seekStrategy)) {
            found.add(new JavaMailGmailMessage(msg));
        }
        LOG.debug("Found " + found.size() + " emails");
        return found;
    } catch (final Exception e) {
        throw new GmailException("Failed getting unread messages", e);
    }
}

From source file:com.cubusmail.server.mail.util.MessageUtils.java

private static SearchTerm createSearchTerm(MessageListFields searchField, String[] searchValues) {

    SearchTerm[] terms = new SearchTerm[searchValues.length];
    for (int i = 0; i < searchValues.length; i++) {
        if (searchField == MessageListFields.SUBJECT) {
            terms[i] = new SubjectTerm(searchValues[i]);
        } else if (searchField == MessageListFields.FROM) {
            terms[i] = new FromStringTerm(searchValues[i]);
        } else if (searchField == MessageListFields.TO) {
            terms[i] = new RecipientStringTerm(RecipientType.TO, searchValues[i]);
        } else if (searchField == MessageListFields.CC) {
            terms[i] = new RecipientStringTerm(RecipientType.CC, searchValues[i]);
        } else if (searchField == MessageListFields.CONTENT) {
            terms[i] = new BodyTerm(searchValues[i]);
        } else {/*from  ww  w . ja v  a  2s .co  m*/
            throw new IllegalArgumentException("Search field now allowed: " + searchField.name());
        }
    }

    if (searchValues.length > 1) {
        return new AndTerm(terms);
    } else {
        return terms[0];
    }
}

From source file:org.apache.manifoldcf.crawler.connectors.email.EmailConnector.java

private Message[] findMessages(Folder folder, long startTime, long endTime, Map<String, String> findMap)
        throws MessagingException, InterruptedException {
    String findParameterName;/*from w  w w.  j ava 2 s  .  c  o m*/
    String findParameterValue;

    SearchTerm searchTerm = null;

    Iterator<Map.Entry<String, String>> it = findMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> pair = it.next();
        findParameterName = pair.getKey().toLowerCase();
        findParameterValue = pair.getValue();
        if (Logging.connectors.isDebugEnabled())
            Logging.connectors.debug(
                    "Email: Finding emails where '" + findParameterName + "' = '" + findParameterValue + "'");
        SearchTerm searchClause = null;
        if (findParameterName.equals(EmailConfig.EMAIL_SUBJECT)) {
            searchClause = new SubjectTerm(findParameterValue);
        } else if (findParameterName.equals(EmailConfig.EMAIL_FROM)) {
            searchClause = new FromStringTerm(findParameterValue);
        } else if (findParameterName.equals(EmailConfig.EMAIL_TO)) {
            searchClause = new RecipientStringTerm(Message.RecipientType.TO, findParameterValue);
        } else if (findParameterName.equals(EmailConfig.EMAIL_BODY)) {
            searchClause = new BodyTerm(findParameterValue);
        }

        if (searchClause != null) {
            if (searchTerm == null)
                searchTerm = searchClause;
            else
                searchTerm = new AndTerm(searchTerm, searchClause);
        } else {
            Logging.connectors.warn("Email: Unknown filter parameter name: '" + findParameterName + "'");
        }
    }

    Message[] result;
    if (searchTerm == null) {
        GetMessagesThread gmt = new GetMessagesThread(session, folder);
        gmt.start();
        result = gmt.finishUp();
    } else {
        SearchMessagesThread smt = new SearchMessagesThread(session, folder, searchTerm);
        smt.start();
        result = smt.finishUp();
    }
    return result;
}

From source file:org.exist.xquery.modules.mail.MessageListFunctions.java

private SearchTerm parseRecipientTerm(Node terms) throws XPathException {
    SearchTerm st = null;/*from w w  w  . j  av a  2s.c  om*/

    String pattern = ((Element) terms).getAttribute("pattern");
    String type = ((Element) terms).getAttribute("recipientType");

    if (StringUtils.isEmpty(type)) {
        throw (new XPathException(this,
                "recipientType not specified for term with type: " + ((Element) terms).getAttribute("type")));
    }

    if (pattern != null && pattern.length() > 0) {
        Message.RecipientType rtype = null;

        if (type.equalsIgnoreCase("to")) {
            rtype = Message.RecipientType.TO;
        } else if (type.equalsIgnoreCase("cc")) {
            rtype = Message.RecipientType.CC;
        } else if (type.equalsIgnoreCase("bcc")) {
            rtype = Message.RecipientType.BCC;
        } else {
            throw (new XPathException(this, "Invalid recipientType: " + type + ", for term with type: "
                    + ((Element) terms).getAttribute("type")));
        }

        st = new RecipientStringTerm(rtype, pattern);
    } else {
        throw (new XPathException(this, "Pattern attribute must be specified for term with type: "
                + ((Element) terms).getAttribute("type")));
    }

    return (st);
}

From source file:org.pentaho.di.job.entries.getpop.MailConnection.java

/**
 * Set filter on receipient.//from   w  w w  .  jav a  2  s . co  m
 *
 * @param receipient
 *          messages will be filtered on receipient
 */
public void setReceipientTerm(String receipient) {
    if (!Utils.isEmpty(receipient)) {
        addSearchTerm(new RecipientStringTerm(Message.RecipientType.TO, receipient));
    }
}