Java 类javax.mail.search.SubjectTerm 实例源码

项目:product-ei    文件:GreenMailServer.java   
/**
 * 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;
}
项目:Java-9-Programming-Blueprints    文件:Rule.java   
private SearchTerm createFieldSearchTerm(String f) {
    switch (f.toLowerCase()) {
        case "from":
            return new FromStringTerm(matchingText);
        case "cc":
            return new RecipientStringTerm(Message.RecipientType.CC, matchingText);
        case "to":
            return new RecipientStringTerm(Message.RecipientType.TO, matchingText);
        case "body":
            return new BodyTerm(matchingText);
        case "subject":
            return new SubjectTerm(matchingText);
        default:
            return null;
    }
}
项目:product-ei    文件:MailToTransportUtil.java   
/**
 * 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);
            }
        }
    }
}
项目:development    文件:WebTester.java   
/**
 * Reads the latest email with the given subject from the email inbox.
 * 
 * @param subject
 *            the email subject
 * @return the email body or null if no email was found
 * @throws Exception
 */
public String readLatestEmailWithSubject(String subject) throws Exception {
    Store store = mailSession.getStore();
    store.connect();

    Folder folder = store.getFolder(MAIL_INBOX);
    folder.open(Folder.READ_WRITE);

    Message[] messages = null;

    messages = folder.search(new SubjectTerm(subject));

    String body = null;
    if (messages.length > 0) {
        Message latest = messages[0];

        for (Message m : messages) {
            if (latest.getSentDate().compareTo(m.getSentDate()) < 0) {
                latest = m;
            }
        }
        body = (String) latest.getContent();
    }

    folder.close(false);
    store.close();

    return body;
}
项目:read-open-source-code    文件:MailConnection.java   
/**
* Set filter on subject.
* @param subject messages will be filtered on subject
* @param notTerm negate condition
*/
  public void setSubjectTerm(String subject, boolean notTerm) {
    if (!Const.isEmpty(subject)) {
        if(notTerm)
            addSearchTerm(new NotTerm(new SubjectTerm(subject)));
        else
            addSearchTerm(new SubjectTerm(subject));
    }
  }
项目:kettle-4.4.0-stable    文件:MailConnection.java   
/**
* Set filter on subject.
* @param subject messages will be filtered on subject
* @param notTerm negate condition
*/
  public void setSubjectTerm(String subject, boolean notTerm) {
    if (!Const.isEmpty(subject)) {
        if(notTerm)
            addSearchTerm(new NotTerm(new SubjectTerm(subject)));
        else
            addSearchTerm(new SubjectTerm(subject));
    }
  }
项目:kettle-trunk    文件:MailConnection.java   
/**
* Set filter on subject.
* @param subject messages will be filtered on subject
* @param notTerm negate condition
*/
  public void setSubjectTerm(String subject, boolean notTerm) {
    if (!Const.isEmpty(subject)) {
        if(notTerm)
            addSearchTerm(new NotTerm(new SubjectTerm(subject)));
        else
            addSearchTerm(new SubjectTerm(subject));
    }
  }
项目:pentaho-kettle    文件:MailConnection.java   
/**
 * Set filter on subject.
 *
 * @param subject
 *          messages will be filtered on subject
 * @param notTerm
 *          negate condition
 */
public void setSubjectTerm( String subject, boolean notTerm ) {
  if ( !Utils.isEmpty( subject ) ) {
    if ( notTerm ) {
      addSearchTerm( new NotTerm( new SubjectTerm( subject ) ) );
    } else {
      addSearchTerm( new SubjectTerm( subject ) );
    }
  }
}
项目:oscm    文件:WebTester.java   
/**
 * Reads the latest email with the given subject from the email inbox.
 * 
 * @param subject
 *            the email subject
 * @return the email body or null if no email was found
 * @throws Exception
 */
public String readLatestEmailWithSubject(String subject) throws Exception {
    Store store = mailSession.getStore();
    store.connect();

    Folder folder = store.getFolder(MAIL_INBOX);
    folder.open(Folder.READ_WRITE);

    Message[] messages = null;

    messages = folder.search(new SubjectTerm(subject));

    String body = null;
    if (messages.length > 0) {
        Message latest = messages[0];

        for (Message m : messages) {
            if (latest.getSentDate().compareTo(m.getSentDate()) < 0) {
                latest = m;
            }
        }
        body = (String) latest.getContent();
    }

    folder.close(false);
    store.close();

    return body;
}
项目:Camel    文件:SearchTermBuilder.java   
public SearchTermBuilder subject(Op op, String pattern) {
    SearchTerm st = new SubjectTerm(pattern);
    addTerm(op, st);
    return this;
}
项目:opennmszh    文件:MailTransportMonitor.java   
/**
 * After a mailbox has been opened, search through the retrieved messages
 * for a matching subject.
 * 
 * @param mailParms
 * @param mailFolder
 * @return a PollStatus indicative of the success of matching a subject or just retieving
 *         mail folder contents... dependent on configuration.
 */
private PollStatus processMailSubject(final MailTransportParameters mailParms, final Folder mailFolder) {
    PollStatus status = PollStatus.unknown();
    try {
        final String subject = computeMatchingSubject(mailParms);
        if (mailFolder.isOpen() && subject != null) {
            final Message[] mailMessages = mailFolder.getMessages();
            final SearchTerm searchTerm = new SubjectTerm(subject);
            final SearchTerm deleteTerm = new HeaderTerm(MTM_HEADER_KEY, m_headerValue);

            LogUtils.debugf(this, "searchMailSubject: searching %d message(s) for subject '%s'", mailMessages.length, subject);

            boolean delete = false;
            boolean found = false;
            for (int i = 1; i <= mailMessages.length; i++) {
                final Message mailMessage = mailFolder.getMessage(i);

                LogUtils.debugf(this, "searchMailSubject: retrieved message subject '%s'", mailMessage.getSubject());

                if (mailMessage.match(searchTerm)) {
                    found = true;
                    LogUtils.debugf(this, "searchMailSubject: message with subject '%s' found.", subject);

                    if (mailParms.isEnd2EndTestInProgress()) {
                        if (!delete) LogUtils.debugf(this, "searchMailSubject: flagging message with subject '%s' for deletion for end2end test.", subject);
                        delete = true;
                    }
                }

                final boolean deleteAllMail = mailParms.getReadTest().isDeleteAllMail();
    final boolean foundMTMHeader = mailMessage.match(deleteTerm);
    LogUtils.debugf(this, "searchMailSubject: deleteAllMail = %s, MTM header found = %s", Boolean.toString(deleteAllMail), Boolean.toString(foundMTMHeader));

    if (deleteAllMail) {
        if (!delete) LogUtils.debugf(this, "searchMailSubject: flagging message with subject '%s' for deletion because deleteAllMail is set.", subject);
        delete = true;
    } else if (foundMTMHeader) {
        if (!delete) LogUtils.debugf(this, "searchMailSubject: flagging message with subject '%s' for deletion because we sent it (found header %s=%s)", subject, MTM_HEADER_KEY, m_headerValue);
        delete = true;
    }

    if (delete) {
        mailMessage.setFlag(Flag.DELETED, true);
    }

                // since we want to delete old messages matchin MTM_HEADER_KEY, we can't break early
                // if (found) break;
            }

            if (!found) {
                LogUtils.debugf(this, "searchMailSubject: message with subject: '%s' NOT found.", subject);
                status = PollStatus.down("searchMailSubject: matching test message: '"+subject+"', not found.");
            } else {
                status = PollStatus.available();
            }
        }
    } catch (final MessagingException e) {
        return PollStatus.down(e.getLocalizedMessage());
    }

    return status;
}
项目:OpenNMS    文件:MailTransportMonitor.java   
/**
 * After a mailbox has been opened, search through the retrieved messages
 * for a matching subject.
 * 
 * @param mailParms
 * @param mailFolder
 * @return a PollStatus indicative of the success of matching a subject or just retieving
 *         mail folder contents... dependent on configuration.
 */
private PollStatus processMailSubject(final MailTransportParameters mailParms, final Folder mailFolder) {
    PollStatus status = PollStatus.unknown();
    try {
        final String subject = computeMatchingSubject(mailParms);
        if (mailFolder.isOpen() && subject != null) {
            final Message[] mailMessages = mailFolder.getMessages();
            final SearchTerm searchTerm = new SubjectTerm(subject);
            final SearchTerm deleteTerm = new HeaderTerm(MTM_HEADER_KEY, m_headerValue);

            LogUtils.debugf(this, "searchMailSubject: searching %d message(s) for subject '%s'", mailMessages.length, subject);

            boolean delete = false;
            boolean found = false;
            for (int i = 1; i <= mailMessages.length; i++) {
                final Message mailMessage = mailFolder.getMessage(i);

                LogUtils.debugf(this, "searchMailSubject: retrieved message subject '%s'", mailMessage.getSubject());

                if (mailMessage.match(searchTerm)) {
                    found = true;
                    LogUtils.debugf(this, "searchMailSubject: message with subject '%s' found.", subject);

                    if (mailParms.isEnd2EndTestInProgress()) {
                        if (!delete) LogUtils.debugf(this, "searchMailSubject: flagging message with subject '%s' for deletion for end2end test.", subject);
                        delete = true;
                    }
                }

                final boolean deleteAllMail = mailParms.getReadTest().isDeleteAllMail();
    final boolean foundMTMHeader = mailMessage.match(deleteTerm);
    LogUtils.debugf(this, "searchMailSubject: deleteAllMail = %s, MTM header found = %s", Boolean.toString(deleteAllMail), Boolean.toString(foundMTMHeader));

    if (deleteAllMail) {
        if (!delete) LogUtils.debugf(this, "searchMailSubject: flagging message with subject '%s' for deletion because deleteAllMail is set.", subject);
        delete = true;
    } else if (foundMTMHeader) {
        if (!delete) LogUtils.debugf(this, "searchMailSubject: flagging message with subject '%s' for deletion because we sent it (found header %s=%s)", subject, MTM_HEADER_KEY, m_headerValue);
        delete = true;
    }

    if (delete) {
        mailMessage.setFlag(Flag.DELETED, true);
    }

                // since we want to delete old messages matchin MTM_HEADER_KEY, we can't break early
                // if (found) break;
            }

            if (!found) {
                LogUtils.debugf(this, "searchMailSubject: message with subject: '%s' NOT found.", subject);
                status = PollStatus.down("searchMailSubject: matching test message: '"+subject+"', not found.");
            } else {
                status = PollStatus.available();
            }
        }
    } catch (final MessagingException e) {
        return PollStatus.down(e.getLocalizedMessage());
    }

    return status;
}