List of usage examples for javax.mail.search ReceivedDateTerm ReceivedDateTerm
public ReceivedDateTerm(int comparison, Date date)
From source file:search.java
public static void main(String argv[]) { int optind;/* w ww .j a v a 2s.co m*/ String subject = null; String from = null; boolean or = false; boolean today = false; int size = -1; for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { host = argv[++optind]; } else if (argv[optind].equals("-U")) { user = argv[++optind]; } else if (argv[optind].equals("-P")) { password = argv[++optind]; } else if (argv[optind].equals("-or")) { or = true; } else if (argv[optind].equals("-D")) { debug = true; } else if (argv[optind].equals("-f")) { mbox = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-subject")) { subject = argv[++optind]; } else if (argv[optind].equals("-from")) { from = argv[++optind]; } else if (argv[optind].equals("-today")) { today = true; } else if (argv[optind].equals("-size")) { size = Integer.parseInt(argv[++optind]); } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: search [-D] [-L url] [-T protocol] [-H host] " + "[-U user] [-P password] [-f mailbox] " + "[-subject subject] [-from from] [-or] [-today]"); System.exit(1); } else { break; } } try { if ((subject == null) && (from == null) && !today && size < 0) { System.out.println("Specify either -subject, -from, -today, or -size"); System.exit(1); } // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); session.setDebug(debug); // Get a Store object Store store = null; if (url != null) { URLName urln = new URLName(url); store = session.getStore(urln); store.connect(); } else { if (protocol != null) store = session.getStore(protocol); else store = session.getStore(); // Connect if (host != null || user != null || password != null) store.connect(host, user, password); else store.connect(); } // Open the Folder Folder folder = store.getDefaultFolder(); if (folder == null) { System.out.println("Cant find default namespace"); System.exit(1); } folder = folder.getFolder(mbox); if (folder == null) { System.out.println("Invalid folder"); System.exit(1); } folder.open(Folder.READ_ONLY); SearchTerm term = null; if (subject != null) term = new SubjectTerm(subject); if (from != null) { FromStringTerm fromTerm = new FromStringTerm(from); if (term != null) { if (or) term = new OrTerm(term, fromTerm); else term = new AndTerm(term, fromTerm); } else term = fromTerm; } if (today) { Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); c.set(Calendar.AM_PM, Calendar.AM); ReceivedDateTerm startDateTerm = new ReceivedDateTerm(ComparisonTerm.GE, c.getTime()); c.add(Calendar.DATE, 1); // next day ReceivedDateTerm endDateTerm = new ReceivedDateTerm(ComparisonTerm.LT, c.getTime()); SearchTerm dateTerm = new AndTerm(startDateTerm, endDateTerm); if (term != null) { if (or) term = new OrTerm(term, dateTerm); else term = new AndTerm(term, dateTerm); } else term = dateTerm; } if (size >= 0) { SizeTerm sizeTerm = new SizeTerm(ComparisonTerm.GT, size); if (term != null) { if (or) term = new OrTerm(term, sizeTerm); else term = new AndTerm(term, sizeTerm); } else term = sizeTerm; } Message[] msgs = folder.search(term); System.out.println("FOUND " + msgs.length + " MESSAGES"); if (msgs.length == 0) // no match System.exit(1); // Use a suitable FetchProfile FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); folder.fetch(msgs, fp); for (int i = 0; i < msgs.length; i++) { System.out.println("--------------------------"); System.out.println("MESSAGE #" + (i + 1) + ":"); dumpPart(msgs[i]); } folder.close(false); store.close(); } catch (Exception ex) { System.out.println("Oops, got exception! " + ex.getMessage()); ex.printStackTrace(); } System.exit(1); }
From source file:search.java
public static void main(String argv[]) { int optind;/*ww w . java 2 s . c om*/ String subject = null; String from = null; boolean or = false; boolean today = false; for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { host = argv[++optind]; } else if (argv[optind].equals("-U")) { user = argv[++optind]; } else if (argv[optind].equals("-P")) { password = argv[++optind]; } else if (argv[optind].equals("-or")) { or = true; } else if (argv[optind].equals("-D")) { debug = true; } else if (argv[optind].equals("-f")) { mbox = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-subject")) { subject = argv[++optind]; } else if (argv[optind].equals("-from")) { from = argv[++optind]; } else if (argv[optind].equals("-today")) { today = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: search [-D] [-L url] [-T protocol] [-H host] " + "[-U user] [-P password] [-f mailbox] " + "[-subject subject] [-from from] [-or] [-today]"); System.exit(1); } else { break; } } try { if ((subject == null) && (from == null) && !today) { System.out.println("Specify either -subject, -from or -today"); System.exit(1); } // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); session.setDebug(debug); // Get a Store object Store store = null; if (url != null) { URLName urln = new URLName(url); store = session.getStore(urln); store.connect(); } else { if (protocol != null) store = session.getStore(protocol); else store = session.getStore(); // Connect if (host != null || user != null || password != null) store.connect(host, user, password); else store.connect(); } // Open the Folder Folder folder = store.getDefaultFolder(); if (folder == null) { System.out.println("Cant find default namespace"); System.exit(1); } folder = folder.getFolder(mbox); if (folder == null) { System.out.println("Invalid folder"); System.exit(1); } folder.open(Folder.READ_ONLY); SearchTerm term = null; if (subject != null) term = new SubjectTerm(subject); if (from != null) { FromStringTerm fromTerm = new FromStringTerm(from); if (term != null) { if (or) term = new OrTerm(term, fromTerm); else term = new AndTerm(term, fromTerm); } else term = fromTerm; } if (today) { ReceivedDateTerm dateTerm = new ReceivedDateTerm(ComparisonTerm.EQ, new Date()); if (term != null) { if (or) term = new OrTerm(term, dateTerm); else term = new AndTerm(term, dateTerm); } else term = dateTerm; } Message[] msgs = folder.search(term); System.out.println("FOUND " + msgs.length + " MESSAGES"); if (msgs.length == 0) // no match System.exit(1); // Use a suitable FetchProfile FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); folder.fetch(msgs, fp); for (int i = 0; i < msgs.length; i++) { System.out.println("--------------------------"); System.out.println("MESSAGE #" + (i + 1) + ":"); dumpPart(msgs[i]); } folder.close(false); store.close(); } catch (Exception ex) { System.out.println("Oops, got exception! " + ex.getMessage()); ex.printStackTrace(); } System.exit(1); }
From source file:com.sonicle.webtop.mail.bol.model.ImapQuery.java
public static SearchTerm toSearchTerm(String allFlagStrings[], List<Tag> atags, QueryObj query, DateTimeZone timezone) {/* www . j a va 2s . c o m*/ SearchTerm searchTerm = null; ArrayList<SearchTerm> terms = new ArrayList<SearchTerm>(); if (query != null) { ArrayList<Condition> conditionsList = query.conditions; String allText = query.allText; if (allText != null && allText.trim().length() > 0) { SearchTerm defaultterms[] = toDefaultSearchTerm(allText); terms.add(new OrTerm(defaultterms)); } conditionsList.forEach(condition -> { String key = condition.keyword; String value = condition.value; if (key.equals(FROM)) { terms.add(new FromStringTerm(value)); } else if (key.equals(TO)) { terms.add(new RecipientStringTerm(Message.RecipientType.TO, value)); } else if (key.equals(SUBJECT)) { terms.add(new SubjectTerm(value)); } else if (key.equals(MESSAGE)) { terms.add(new BodyTerm(value)); } else if (key.equals(EVERYWHERE)) { SearchTerm anyterms[] = toAnySearchTerm(value); terms.add(new OrTerm(anyterms)); } else if (key.equals(AFTER)) { Date afterDate = ImapQuery.parseDate(value, timezone); terms.add(new ReceivedDateTerm(DateTerm.GE, afterDate)); terms.add(new SentDateTerm(DateTerm.GE, afterDate)); } else if (key.equals(BEFORE)) { Date beforeDate = ImapQuery.parseDate(value, timezone); terms.add(new ReceivedDateTerm(DateTerm.LE, beforeDate)); terms.add(new SentDateTerm(DateTerm.LE, beforeDate)); } else if (value.equals(ATTACHMENT)) { } else if (value.equals(UNREAD)) { terms.add(new FlagTerm(new Flags(Flag.SEEN), false)); } else if (value.equals(FLAGGED)) { FlagTerm fts[] = new FlagTerm[allFlagStrings.length + 1]; fts[0] = new FlagTerm(new Flags(Flag.FLAGGED), true); for (int i = 0; i < allFlagStrings.length; ++i) fts[i + 1] = new FlagTerm(new Flags(allFlagStrings[i]), true); terms.add(new OrTerm(fts)); } else if (value.equals(TAGGED)) { FlagTerm fts[] = new FlagTerm[atags.size()]; int i = 0; for (Tag tag : atags) fts[i++] = new FlagTerm(new Flags(tag.getTagId()), true); terms.add(new OrTerm(fts)); } else if (value.equals(UNANSWERED)) { terms.add(new FlagTerm(new Flags(Flag.ANSWERED), false)); } else if (value.equals(PRIORITY)) { HeaderTerm p1 = new HeaderTerm("X-Priority", "1"); HeaderTerm p2 = new HeaderTerm("X-Priority", "2"); terms.add(new OrTerm(p1, p2)); } }); } int n = terms.size(); if (n == 1) { searchTerm = terms.get(0); } else if (n > 1) { SearchTerm vterms[] = new SearchTerm[n]; terms.toArray(vterms); searchTerm = new AndTerm(vterms); } return searchTerm; }
From source file:com.intranet.intr.inbox.EmpControllerInbox.java
public correoNoLeidos ltaRecibidos2(String name, int num) { correoNoLeidos lta = new correoNoLeidos(); Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); try {//from ww w . j a v a 2 s. c o m users u = usuarioService.getByLogin(name); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.1and1.es", u.getCorreoUsuario(), u.getCorreoContreasenna()); System.out.println("ola" + store); Folder inbox = store.getFolder("Inbox"); inbox.open(Folder.READ_ONLY); Calendar fecha3 = Calendar.getInstance(); fecha3.roll(Calendar.MONTH, false); Message msg[] = inbox.search(new ReceivedDateTerm(ComparisonTerm.GT, fecha3.getTime())); //Message msg[] = inbox.getMessages(); System.out.println("MAILS: " + msg.length); for (Message message : msg) { try { if (num == message.getMessageNumber()) { //message.setFlag(Flags.Flag.SEEN, true); lta.setNum(message.getMessageNumber()); lta.setFecha(message.getSentDate().toString()); lta.setDe(message.getFrom()[0].toString()); lta.setAsunto(message.getSubject().toString()); correoNoLeidos co = new correoNoLeidos(); List<String> arc = new ArrayList<String>(); List<String> rar = new ArrayList<String>(); correoNoLeidos cc = analizaParteDeMensaje2(co, arc, rar, message); lta.setImagenes(cc.getImagenes()); lta.setRar(cc.getRar()); lta.setContenido(cc.getContenido()); String cont = ""; String contenido = ""; //lta.setContenido(analizaParteDeMensaje2(message)); } } catch (Exception e) { System.out.println("No Information"); } } } catch (MessagingException e) { System.out.println(e.toString()); } return lta; }
From source file:edu.stanford.muse.email.Filter.java
public SearchTerm convertToSearchTerm(boolean useReceivedDateTerms) { // FLAG DEBUG: end date = null //endDate = null; SearchTerm sentOnlyTerm = null;/* w w w . ja v a2 s . c o 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:com.intranet.intr.inbox.EmpControllerInbox.java
@RequestMapping(value = "/EajaxtestNoL", method = RequestMethod.GET) public @ResponseBody String ajaxtestNoL(Principal principal) { String name = principal.getName(); String result = ""; try {/*from ww w. j a v a 2 s. co m*/ users u = usuarioService.getByLogin(name); Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.1and1.es", u.getCorreoUsuario(), u.getCorreoContreasenna()); System.out.println("ola" + store); Folder inbox = store.getFolder("Inbox"); inbox.open(Folder.READ_ONLY); FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false); Calendar fecha3 = Calendar.getInstance(); fecha3.roll(Calendar.MONTH, false); Message msg[] = inbox.search(new ReceivedDateTerm(ComparisonTerm.GT, fecha3.getTime())); //Message msg[] = inbox.search(ft); System.out.println("MAILS: " + msg.length); for (Message message : msg) { try { /*System.out.println("DATE: "+message.getSentDate().toString()); System.out.println("FROM: "+message.getFrom()[0].toString()); System.out.println("SUBJECT: "+message.getSubject().toString()); System.out.println("CONTENT: "+message.getContent().toString()); System.out.println("******************************************"); */result = result + "<li>" + "<a href='#' class='clearfix'>" + "<span class='msg-body'>" + "<span class='msg-title'>" + "<span class='blue'>" + message.getFrom()[0].toString() + "</span>" + message.getSubject().toString() + "</span>" + "</span>" + "</a>" + "</li> "; } catch (Exception e) { // TODO Auto-generated catch block System.out.println("No Information"); } } } catch (Exception ex) { } return result; }
From source file:com.intranet.intr.inbox.SupControllerInbox.java
@RequestMapping(value = "/ajaxtestNoL", method = RequestMethod.GET) public @ResponseBody String ajaxtestNoL(Principal principal) { String name = principal.getName(); String result = ""; try {/* w ww . j av a 2 s.c o m*/ users u = usuarioService.getByLogin(name); Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.1and1.es", u.getCorreoUsuario(), u.getCorreoContreasenna()); System.out.println("ola" + store); Folder inbox = store.getFolder("Inbox"); inbox.open(Folder.READ_ONLY); FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false); Calendar fecha3 = Calendar.getInstance(); fecha3.roll(Calendar.MONTH, false); Message msg[] = inbox.search(new ReceivedDateTerm(ComparisonTerm.GT, fecha3.getTime())); //Message msg[] = inbox.search(ft); System.out.println("MAILS: " + msg.length); for (Message message : msg) { try { /*System.out.println("DATE: "+message.getSentDate().toString()); System.out.println("FROM: "+message.getFrom()[0].toString()); System.out.println("SUBJECT: "+message.getSubject().toString()); System.out.println("CONTENT: "+message.getContent().toString()); System.out.println("******************************************"); */result = result + "<li>" + "<a href='#' class='clearfix'>" + "<span class='msg-body'>" + "<span class='msg-title'>" + "<span class='blue'>" + message.getFrom()[0].toString() + "</span>" + message.getSubject().toString() + "</span>" + "</span>" + "</a>" + "</li> "; } catch (Exception e) { // TODO Auto-generated catch block System.out.println("No Information"); } } } catch (Exception ex) { } return result; }
From source file:org.exist.xquery.modules.mail.MessageListFunctions.java
private SearchTerm parseReceivedDateTerm(Node terms) throws XPathException { SearchTerm st = null;/* w w w.j ava2 s . c o m*/ String value = ((Element) terms).getAttribute("date"); String format = ((Element) terms).getAttribute("format"); if (StringUtils.isEmpty(value)) { throw (new XPathException(this, "value not specified for term with type: " + ((Element) terms).getAttribute("type"))); } if (StringUtils.isEmpty(format)) { throw (new XPathException(this, "format not specified for term with type: " + ((Element) terms).getAttribute("type"))); } int cp = parseComparisonAttribute(terms); try { SimpleDateFormat sdf = new SimpleDateFormat(format); Date date = sdf.parse(value); st = new ReceivedDateTerm(cp, date); } catch (ParseException pe) { throw (new XPathException(this, "Cannot parse date value: " + value + ", using format: " + format + ", for term with type: " + ((Element) terms).getAttribute("type"))); } return (st); }
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();/*from ww w .j ava2 s. c om*/ _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.pentaho.di.job.entries.getpop.MailConnection.java
/** * Set filter on message received date.// w w w .j av a 2 s.c o m * * @param receiveddate * messages will be filtered on receiveddate */ public void setReceivedDateTermEQ(Date receiveddate) { if (this.protocol == MailConnectionMeta.PROTOCOL_POP3) { log.logError(BaseMessages.getString(PKG, "MailConnection.Error.ReceivedDatePOP3Unsupported")); } else { addSearchTerm(new ReceivedDateTerm(ComparisonTerm.EQ, receiveddate)); } }