javax.mail.search.BodyTerm Java Examples

The following examples show how to use javax.mail.search.BodyTerm. 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 File: GreenMailServer.java    From micro-integrator with Apache License 2.0 9 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
Source File: GreenMailServer.java    From product-ei with 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 #3
Source File: SearchTermBuilder.java    From greenmail with 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 #4
Source File: MailToTransportUtil.java    From micro-integrator with 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 #5
Source File: MailConnection.java    From hop with Apache License 2.0 5 votes vote down vote up
/**
 * Search all messages with body containing the word bodyfilter
 *
 * @param bodyfilter
 * @param notTerm    negate condition
 */
public void setBodyTerm( String bodyfilter, boolean notTerm ) {
  if ( !Utils.isEmpty( bodyfilter ) ) {
    if ( notTerm ) {
      addSearchTerm( new NotTerm( new BodyTerm( bodyfilter ) ) );
    } else {
      addSearchTerm( new BodyTerm( bodyfilter ) );
    }
  }
}
 
Example #6
Source File: MailToTransportUtil.java    From product-ei with 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 #7
Source File: SearchTermBuilder.java    From greenmail with 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 #8
Source File: MailConnection.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Search all messages with body containing the word bodyfilter
 *
 * @param bodyfilter
 * @param notTerm
 *          negate condition
 */
public void setBodyTerm( String bodyfilter, boolean notTerm ) {
  if ( !Utils.isEmpty( bodyfilter ) ) {
    if ( notTerm ) {
      addSearchTerm( new NotTerm( new BodyTerm( bodyfilter ) ) );
    } else {
      addSearchTerm( new BodyTerm( bodyfilter ) );
    }
  }
}