Java Code Examples for javax.mail.search.SearchTerm

The following examples show how to use javax.mail.search.SearchTerm. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: micro-integrator   Source File: GreenMailServer.java    License: Apache License 2.0 7 votes vote down vote up
/**
 * Check mail folder for an email using subject.
 *
 * @param emailSubject Email subject
 * @param folder       mail folder to check for an email
 * @param protocol     protocol used to connect to the server
 * @return whether mail received or not
 * @throws MessagingException if we're unable to connect to the store
 */
private static boolean isMailReceivedBySubject(String emailSubject, String folder, String protocol,
                                               GreenMailUser user) throws MessagingException {
    boolean emailReceived = false;
    Folder mailFolder;
    Store store = getConnection(user, protocol);
    try {
        mailFolder = store.getFolder(folder);
        mailFolder.open(Folder.READ_WRITE);
        SearchTerm searchTerm = new AndTerm(new SubjectTerm(emailSubject), new BodyTerm(emailSubject));
        Message[] messages = mailFolder.search(searchTerm);
        for (Message message : messages) {
            if (message.getSubject().contains(emailSubject)) {
                log.info("Found the Email with Subject : " + emailSubject);
                emailReceived = true;
                break;
            }
        }
    } finally {
        if (store != null) {
            store.close();
        }
    }
    return emailReceived;
}
 
Example 2
@Override
protected long[] searchInternal(SearchTerm searchTerm)
{
    List<SimpleStoredMessage> messages = getMessages();
    long[] result = new long[messages.size()];
    int i = 0;
    
    for (SimpleStoredMessage message : messages)
    {
        if (searchTerm.match(message.getMimeMessage()))
        {
            result[i] = message.getUid();
            i++;
        }
    }
    return Arrays.copyOfRange(result, 0, i);
}
 
Example 3
Source Project: product-ei   Source File: GreenMailServer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Check mail folder for an email using subject.
 *
 * @param emailSubject Email subject
 * @param folder       mail folder to check for an email
 * @param protocol     protocol used to connect to the server
 * @return whether mail received or not
 * @throws MessagingException if we're unable to connect to the store
 */
private static boolean isMailReceivedBySubject(String emailSubject, String folder, String protocol,
        GreenMailUser user) throws MessagingException {
    boolean emailReceived = false;
    Folder mailFolder;
    Store store = getConnection(user, protocol);
    try {
        mailFolder = store.getFolder(folder);
        mailFolder.open(Folder.READ_WRITE);
        SearchTerm searchTerm = new AndTerm(new SubjectTerm(emailSubject), new BodyTerm(emailSubject));
        Message[] messages = mailFolder.search(searchTerm);
        for (Message message : messages) {
            if (message.getSubject().contains(emailSubject)) {
                log.info("Found the Email with Subject : " + emailSubject);
                emailReceived = true;
                break;
            }
        }
    } finally {
        if (store != null) {
            store.close();
        }
    }
    return emailReceived;
}
 
Example 4
Source Project: javamail-mock2   Source File: MailboxFolder.java    License: Apache License 2.0 6 votes vote down vote up
public Message[] search(final SearchTerm term, final Message[] msgsToSearch) {
    final List<MockMessage> sms = new ArrayList<MockMessage>();
    final List<Message> msgsToSearchL = new ArrayList<Message>();

    if (msgsToSearch != null) {
        msgsToSearchL.addAll(Arrays.asList(msgsToSearch));
    }

    for (final Message msg : getMessages()) {
        if (term != null && term.match(msg)) {

            if (msgsToSearch == null || msgsToSearchL.contains(msg)) {
                sms.add((MockMessage) msg);
            }
        }
    }
    return sms.toArray(new Message[sms.size()]);
}
 
Example 5
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 6 votes vote down vote up
private static SearchTermBuilder createTextSearchTermBuilder() {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            String query = getParameters().get(0);
            SearchTerm[] terms = {
              new RecipientStringTerm(Message.RecipientType.TO, query),
              new RecipientStringTerm(Message.RecipientType.CC, query),
              new RecipientStringTerm(Message.RecipientType.BCC, query),
              new FromStringTerm(query),
              new SubjectTerm(query),
              new BodyTerm(query)
            };
            return new OrTerm(terms);
        }
    };
}
 
Example 6
Source Project: greenmail   Source File: HierarchicalFolder.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public long[] search(SearchTerm searchTerm) {
    List<StoredMessage> matchedMessages = new ArrayList<>();

    synchronized (mailMessages) {
        for (int i = 0; i < mailMessages.size(); i++) {
            StoredMessage mailMessage = mailMessages.get(i);
            // Update message sequence number for potential sequence set search
            // https://tools.ietf.org/html/rfc3501#page-10
            mailMessage.updateMessageNumber(i + 1);
            if (searchTerm.match(mailMessage.getMimeMessage())) {
                matchedMessages.add(mailMessage);
            }
        }
    }

    long[] matchedUids = new long[matchedMessages.size()];
    for (int i = 0; i < matchedUids.length; i++) {
        StoredMessage storedMessage = matchedMessages.get(i);
        long uid = storedMessage.getUid();
        matchedUids[i] = uid;
    }
    return matchedUids;
}
 
Example 7
Source Project: micro-integrator   Source File: MailToTransportUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Check a particular email has received to a given email folder by email subject.
 *
 * @param emailSubject - Email emailSubject to find email is in inbox or not
 * @return - found the email or not
 * @throws ESBMailTransportIntegrationTestException - Is thrown if an error occurred while reading the emails
 */
public static boolean isMailReceivedBySubject(String emailSubject, String folder)
        throws ESBMailTransportIntegrationTestException {
    boolean emailReceived = false;
    Folder mailFolder;
    Store store = getConnection();
    try {
        mailFolder = store.getFolder(folder);
        mailFolder.open(Folder.READ_WRITE);
        SearchTerm searchTerm = new AndTerm(new SubjectTerm(emailSubject), new BodyTerm(emailSubject));
        Message[] messages = mailFolder.search(searchTerm);
        for (Message message : messages) {
            if (message.getSubject().contains(emailSubject)) {
                log.info("Found the email emailSubject : " + emailSubject);
                emailReceived = true;
                break;
            }
        }
        return emailReceived;
    } catch (MessagingException ex) {
        log.error("Error when getting mail count ", ex);
        throw new ESBMailTransportIntegrationTestException("Error when getting mail count ", ex);
    } finally {
        if (store != null) {
            try {
                store.close();
            } catch (MessagingException e) {
                log.warn("Error when closing the store ", e);
            }
        }
    }
}
 
Example 8
Source Project: hop   Source File: MailConnection.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Add search term.
 *
 * @param term search term to add
 */
private void addSearchTerm( SearchTerm term ) {
  if ( this.searchTerm != null ) {
    this.searchTerm = new AndTerm( this.searchTerm, term );
  } else {
    this.searchTerm = term;
  }
}
 
Example 9
/**
 * Searches the mailbox for messages that match the given searching criteria
 * 
 * @param searchTerm - search term that contains search criteria.
 * @return UIDs of the messages
 */
public long[] search(final SearchTerm searchTerm)
{
    CommandCallback<long[]> command = new CommandCallback<long[]>()
    {
        public long[] command() throws Throwable
        {
            return searchInternal(searchTerm);
        }
    };
    return command.run();
}
 
Example 10
private void extractSearchTermResultsToList(List<AlfrescoImapFolder> folders, SearchTerm searchTerm, ArrayList<Long> resList)
{
    for (AlfrescoImapFolder folder : folders)
    {
        for (long l : folder.search(searchTerm))
        {
            resList.add(l);
        }
    }
}
 
Example 11
Source Project: NoraUi   Source File: MailSteps.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Valid activation email.
 *
 * @param mailHost
 *            example: imap.gmail.com
 * @param mailUser
 *            login of mail box
 * @param mailPassword
 *            password of mail box
 * @param senderMail
 *            sender of mail box
 * @param subjectMail
 *            subject of mail box
 * @param firstCssQuery
 *            the first matching element
 * @param conditions
 *            list of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
 * @throws TechnicalException
 *             is throws if you have a technical error (format, configuration, datas, ...) in NoraUi.
 *             Exception with message and with screenshot and with exception if functional error but no screenshot and no exception if technical error.
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
@Experimental(name = "validActivationEmail")
@RetryOnFailure(attempts = 3, delay = 60)
@Conditioned
@Et("Je valide le mail d'activation {string}(\\?)")
@And("I valid activation email {string}(\\?)")
public void validActivationEmail(String mailHost, String mailUser, String mailPassword, String senderMail, String subjectMail, String firstCssQuery, List<GherkinStepCondition> conditions)
        throws FailureException, TechnicalException {
    try {
        final Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imap");
        final Session session = Session.getDefaultInstance(props, null);
        final Store store = session.getStore("imaps");
        store.connect(mailHost, mailUser, mailPassword);
        final Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        final SearchTerm filterA = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        final SearchTerm filterB = new FromTerm(new InternetAddress(senderMail));
        final SearchTerm filterC = new SubjectTerm(subjectMail);
        final SearchTerm[] filters = { filterA, filterB, filterC };
        final SearchTerm searchTerm = new AndTerm(filters);
        final Message[] messages = inbox.search(searchTerm);
        for (final Message message : messages) {
            validateActivationLink(subjectMail, firstCssQuery, message);
        }
    } catch (final Exception e) {
        new Result.Failure<>("", Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_MAIL_ACTIVATION), subjectMail), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
    }
}
 
Example 12
Source Project: product-ei   Source File: MailToTransportUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Check a particular email has received to a given email folder by email subject.
 *
 * @param emailSubject - Email emailSubject to find email is in inbox or not
 * @return - found the email or not
 * @throws ESBMailTransportIntegrationTestException - Is thrown if an error occurred while reading the emails
 */
public static boolean isMailReceivedBySubject(String emailSubject, String folder)
        throws ESBMailTransportIntegrationTestException {
    boolean emailReceived = false;
    Folder mailFolder;
    Store store = getConnection();
    try {
        mailFolder = store.getFolder(folder);
        mailFolder.open(Folder.READ_WRITE);
        SearchTerm searchTerm = new AndTerm(new SubjectTerm(emailSubject), new BodyTerm(emailSubject));
        Message[] messages = mailFolder.search(searchTerm);
        for (Message message : messages) {
            if (message.getSubject().contains(emailSubject)) {
                log.info("Found the email emailSubject : " + emailSubject);
                emailReceived = true;
                break;
            }
        }
        return emailReceived;
    } catch (MessagingException ex) {
        log.error("Error when getting mail count ", ex);
        throw new ESBMailTransportIntegrationTestException("Error when getting mail count ", ex);
    } finally {
        if (store != null) {
            try {
                store.close();
            } catch (MessagingException e) {
                log.warn("Error when closing the store ", e);
            }
        }
    }
}
 
Example 13
Source Project: mail-importer   Source File: JavaxMailFolder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public JavaxMailMessage[] search(SearchTerm term)
    throws RuntimeMessagingException {
  try {
    return decorateMessageArray(delegate.search(term));
  } catch (MessagingException e) {
    throw new RuntimeMessagingException(e);
  }
}
 
Example 14
Source Project: mail-importer   Source File: JavaxMailFolder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public JavaxMailMessage[] search(SearchTerm term, Message[] msgs)
    throws RuntimeMessagingException {
  try {
    return decorateMessageArray(delegate.search(term, msgs));
  } catch (MessagingException e) {
    throw new RuntimeMessagingException(e);
  }
}
 
Example 15
Source Project: mail-importer   Source File: JavaxMailMessage.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean match(SearchTerm term) throws RuntimeMessagingException {
  try {
    return delegate.match(term);
  } catch (MessagingException e) {
    throw new RuntimeMessagingException(e);
  }
}
 
Example 16
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createSentDateTermBuilder(final int searchTerm) {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            return new SentDateTerm(searchTerm, parseDate(getParameters()));
        }
    };
}
 
Example 17
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createReceivedDateTermBuilder(final int searchTerm) {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            return new ReceivedDateTerm(searchTerm, parseDate(getParameters()));
        }
    };
}
 
Example 18
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createMessageSizeTermBuilder(final int searchTerm) {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            return new SizeTerm(searchTerm, SearchTermBuilder.parseInteger(getParameters()));
        }
    };
}
 
Example 19
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createORTermBuilder() {
return new SearchTermBuilder() {
         @Override
         public SearchTerm build() {
            return new OrTerm(new SubjectTerm(getParameters().get(0)),new SubjectTerm(getParameters().get(1)));
         }
     };	}
 
Example 20
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createHeaderTermBuilder() {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            return new HeaderTerm(getParameters().get(0), getParameters().get(1));
        }
    };
}
 
Example 21
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createSearchTermBuilder(final SearchTerm pSearchTerm) {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            return pSearchTerm;
        }
    };
}
 
Example 22
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createRecipientSearchTermBuilder(final Message.RecipientType type) {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            try {
                return new RecipientTerm(type, new InternetAddress(getParameters().get(0)));
            } catch (AddressException e) {
                throw new IllegalArgumentException("Address is not correct", e);
            }
        }
    };
}
 
Example 23
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createBodySearchTermBuilder() {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            String query = getParameters().get(0);
            return new BodyTerm(query);
        }
    };
}
 
Example 24
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createFromSearchTermBuilder() {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            try {
                return new FromTerm(new InternetAddress(getParameters().get(0)));
            } catch (AddressException e) {
                throw new IllegalArgumentException("Address is not correct", e);
            }
        }
    };
}
 
Example 25
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createSubjectTermBuilder() {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            return new SubjectTerm(getParameters().get(0));
        }
    };
}
 
Example 26
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createFlagSearchTermBuilder(final String pFlagName, final boolean pValue) {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            return createFlagSearchTerm(pFlagName, pValue);
        }
    };
}
 
Example 27
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createKeywordSearchTermBuilder(final SearchKey pKey) {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            return createFlagSearchTerm(getParameter(0), pKey == SearchKey.KEYWORD);
        }
    };
}
 
Example 28
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTerm createFlagSearchTerm(String pFlagName, boolean pValue) {
    Flags.Flag flag = toFlag(pFlagName);
    Flags flags = new javax.mail.Flags();
    if (null == flag) { // user flags
        flags.add(pFlagName);
    } else {
        flags.add(flag);
    }
    return new FlagTerm(flags, pValue);
}
 
Example 29
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createUidTermBuilder() {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            final List<IdRange> uidSetList = IdRange.parseRangeSequence(getParameter(0));
            return new UidSearchTerm(uidSetList);
        }
    };
}
 
Example 30
Source Project: greenmail   Source File: SearchTermBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static SearchTermBuilder createSequenceSetTermBuilder() {
    return new SearchTermBuilder() {
        @Override
        public SearchTerm build() {
            final List<IdRange> idRanges = IdRange.parseRangeSequence(getParameter(0));
            return new MessageNumberSearchTerm(idRanges);
        }
    };
}