private static Folder getMailInbox(Store store) { Folder folder = null; Folder inboxfolder = null; try { folder = store.getDefaultFolder(); if (folder == null) throw new Exception("No default folder"); inboxfolder = folder.getFolder("INBOX"); if (inboxfolder == null) throw new Exception("No INBOX"); inboxfolder.open(Folder.READ_ONLY); Message[] msgs = inboxfolder.getMessages(); FetchProfile fp = new FetchProfile(); fp.add("Subject"); inboxfolder.fetch(msgs, fp); return inboxfolder; } catch (NoSuchProviderException ex) { ex.printStackTrace(); } catch (MessagingException ex) { ex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { } return null; }
/** * Fetches and returns message headers as message objects. * * @return * @throws SystemException * @throws ConnectionException */ public ArrayList fetchAllHeadersAsMessages() throws SystemException, ConnectionException { ArrayList headers = null; Folder fold = null; try { fold = getFolder(); closeFolder(fold); fold = getFolder(); headers = new ArrayList(); Message[] msgs = fold.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.FLAGS); fp.add(FetchProfile.Item.CONTENT_INFO); fp.add("Size"); fp.add("Date"); fold.fetch(msgs, fp); Message msg = null; for (int i = 0; i < msgs.length; i++) { msg = msgs[i]; headers.add(msg); } } catch (Exception e) { System.out.println("Could not fetch message headers. Is mbox connection still alive???"); throw new ConnectionException(e); } return headers; }
/** * Fetches all e-mail headers from the server, with appropriate fields already set. * * @param handler * @return ArrayList of MessageHeaders * @throws ConnectionException */ public ArrayList fetchAllHeaders() throws SystemException, ConnectionException { ArrayList headers = null; Folder fold = null; try { fold = getFolder(); closeFolder(fold); fold = getFolder(); headers = new ArrayList(); EmailHeader header = null; Message[] msgs = fold.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.FLAGS); fp.add(FetchProfile.Item.CONTENT_INFO); fp.add("Size"); fp.add("Date"); fold.fetch(msgs, fp); Message msg = null; for (int i = 0; i < msgs.length; i++) { try { header = new EmailHeader(); msg = msgs[i]; header.setMultipart((msg.isMimeType("multipart/*")) ? true : false); header.setMessageId(i + 1); header.setFrom(msg.getFrom()); header.setTo(msg.getRecipients(Message.RecipientType.TO)); header.setCc(msg.getRecipients(Message.RecipientType.CC)); header.setBcc(msg.getRecipients(Message.RecipientType.BCC)); header.setDate(msg.getSentDate()); header.setReplyTo(msg.getReplyTo()); header.setSize(msg.getSize()); header.setSubject(msg.getSubject()); // now set the human readables. header.setDateShown(Formatter.formatDate(header.getDate(), "dd.MM.yyyy HH:mm")); header.setFromShown(Utility.addressArrToString(header.getFrom())); header.setToShown(Utility.addressArrToString(header.getTo())); header.setCcShown(Utility.addressArrToString(header.getCc())); header.setSizeShown(Utility.sizeToHumanReadable(header.getSize())); // it is time to add it to the arraylist headers.add(header); } catch (MessagingException e1) { System.out.println( "Could not parse headers of e-mail. Message might be defuncted or illegal formatted."); } } } catch (Exception e) { System.out.println("Could not fetch message headers. Is mbox connection still alive???"); throw new ConnectionException(e); } return headers; }
// 获得所有邮件的列表 public Message[] getAllMail() throws Exception { // 建立POP3连接 connect(); // 连接邮件服务器 // 取得一个Folder对象 folder = store.getDefaultFolder().getFolder("INBOX"); folder.open(Folder.READ_WRITE); // 取得所有的Message对象 msg = folder.getMessages(); FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.ENVELOPE); folder.fetch(msg, profile); closeConnect(); // 关闭连接邮件服务器 return msg; }
/** * Fetches and returns message headers as message objects. * * @return * @throws SystemException * @throws ConnectionException */ public ArrayList fetchAllHeadersAsMessages() throws SystemException, ConnectionException { ArrayList headers = null; Folder fold = null; try { headers = new ArrayList(); fold = getFolder(); Message[] msgs = fold.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.FLAGS); fp.add(FetchProfile.Item.CONTENT_INFO); fp.add("Size"); fp.add("Date"); fold.fetch(msgs, fp); Message msg = null; for (int i = 0; i < msgs.length; i++) { try { msg = msgs[i]; boolean deleted = false; Flags.Flag flags[] = msg.getFlags().getSystemFlags(); if (flags != null) { Flags.Flag flag = null; for (int m = 0; m < flags.length; m++) { flag = flags[m]; if (flag.equals(Flags.Flag.DELETED)) { deleted = true; } } } if (!deleted) { headers.add(msg); } } catch (Exception e) { log.debug("probably an error fetching list", e); } } } catch (MessagingException e) { log.error("Could not fetch message headers. Is mbox connection still alive???", e); throw new ConnectionException(e); } catch (Exception e) { log.error("Could not fetch message headers. Is mbox connection still alive???", e); throw new ConnectionException(e); } return headers; }
public Message[] getLatestMessages(String theFolder, int num) throws MessagingException { // Open main "INBOX" folder. Folder folder = getFolder(theFolder); folder.open(Folder.READ_WRITE); int totalMessages = folder.getMessageCount(); // Get folder's list of messages. Message[] messages = folder.getMessages(totalMessages - num, totalMessages); // Retrieve message headers for each message in folder. FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.ENVELOPE); profile.add(FetchProfile.Item.CONTENT_INFO); folder.fetch(messages, profile); return messages; }
public void scanWholeFolder() throws MessagingException { if (folder.isOpen() == false) { folder.open(Folder.READ_WRITE); } // Bulk-fetch the message envelopes. Message[] newMessages = folder.getMessages(); FetchProfile fetchProfile = new FetchProfile(); // FIXME: add CONTENT_INFO if we start to display the size // fetchProfile.add(FetchProfile.Item.CONTENT_INFO); fetchProfile.add(FetchProfile.Item.ENVELOPE); fetchProfile.add(FetchProfile.Item.FLAGS); folder.fetch(newMessages, fetchProfile); this.messages = new ArrayList<Message>(); messages.addAll(Arrays.asList(newMessages)); fireTableDataChanged(); }
public void emptyFolder() throws Exception { Folder f = getFolder(); try { Message msgs[] = f.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); f.fetch(msgs, fp); int ids[] = new int[msgs.length]; for (int i = 0; i < msgs.length; i++) { ids[i] = msgs[i].getMessageNumber(); } if (ids.length > 0) { deleteMessages(ids); } } catch (Exception e) { System.out.println("Could not delete all messages"); } }
public static void ClaimsMsgShow( java.lang.String argvs[], Folder folder, Store store, biz.systempartners.claims.ClaimsViewer claimsViewer) { if (claimsViewer == null) {} int msgnum = -1; int optind = 0; InputStream msgStream = System.in; if (argvs != null) { for (optind = 0; optind < argvs.length; optind++) { if (argvs[optind].equals("-T")) { protocol = argvs[++optind]; } else if (argvs[optind].equals("-H")) { host = argvs[++optind]; } else if (argvs[optind].equals("-U")) { user = argvs[++optind]; } else if (argvs[optind].equals("-P")) { password = argvs[++optind]; } else if (argvs[optind].equals("-v")) { verbose = true; } else if (argvs[optind].equals("-D")) { debug = true; } else if (argvs[optind].equals("-f")) { mbox = argvs[++optind]; } else if (argvs[optind].equals("-L")) { url = argvs[++optind]; } else if (argvs[optind].equals("-p")) { port = Integer.parseInt(argvs[++optind]); } else if (argvs[optind].equals("-s")) { showStructure = true; } else if (argvs[optind].equals("-S")) { saveAttachments = true; } else if (argvs[optind].equals("-m")) { showMessage = true; } else if (argvs[optind].equals("-a")) { showAlert = true; } else if (argvs[optind].equals("--")) { optind++; break; } else if (argvs[optind].startsWith("-")) { System.out.println("Usage: msgshow [-L url] [-T protocol] [-H host] [-p port] [-U user]"); System.out.println("\t[-P password] [-f mailbox] [msgnum] [-v] [-D] [-s] [-S] [-a]"); System.out.println("or msgshow -m [-v] [-D] [-s] [-S] < msg"); System.exit(1); } else { break; } } } try { if (optind < argvs.length) msgnum = Integer.parseInt(argvs[optind]); // msgnum = 1; // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); session.setDebug(debug); if (showMessage) { MimeMessage msg = new MimeMessage(session, msgStream); dumpPart(msg, claimsViewer); // System.exit(0); } /* // Get a Store object Store store = null; if (url != null) { URLName urln = new URLName(url); store = session.getStore(urln); if (showAlert) { store.addStoreListener(new StoreListener() { public void notification(StoreEvent e) { String s; if (e.getMessageType() == StoreEvent.ALERT) s = "ALERT: "; else s = "NOTICE: "; System.out.println(s + e.getMessage()); } }); } 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, port, user, password); else store.connect(); } // Open the Folder Folder folder = store.getDefaultFolder(); if (folder == null) { System.out.println("No default folder"); System.exit(1); } */ // folder = folder.getFolder(mbox); if (folder == null) { System.out.println("Invalid folder"); System.exit(1); } // try to open read/write and if that fails try read-only try { folder.open(Folder.READ_WRITE); } catch (MessagingException ex) { folder.open(Folder.READ_ONLY); } int totalMessages = folder.getMessageCount(); if (totalMessages == 0) { System.out.println("Empty folder"); folder.close(false); store.close(); System.exit(1); } if (verbose) { int newMessages = folder.getNewMessageCount(); System.out.println("Total messages = " + totalMessages); System.out.println("New messages = " + newMessages); System.out.println("-------------------------------"); } if (msgnum == -1) { // Attributes & Flags for all messages .. Message[] msgs = folder.getMessages(); // Use a suitable FetchProfile FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.FLAGS); fp.add("X-Mailer"); folder.fetch(msgs, fp); for (int i = 0; i < msgs.length; i++) { System.out.println("--------------------------"); System.out.println("MESSAGE #" + (i + 1) + ":"); dumpEnvelope(msgs[i]); // dumpPart(msgs[i]); } } else { Message[] msgs = folder.getMessages(); for (int n = 0; n < msgs.length; n++) { System.out.println("Getting message number: " + n + 1); Message m = null; try { m = folder.getMessage(msgnum + 1); // m.setDisposition( dumpPart(m, claimsViewer); // m.setExpunged(true); } catch (IndexOutOfBoundsException iex) { System.out.println("Message number out of range"); } } folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true); } // folder.expunge(); folder.close(true); store.close(); } catch (Exception ex) { System.out.println("Oops, got exception! " + ex.getMessage()); ex.printStackTrace(); // System.exit(1); } // System.exit(0); }
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(); }
/** * Fetches all e-mail headers from the server, with appropriate fields already set. * * @param handler * @return ArrayList of MessageHeaders * @throws ConnectionException */ public ArrayList fetchAllHeaders() throws SystemException, ConnectionException { ArrayList headers = new ArrayList(); Folder fold = null; try { fold = getFolder(); EmailHeader header = null; Message[] msgs = fold.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.FLAGS); fp.add(FetchProfile.Item.CONTENT_INFO); fp.add("Size"); fp.add("Date"); fp.add("Disposition-Notification-To"); fp.add("X-Priority"); fp.add("X-MSMail-Priority"); fp.add("Sensitivity"); fold.fetch(msgs, fp); Message msg = null; for (int i = 0; i < msgs.length; i++) { try { header = new EmailHeader(); msg = msgs[i]; header.setMultipart((msg.isMimeType("multipart/*")) ? true : false); header.setMessageId(i + 1); header.setFrom(msg.getFrom()); header.setTo(msg.getRecipients(Message.RecipientType.TO)); header.setCc(msg.getRecipients(Message.RecipientType.CC)); header.setBcc(msg.getRecipients(Message.RecipientType.BCC)); header.setDate(msg.getSentDate()); header.setReplyTo(msg.getReplyTo()); header.setSize(msg.getSize()); header.setSubject(org.claros.commons.utility.Utility.updateTRChars(msg.getSubject())); // now set the human readables. header.setDateShown(Formatter.formatDate(header.getDate(), "dd.MM.yyyy HH:mm")); header.setFromShown( org.claros.commons.utility.Utility.updateTRChars( Utility.addressArrToStringShort(header.getFrom()))); header.setToShown(Utility.addressArrToStringShort(header.getTo())); header.setCcShown(Utility.addressArrToStringShort(header.getCc())); header.setSizeShown(Utility.sizeToHumanReadable(header.getSize())); org.claros.commons.mail.parser.MessageParser.setHeaders(msg, header); boolean deleted = false; if (profile.getProtocol().equals(Constants.IMAP)) { Flags.Flag flags[] = msg.getFlags().getSystemFlags(); if (flags != null) { Flags.Flag flag = null; for (int m = 0; m < flags.length; m++) { flag = flags[m]; if (flag.equals(Flags.Flag.SEEN)) { header.setUnread(new Boolean(false)); } if (flag.equals(Flags.Flag.DELETED)) { deleted = true; } } } } if (header.getUnread() == null) { header.setUnread(new Boolean(true)); } // it is time to add it to the arraylist if (!deleted) { headers.add(header); } } catch (MessagingException e1) { log.error( "Could not parse headers of e-mail. Message might be defuncted or illegal formatted.", e1); } } } catch (MessagingException e) { log.error("Could not fetch message headers. Is mbox connection still alive???", e); // throw new ConnectionException(e); } catch (Exception e) { log.error("Could not fetch message headers. Is mbox connection still alive???", e); // throw new ConnectionException(e); } return headers; }