public MessagesWrapper search(List<SearchTerm> term) throws MessagingException { if (!folder.isOpen()) { folder.open(Folder.READ_ONLY); } Message[] messages = folder.search(and(term)); return new MessagesWrapper(logger, Arrays.asList(messages), folder); }
public void testSearch() throws Exception { Store store = session.getStore("imap"); store.connect("mail.skyteam.prv", "advalso", ""); Folder folder = store.getFolder("Shitty Things"); System.out.println("got folder: " + folder); folder.open(Folder.READ_ONLY); SearchTerm term = new FromStringTerm("steve"); Message message[] = folder.search(term); System.out.println("there are " + message.length + " matching shitty messages."); for (Message msg : message) { System.out.println(((MimeMessage) msg).getContent()); } folder.close(false); store.close(); }
@Test public void testEmptyList() throws Exception { Folder folder = mock(Folder.class); when(folder.search(any(SearchTerm.class))).thenThrow(new MessagingException()); MessageExtractor extractor = new MessageExtractor(folder); List<NamedPeriod> periods = extractor.list(new DateTime(), new DateTime()); assertEquals(0, periods.size()); }
public static Renderable[] getMessages( String emailServer, String user, String password, String provider) { Store store = null; Folder inboxfolder = null; Renderable[] renderedMsgs = null; try { store = getEmailStore(emailServer, user, password, provider); inboxfolder = getMailInbox(store); Calendar date = Calendar.getInstance(); date.add(Calendar.HOUR, -10); SearchTerm newer = new SentDateTerm(ComparisonTerm.GE, date.getTime()); try { Message[] msg = inboxfolder.search(newer, inboxfolder.getMessages()); if (msg != null) { renderedMsgs = new Renderable[msg.length]; } else { return null; } for (int j = msg.length - 1; j >= 0; j--) { renderedMsgs[j] = getMessage(msg[j]); } } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* for(int j=msgs.length-1;j>=0;j--) { }*/ } finally { try { inboxfolder.close(false); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { store.close(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return renderedMsgs; }
private Object receiveMail() throws Exception { Folder folder = null; Store store = null; ConfigurationPropertyManager configManager = ConfigurationPropertyManager.getConfigurationPropertyManager(); final String email_address = configManager.getPropValues("gmail_address"); final String password = configManager.getPropValues("gmail_pw"); try { Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); // Session session = Session.getDefaultInstance(props, null); Session session = Session.getInstance(props, new GMailAuthenticator(email_address, password)); store = session.getStore("imaps"); store.connect("imap.gmail.com", email_address, password); folder = store.getFolder("Inbox"); folder.open(Folder.READ_WRITE); // Message messages[] = folder.getMessages(); // search for all "unseen" messages Flags seen = new Flags(Flags.Flag.SEEN); FlagTerm unseenFlagTerm = new FlagTerm(seen, false); Message messages[] = folder.search(unseenFlagTerm); for (int i = 0; i < messages.length; i++) { folder.getMessage( messages[i].getMessageNumber()); // this will mark the retrieved messages as READ } String s = updateEmailrepository(messages); // System.out.println(s); StringTerm st = new StringTermImpl(s); return st; } catch (Exception e) { System.out.println(e.toString()); } finally { if (folder != null) { folder.close(true); } } return null; }
/** * Returns a list of indexes that satisfy the search parameters. The list is ordered by ascending * order, with the biggest index being the most recent. If a field is set to null, it is ignored. * * @throws ParseException if the address format in <from> is not respected */ public List<Message> search(String from, Date since, String subject) throws IOException, ParseException { if (!inboxSelected) { this.selectInbox(); } List<SearchTerm> searchTerms = new ArrayList<SearchTerm>(); if (from != null) { try { Address address = new InternetAddress(from); AddressTerm addrTerm = new FromTerm(address); searchTerms.add(addrTerm); } catch (AddressException e) { throw new IllegalArgumentException("Address format is not correct."); } } if (since != null) { DateTerm dateTerm = new ReceivedDateTerm(ComparisonTerm.GE, since); searchTerms.add(dateTerm); } if (subject != null) { SubjectTerm subjTerm = new SubjectTerm(subject); searchTerms.add(subjTerm); } SearchTerm[] st = new SearchTerm[searchTerms.size()]; for (int i = 0; i < st.length; i++) { st[i] = searchTerms.get(i); } SearchTerm search = new AndTerm(st); List<Message> list = new ArrayList<Message>(); try { Message[] msgs = inbox.search(search); for (int i = 0; i < msgs.length; i++) { list.add(msgs[i]); } } catch (MessagingException e) { throw new IOException(e.getMessage()); } return list; }
@Test public void testValidList() throws Exception { Message message1 = mockMessage(DATE_1, SUBJECT_1); Message message2 = mockMessage(DATE_2, SUBJECT_2); Folder folder = mock(Folder.class); when(folder.search(any(SearchTerm.class))).thenReturn(new Message[] {message1, message2}); MessageExtractor extractor = new MessageExtractor(folder); List<NamedPeriod> periods = extractor.list(new DateTime(), new DateTime()); assertEquals(2, periods.size()); assertEquals(DATE_1, periods.get(0).getFrom().toDate()); assertEquals(DATE_1, periods.get(0).getTo().toDate()); assertEquals(SUBJECT_1, periods.get(0).getName()); assertEquals(DATE_2, periods.get(1).getFrom().toDate()); assertEquals(DATE_2, periods.get(1).getTo().toDate()); assertEquals(SUBJECT_2, periods.get(1).getName()); }
public MessageIterator(Folder folder, int batchSize) throws EmailFetchException { try { this.folder = folder; this.batchSize = batchSize; SearchTerm st = getSearchTerm(); if (st != null) { doBatching = false; messagesInCurBatch = folder.search(st); totalInFolder = messagesInCurBatch.length; folder.fetch(messagesInCurBatch, fp); current = 0; LOG.info("Total messages: {}", totalInFolder); LOG.info("Search criteria applied. Batching disabled."); } else { totalInFolder = folder.getMessageCount(); LOG.info("Total messages: {}", totalInFolder); getNextBatch(batchSize, folder); } } catch (MessagingException e) { throw new EmailFetchException("Message retreival failed", e); } }
public void delete(String msgid) throws Exception { Store store = this.session.getStore(); store.connect(); try { Folder folder = store.getFolder(folderName); folder.open(Folder.READ_WRITE); SearchTerm term = new MessageIDTerm(msgid); Message[] result = folder.search(term); log.info("ho trovato " + result.length + " messaggi con msgid " + msgid); for (Message m : result) { m.setFlag(Flag.SEEN, false); } folder.close(true); } finally { store.close(); } }
public static String readQuikFlixResetLink() { properties = new Properties(); properties.setProperty("mail.host", "imap.gmail.com"); properties.setProperty("mail.port", "995"); properties.setProperty("mail.transport.protocol", "imaps"); session = Session.getInstance( properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }); try { Thread.sleep(8000L); store = session.getStore("imaps"); store.connect(); inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_WRITE); Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false)); System.out.println("Number of mails = " + messages.length); for (int i = 0; i < messages.length; i++) { Message message = messages[i]; if (message.getSubject().equalsIgnoreCase("Quickflix Password Reset")) { Object content; try { content = message.getContent(); if (content instanceof String) { message.setFlag(Flags.Flag.DELETED, true); resetURL = Jsoup.parse((String) content).select("a").first().text(); return resetURL; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } inbox.close(true); store.close(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return "No Unread Mails from QuickFlix"; }
public static void main(String argv[]) { int optind; 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.getDefaultInstance(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); }
@Override protected Iterator<MultiValueHashMap<String, Object>> getSubDataEntitiesInformation( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws Exception { // imap url schema: imap[s]://uname@hostname:port/folder;uidvalidity=385759045/;uid=20. Examples // (incl. message-referenzierung) // http://xml.resource.org/public/rfc/html/rfc2192.html#anchor10 // allerdings nimmt der Java ImapStore auch URLs mit Passwörtern an. Dann geht auch // imap[s]://uname:pwd@hostname:port/folder;uidvalidity=385759045/;uid=20 CrawlerContext crawlerContext = context.get(CrawlerContext.class, new CrawlerContext()); String strContainerURL = metadata.get(Metadata.SOURCE); URLName containerURLName = new URLName(strContainerURL); if (m_mailStore == null) m_mailStore = connect2Server(containerURLName, context); // wenn kein directory angegeben wird, dann crawlen wir einfach den default folder und die inbox LinkedList<Folder> llFolderz2Crawl = new LinkedList<Folder>(); if (containerURLName.getFile() != null) { Folder folder = m_mailStore.getFolder(containerURLName.getFile()); if (folder != null && folder.exists()) llFolderz2Crawl.add(folder); else throw new FileNotFoundException("Can't find imap folder '" + folder.getFullName() + "'"); } else { Folder folder = m_mailStore.getDefaultFolder(); if (folder != null && folder.exists()) llFolderz2Crawl.add(folder); folder = m_mailStore.getFolder("INBOX"); if (folder != null && folder.exists()) llFolderz2Crawl.add(folder); } LinkedList<MultiValueHashMap<String, Object>> llEntityInfo = new LinkedList<MultiValueHashMap<String, Object>>(); for (Folder folder2crawl : llFolderz2Crawl) { // Jetzt haben wir die Containerobjekte - nun geben wir die Daten zu den SubEntities zurück // die subfolder boolean bFolderCanHaveSubFolders = (folder2crawl.getType() & Folder.HOLDS_FOLDERS) == Folder.HOLDS_FOLDERS; if (bFolderCanHaveSubFolders) { folder2crawl.open(Folder.READ_ONLY); Folder[] subFolders = folder2crawl.list(); for (Folder subFolder : subFolders) { URLName urlName = subFolder.getURLName(); URLName urlNameWithPassword = new URLName( containerURLName.getProtocol(), urlName.getHost(), urlName.getPort(), urlName.getFile(), urlName.getUsername(), containerURLName.getPassword()); if (!checkIfInConstraints(urlName.toString(), null, context)) continue; MultiValueHashMap<String, Object> hsEntityInformation = new MultiValueHashMap<String, Object>(); hsEntityInformation.add(CrawlerParser.SOURCEID, urlName); hsEntityInformation.add("urlNameWithPassword", urlNameWithPassword); hsEntityInformation.add("folder", subFolder.getFullName()); llEntityInfo.add(hsEntityInformation); } } // die messages boolean bFolderCanHaveMessages = (folder2crawl.getType() & Folder.HOLDS_MESSAGES) == Folder.HOLDS_MESSAGES; if (bFolderCanHaveMessages) { if (!folder2crawl.isOpen()) folder2crawl.open(Folder.READ_ONLY); // wir holen uns alle nicht-deleted messages, und werfen noch die raus, die 'expunged' sind Message[] relevantMessagesOfFolder = folder2crawl.search(new FlagTerm(new Flags(Flags.Flag.DELETED), false)); ArrayList<Message> nonDelNonExpungedMessages = new ArrayList<Message>(); for (Message message : relevantMessagesOfFolder) if (!message.isExpunged()) nonDelNonExpungedMessages.add(message); relevantMessagesOfFolder = nonDelNonExpungedMessages.toArray(new Message[0]); // die Daten die wir später benötigen holen wir uns effizient in einem Rutsch - deswegen // benötigen wir auch keinen Thread mit dem // OneAfterOneIterator, um Speicher zu sparen (siehe DirectoryCrawlerParser). Das Array // haben wir hier eh. Entweder oder. FetchProfile profile = new FetchProfile(); profile.add(UIDFolder.FetchProfileItem.UID); profile.add("Message-ID"); folder2crawl.fetch(relevantMessagesOfFolder, profile); for (int i = 0; i < relevantMessagesOfFolder.length && !crawlerContext.stopRequested(); i++) { MimeMessage message = (MimeMessage) relevantMessagesOfFolder[i]; // hier brauchen wir noch eine URL mit und eine ohne Passwort URLName urlName = getMessageUrl(folder2crawl, message); URLName urlNameWithPassword = new URLName( containerURLName.getProtocol(), urlName.getHost(), urlName.getPort(), urlName.getFile(), urlName.getUsername(), containerURLName.getPassword()); if (!checkIfInConstraints(urlName.toString(), message, context)) continue; MultiValueHashMap<String, Object> hsEntityInformation = new MultiValueHashMap<String, Object>(); hsEntityInformation.add(CrawlerParser.SOURCEID, urlName); hsEntityInformation.add("urlNameWithPassword", urlNameWithPassword); hsEntityInformation.add("Message-ID", message.getHeader("Message-ID")[0]); hsEntityInformation.add("folder", folder2crawl.getFullName()); llEntityInfo.add(hsEntityInformation); } } // wir haben die folder abgearbeitet, dann können wir diesen Speicher wieder frei geben m_hsImapFolder2Stickyness.clear(); if (folder2crawl.isOpen()) folder2crawl.close(false); } return llEntityInfo.iterator(); }
public ClienteEmailCentralServicoDTO readMessage( DocumentHTML document, HttpServletRequest request, HttpServletResponse response, String messageId) throws Exception { ClienteEmailCentralServicoDTO clienteEmailCentralServicoDto = new ClienteEmailCentralServicoDTO(); clienteEmailCentralServicoDto.setResultSucess(true); try { if (CONEXAO_EMAIL_SERVIDOR.equals("") || CONEXAO_EMAIL_PROVIDER.equals("") || CONEXAO_EMAIL_CAIXA.equals("") || CONEXAO_EMAIL_SENHA.equals("") || CONEXAO_EMAIL_PASTA.equals("")) { clienteEmailCentralServicoDto.setResultSucess(false); clienteEmailCentralServicoDto.setResultMessage( UtilI18N.internacionaliza( request, "clienteEmailCentralServico.problemasRealizarleituraEmailsParametros")); } else { Properties props = new Properties(); props.setProperty("mail.store.protocol", CONEXAO_EMAIL_PROVIDER); props.setProperty("mail.imaps.auth.plain.disable", "true"); props.setProperty("mail.imaps.ssl.trust", "*"); // props.setProperty("mail.debug", "true"); if (!CONEXAO_EMAIL_PORTA.equals("")) props.setProperty("mail." + CONEXAO_EMAIL_PROVIDER + ".port", CONEXAO_EMAIL_PORTA); Session session = Session.getInstance(props, null); Store store = session.getStore(); store.connect(CONEXAO_EMAIL_SERVIDOR, CONEXAO_EMAIL_CAIXA, CONEXAO_EMAIL_SENHA); Folder inbox = store.getFolder(CONEXAO_EMAIL_PASTA); inbox.open(Folder.READ_WRITE); SearchTerm searchTerm = new MessageIDTerm(messageId); Message[] messages = inbox.search(searchTerm); if (messages != null && messages.length > 0) { ArrayList<ClienteEmailCentralServicoMessagesDTO> emailMessages = new ArrayList<ClienteEmailCentralServicoMessagesDTO>(); for (Message message : messages) { ClienteEmailCentralServicoMessagesDTO clienteEmailMessagesDto = new ClienteEmailCentralServicoMessagesDTO(); MimeMessage m = (MimeMessage) inbox.getMessage(message.getMessageNumber()); clienteEmailMessagesDto.setMessageId(m.getMessageID()); clienteEmailMessagesDto.setMessageNumber(message.getMessageNumber()); Address[] in = message.getFrom(); clienteEmailMessagesDto.setMessageEmail( (in == null ? null : ((InternetAddress) in[0]).getAddress())); clienteEmailMessagesDto.setMessageSubject(message.getSubject()); clienteEmailMessagesDto.setMessageReceivedDate(message.getReceivedDate()); clienteEmailMessagesDto.setSeen(message.isSet(Flags.Flag.SEEN)); Object objRef = message.getContent(); String content = ""; if (!(objRef instanceof Multipart)) { content = (String) message.getContent(); } else { Multipart mp = (Multipart) message.getContent(); BodyPart bp = mp.getBodyPart(0); content = getContent(bp); } if (content != null) { // content = content.replaceAll("(\r\n|\r|\n)", "<br />"); content = StringEscapeUtils.escapeEcmaScript(content); clienteEmailMessagesDto.setMessageContent(content); } emailMessages.add(clienteEmailMessagesDto); } clienteEmailCentralServicoDto.setEmailMessages(emailMessages); } } } catch (Exception e) { e.printStackTrace(); clienteEmailCentralServicoDto.setResultSucess(false); clienteEmailCentralServicoDto.setResultMessage( UtilI18N.internacionaliza( request, "clienteEmailCentralServico.problemasRealizarleituraEmails")); } return clienteEmailCentralServicoDto; }
public void run() { while (!stop) { String line; ArrayList getMail = new ArrayList(); BufferedReader reader; Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); try { reader = new BufferedReader(new FileReader("D:\\HomeControlPanelMailInfo.txt")); while ((line = reader.readLine()) != null) getMail.add(line); reader.close(); String user = (String) getMail.get(1); String password = (String) getMail.get(2); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.gmail.com", user, password); System.out.println(store); Folder inbox = store.getFolder("Inbox"); inbox.open(Folder.READ_WRITE); FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false); Message[] messages = inbox.search(ft); for (int i = 0; i < messages.length; i++) { System.out.println("------------ Message " + (i + 1) + " ------------"); String subject = messages[i].getSubject(); if (subject.toLowerCase().equals("on")) { Communicator.writeData("o"); GUI.txtLog.append("Email was retrieved successfully!! \n"); } if (subject.toLowerCase().equals("off")) { Communicator.writeData("c"); GUI.txtLog.append("Email was retrieved successfully!! \n"); } else if (subject != null) { System.out.println("subject:" + subject); } Date sent = messages[i].getSentDate(); if (sent != null) { System.out.println("Sent: " + sent); } /* * Body and date of message if needed * * System.out.println(); System.out.println("Message : "); if * (messages[i].getContentType().toLowerCase() * .contains("text/plain")) { * System.out.println(messages[i].getContent().toString()); } * else { Multipart multipart = (Multipart) * messages[i].getContent(); * * for (int x = 0; x < multipart.getCount(); x++) { BodyPart * bodyPart = multipart.getBodyPart(x); * * String disposition = bodyPart.getDisposition(); * * if (disposition != null && * (disposition.equals(BodyPart.ATTACHMENT))) { * System.out.println("Mail have some attachment : "); * * DataHandler handler = bodyPart.getDataHandler(); * System.out.println("file name : " + handler.getName()); } * else { System.out.println(bodyPart.getContent()); } } * System.out.println(); } */ messages[i].setFlag(Flags.Flag.DELETED, true); } inbox.close(true); store.close(); Thread.sleep(5000); // Thread thread = new getMail(); // thread.start(); } catch (NoSuchProviderException e) { e.printStackTrace(); GUI.system_log.setForeground(Color.red); GUI.system_log.append("There was a problem with the provider!! \n"); } catch (MessagingException e) { e.printStackTrace(); GUI.system_log.setForeground(Color.red); GUI.system_log.append("There was a problem with authentication!! \n"); } catch (IOException e) { e.printStackTrace(); GUI.system_log.setForeground(Color.red); GUI.system_log.append("There was a problem with the file info!! \n"); } catch (InterruptedException e) { GUI.system_log.setForeground(Color.red); GUI.system_log.append("The process was interrupted!! \n"); e.printStackTrace(); } } }