Example usage for javax.mail.search FromTerm FromTerm

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

Introduction

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

Prototype

public FromTerm(Address address) 

Source Link

Document

Constructor

Usage

From source file:edu.stanford.muse.email.Filter.java

public SearchTerm convertToSearchTerm(boolean useReceivedDateTerms) {
    // FLAG DEBUG: end date = null
    //endDate = null;
    SearchTerm sentOnlyTerm = null;//from  ww w  .  j a  va 2  s  .  co m
    if (sentMessagesOnly) {
        List<SearchTerm> fromAddrTerms = new ArrayList<SearchTerm>();
        if (ownCI != null) {
            for (String e : ownCI.emails)
                try {
                    fromAddrTerms.add(new FromTerm(new InternetAddress(e, ""))); // the name doesn't matter for equality of InternetAddress            
                } catch (Exception ex) {
                    Util.print_exception(ex, log);
                }

            // or all the from terms (not and)
            if (fromAddrTerms.size() >= 1) {
                sentOnlyTerm = fromAddrTerms.get(0);
                for (int i = 1; i < fromAddrTerms.size(); i++)
                    sentOnlyTerm = new OrTerm(sentOnlyTerm, fromAddrTerms.get(i));
            }
        }
    }

    SearchTerm result = sentOnlyTerm;

    if (startDate != null) {
        SearchTerm startTerm = useReceivedDateTerms ? new ReceivedDateTerm(ComparisonTerm.GT, startDate)
                : new SentDateTerm(ComparisonTerm.GT, startDate);
        if (result != null)
            result = new AndTerm(result, startTerm);
        else
            result = startTerm;
    }

    if (endDate != null) {
        SearchTerm endTerm = useReceivedDateTerms ? new ReceivedDateTerm(ComparisonTerm.LT, endDate)
                : new SentDateTerm(ComparisonTerm.LT, endDate);
        if (result != null)
            result = new AndTerm(result, endTerm);
        else
            result = endTerm;
    }

    if (keywords != null)
        for (String s : keywords) {
            if (Util.nullOrEmpty(s))
                continue;
            // look for this keyword in both subject and body
            SearchTerm sTerm = new OrTerm(new BodyTerm(s), new SubjectTerm(s));
            if (result != null)
                result = new AndTerm(result, sTerm);
            else
                result = sTerm;
        }
    return result;
}

From source file:org.jevis.emaildatasource.EmailForTest.java

public void testMail() throws MessagingException {
    List<InputStream> answerList = new ArrayList<InputStream>();

    _userName = "artur.iablokov@envidatec.com";
    _password = "na733aya";
    _host = "imap.1und1.de";

    Properties props = new Properties();
    props.put("mail.debug", "true");
    props.put("mail.store.protocol", "imaps");
    _session = Session.getInstance(props);
    _store = _session.getStore();//  ww w  . ja  va  2 s . c  o  m
    _store.connect(_host, _userName, _password);
    if (!_store.isConnected()) {
        System.out.println("Connected not possible");
    }
    _folderName = "INBOX"; // TODO
    _folder = _store.getFolder(_folderName);
    _folder.open(Folder.READ_ONLY);

    System.out.println("//////////Folder open!/////");

    InputStream answer = null;

    //channel parameter bekommen
    String sender = "support@jevis.de";
    String subject = "testinfo";
    Date lastReadout = new Date(1459658993827L);
    System.out.println("channel parameters: " + sender + " " + subject + " " + lastReadout);
    //richtige E-Mail(-s) finden
    SearchTerm newerThan = new ReceivedDateTerm(ComparisonTerm.GT, lastReadout);
    SearchTerm senderTerm = null;
    try {
        senderTerm = new FromTerm(new InternetAddress(sender));
    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    SearchTerm subjectTerm = new SubjectTerm(subject);
    SearchTerm andTerm = new AndTerm(newerThan, new AndTerm(senderTerm, subjectTerm));
    System.out.println(andTerm.toString());
    List<Message> messageList = null;
    try {
        messageList = Arrays.asList(_folder.search(andTerm));
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //
    _folder.close(false);
    System.out.println("Folder closed");
    _store.close();
    System.out.println("Store closed");

    /**
     * List<File> attachments = new ArrayList<File>(); for (Message message
     * : messageList) { try { Multipart multipart = (Multipart)
     * message.getContent(); for (int i = 0; i < multipart.getCount(); i++)
     * { BodyPart bodyPart = multipart.getBodyPart(i); if
     * (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) &&
     * !StringUtils.isNotBlank(bodyPart.getFileName())) { // !Checks if a
     * String is not empty (""), not null and not whitespace only. continue;
     * // dealing with attachments only } InputStream is =
     * bodyPart.getInputStream(); File file = new File("/tmp/" +
     * bodyPart.getFileName()); FileOutputStream fos = new
     * FileOutputStream(file); byte[] buf = new byte[4096]; int bytesRead;
     * while ((bytesRead = is.read(buf)) != -1) { fos.write(buf, 0,
     * bytesRead); } fos.close(); attachments.add(file);
        }*
     */

    /*for (int i = 0; i < answerList.size(); i++) {
        System.out.print(answerList.get(i).toString());
        }*/
}

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

@Test
public void testIdleWithServerCustomSearch() throws Exception {
    ImapMailReceiver receiver = new ImapMailReceiver(
            "imap://user:pw@localhost:" + imapIdleServer.getPort() + "/INBOX");
    receiver.setSearchTermStrategy((supportedFlags, folder) -> {
        try {//from  w w w.  ja  va2s  . c o  m
            FromTerm fromTerm = new FromTerm(new InternetAddress("bar@baz"));
            return new AndTerm(fromTerm, new FlagTerm(new Flags(Flag.SEEN), false));
        } catch (AddressException e) {
            throw new RuntimeException(e);
        }
    });
    testIdleWithServerGuts(receiver, false);
}