Search in sources :

Example 16 with SearchTerm

use of javax.mail.search.SearchTerm in project jodd by oblac.

the class EmailFilterTest method testAnd2.

@Test
public void testAnd2() {
    EmailFilter emailFilter = filter().from("from").to("to");
    SearchTerm expected = new AndTerm(new FromStringTerm("from"), new RecipientStringTerm(Message.RecipientType.TO, "to"));
    assertEquals(expected, emailFilter.searchTerm);
}
Also used : AndTerm(javax.mail.search.AndTerm) FromStringTerm(javax.mail.search.FromStringTerm) RecipientStringTerm(javax.mail.search.RecipientStringTerm) SearchTerm(javax.mail.search.SearchTerm) Test(org.junit.Test)

Example 17 with SearchTerm

use of javax.mail.search.SearchTerm in project jodd by oblac.

the class EmailFilterTest method testAndOrNotAlt.

@Test
public void testAndOrNotAlt() {
    EmailFilter emailFilter = filter().or(filter().and(filter().from("from"), filter().to("to")), filter().not(filter().subject("subject")), filter().from("from2"));
    SearchTerm expected = new OrTerm(new SearchTerm[] { new AndTerm(new FromStringTerm("from"), new RecipientStringTerm(Message.RecipientType.TO, "to")), new NotTerm(new SubjectTerm("subject")), new FromStringTerm("from2") });
    assertEquals(expected, emailFilter.searchTerm);
}
Also used : AndTerm(javax.mail.search.AndTerm) OrTerm(javax.mail.search.OrTerm) FromStringTerm(javax.mail.search.FromStringTerm) RecipientStringTerm(javax.mail.search.RecipientStringTerm) NotTerm(javax.mail.search.NotTerm) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) Test(org.junit.Test)

Example 18 with SearchTerm

use of javax.mail.search.SearchTerm in project jodd by oblac.

the class EmailFilterTest method testOr2Alt.

@Test
public void testOr2Alt() {
    EmailFilter emailFilter = filter().or().from("from").to("to");
    SearchTerm expected = new OrTerm(new FromStringTerm("from"), new RecipientStringTerm(Message.RecipientType.TO, "to"));
    assertEquals(expected, emailFilter.searchTerm);
}
Also used : OrTerm(javax.mail.search.OrTerm) FromStringTerm(javax.mail.search.FromStringTerm) RecipientStringTerm(javax.mail.search.RecipientStringTerm) SearchTerm(javax.mail.search.SearchTerm) Test(org.junit.Test)

Example 19 with SearchTerm

use of javax.mail.search.SearchTerm in project nhin-d by DirectProject.

the class MessageServiceImplService method requestStatus.

@Override
public StatusResponseType requestStatus(StatusRefType body) {
    List<String> msgs = body.getMessageID();
    StatusResponseType response = new StatusResponseType();
    try {
        checkAuth(response);
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(imapProps, auth);
        session.setDebug(true);
        if (msgs.size() > 0) {
            Store store = session.getStore(new javax.mail.URLName("imaps://" + username));
            store.connect(getImapHost(), Integer.valueOf(getImapPort()).intValue(), username, password);
            for (int x = 0; x < msgs.size(); x++) {
                String msgid = msgs.get(x);
                MessageIDTerm messageIdTerm = new MessageIDTerm(msgid);
                IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
                folder.open(Folder.READ_ONLY);
                SearchTerm st = messageIdTerm;
                IMAPMessage[] msgsearch = (IMAPMessage[]) folder.search(st);
                if (msgsearch.length > 0) {
                    Flags flags = msgsearch[0].getFlags();
                    Flag[] inboxflags = flags.getSystemFlags();
                    String[] listofflags = new String[inboxflags.length];
                    listofflags = setSystemFlags(inboxflags);
                    setMessageIdStatus(msgid, listofflags, response.getMessageIDAndStatus());
                }
            }
        }
    } catch (AddressException e) {
        log.error(e);
    } catch (MessagingException e) {
        log.error(e);
    } catch (Exception e) {
        log.error(e);
    }
    return response;
}
Also used : MessageIDTerm(javax.mail.search.MessageIDTerm) MessagingException(javax.mail.MessagingException) IMAPFolder(com.sun.mail.imap.IMAPFolder) Store(javax.mail.Store) Flags(javax.mail.Flags) SearchTerm(javax.mail.search.SearchTerm) Flag(javax.mail.Flags.Flag) StatusResponseType(org.nhindirect.schema.edge.ws.StatusResponseType) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) IOException(java.io.IOException) AddressException(javax.mail.internet.AddressException) IMAPMessage(com.sun.mail.imap.IMAPMessage) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session)

Example 20 with SearchTerm

use of javax.mail.search.SearchTerm in project opennms by OpenNMS.

the class MailTransportMonitor method processMailSubject.

/**
 * 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);
            LOG.debug("searchMailSubject: searching {} message(s) for subject '{}'", mailMessages.length, subject);
            boolean delete = false;
            boolean found = false;
            for (int i = 1; i <= mailMessages.length; i++) {
                final Message mailMessage = mailFolder.getMessage(i);
                LOG.debug("searchMailSubject: retrieved message subject '{}'", mailMessage.getSubject());
                if (mailMessage.match(searchTerm)) {
                    found = true;
                    LOG.debug("searchMailSubject: message with subject '{}' found.", subject);
                    if (mailParms.isEnd2EndTestInProgress()) {
                        if (!delete)
                            LOG.debug("searchMailSubject: flagging message with subject '{}' for deletion for end2end test.", subject);
                        delete = true;
                    }
                }
                final boolean deleteAllMail = mailParms.getReadTest().getDeleteAllMail();
                final boolean foundMTMHeader = mailMessage.match(deleteTerm);
                LOG.debug("searchMailSubject: deleteAllMail = {}, MTM header found = {}", Boolean.toString(deleteAllMail), Boolean.toString(foundMTMHeader));
                if (deleteAllMail) {
                    if (!delete)
                        LOG.debug("searchMailSubject: flagging message with subject '{}' for deletion because deleteAllMail is set.", subject);
                    delete = true;
                } else if (foundMTMHeader) {
                    if (!delete)
                        LOG.debug("searchMailSubject: flagging message with subject '{}' for deletion because we sent it (found header {}={})", 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) {
                LOG.debug("searchMailSubject: message with subject: '{}' 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;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) HeaderTerm(javax.mail.search.HeaderTerm) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm)

Aggregations

SearchTerm (javax.mail.search.SearchTerm)38 RecipientStringTerm (javax.mail.search.RecipientStringTerm)9 FromStringTerm (javax.mail.search.FromStringTerm)8 Test (org.junit.Test)8 OrTerm (javax.mail.search.OrTerm)6 SubjectTerm (javax.mail.search.SubjectTerm)6 AndTerm (javax.mail.search.AndTerm)5 Message (javax.mail.Message)3 InternetAddress (javax.mail.internet.InternetAddress)3 MimeMessage (javax.mail.internet.MimeMessage)3 FlagTerm (javax.mail.search.FlagTerm)3 HeaderTerm (javax.mail.search.HeaderTerm)3 NotTerm (javax.mail.search.NotTerm)3 IMAPFolder (com.sun.mail.imap.IMAPFolder)2 Date (java.util.Date)2 Flags (javax.mail.Flags)2 MessagingException (javax.mail.MessagingException)2 BodyTerm (javax.mail.search.BodyTerm)2 MessageIDTerm (javax.mail.search.MessageIDTerm)2 ReceivedDateTerm (javax.mail.search.ReceivedDateTerm)2