List of usage examples for javax.mail Folder READ_ONLY
int READ_ONLY
To view the source code for javax.mail Folder READ_ONLY.
Click Source Link
From source file:net.prhin.mailman.MailMan.java
public static void main(String[] args) { Properties props = new Properties(); props.setProperty(resourceBundle.getString("mailman.mail.store"), resourceBundle.getString("mailman.protocol")); Session session = Session.getInstance(props); try {/*from w w w . ja v a2s . com*/ Store store = session.getStore(); store.connect(resourceBundle.getString("mailman.host"), resourceBundle.getString("mailman.user"), resourceBundle.getString("mailman.password")); Folder inbox = store.getFolder(resourceBundle.getString("mailman.folder")); inbox.open(Folder.READ_ONLY); inbox.getUnreadMessageCount(); Message[] messages = inbox.getMessages(); for (int i = 0; i <= messages.length / 2; i++) { Message tmpMessage = messages[i]; Multipart multipart = (Multipart) tmpMessage.getContent(); System.out.println("Multipart count: " + multipart.getCount()); for (int j = 0; j < multipart.getCount(); j++) { BodyPart bodyPart = multipart.getBodyPart(j); if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) { if (bodyPart.getContent().getClass().equals(MimeMultipart.class)) { MimeMultipart mimeMultipart = (MimeMultipart) bodyPart.getContent(); for (int k = 0; k < mimeMultipart.getCount(); k++) { if (mimeMultipart.getBodyPart(k).getFileName() != null) { printFileContents(mimeMultipart.getBodyPart(k)); } } } } else { printFileContents(bodyPart); } } } inbox.close(false); store.close(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:search.java
public static void main(String argv[]) { int optind;//from ww w .j av a 2 s .c o 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;// w w w. j a v a2 s . c o m 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:msgshow.java
public static void main(String argv[]) { int optind;/* w w w .j a va 2s . c o m*/ InputStream msgStream = System.in; 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("-v")) { verbose = 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("-p")) { port = Integer.parseInt(argv[++optind]); } else if (argv[optind].equals("-s")) { showStructure = true; } else if (argv[optind].equals("-S")) { saveAttachments = true; } else if (argv[optind].equals("-m")) { showMessage = true; } else if (argv[optind].equals("-a")) { showAlert = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[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] [-f msg-file]"); System.exit(1); } else { break; } } try { // 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; if (mbox != null) msg = new MimeMessage(session, new BufferedInputStream(new FileInputStream(mbox))); else msg = new MimeMessage(session, msgStream); dumpPart(msg); 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); } if (mbox == null) mbox = "INBOX"; 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 (optind >= argv.length) { // 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 { while (optind < argv.length) { int msgnum = Integer.parseInt(argv[optind++]); System.out.println("Getting message number: " + msgnum); Message m = null; try { m = folder.getMessage(msgnum); dumpPart(m); } catch (IndexOutOfBoundsException iex) { System.out.println("Message number out of range"); } } } folder.close(false); store.close(); } catch (Exception ex) { System.out.println("Oops, got exception! " + ex.getMessage()); ex.printStackTrace(); System.exit(1); } System.exit(0); }
From source file:MainClass.java
public static void main(String argv[]) { int msgnum = -1; int optind;/*from ww w . jav a 2 s .c om*/ InputStream msgStream = System.in; 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("-v")) { verbose = 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("-p")) { port = Integer.parseInt(argv[++optind]); } else if (argv[optind].equals("-s")) { showStructure = true; } else if (argv[optind].equals("-S")) { saveAttachments = true; } else if (argv[optind].equals("-m")) { showMessage = true; } else if (argv[optind].equals("-a")) { showAlert = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[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] [-f msg-file]"); System.exit(1); } else { break; } } try { if (optind < argv.length) msgnum = Integer.parseInt(argv[optind]); // 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; if (mbox != null) msg = new MimeMessage(session, new BufferedInputStream(new FileInputStream(mbox))); else msg = new MimeMessage(session, msgStream); dumpPart(msg); 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); } if (mbox == null) mbox = "INBOX"; 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 { System.out.println("Getting message number: " + msgnum); Message m = null; try { m = folder.getMessage(msgnum); dumpPart(m); } catch (IndexOutOfBoundsException iex) { System.out.println("Message number out of range"); } } folder.close(false); store.close(); } catch (Exception ex) { System.out.println("Oops, got exception! " + ex.getMessage()); ex.printStackTrace(); System.exit(1); } System.exit(0); }
From source file:dao.FetchEmailDAO.java
/** * * @return/*w w w . ja va 2 s . c o m*/ */ public Message[] fetchEmail() { Properties props = new Properties(); props.setProperty("mail.store.protocol", "imaps"); //store inbox emails as Message object, store all into Message[] array. Message[] msgArr = null; try { Session session = Session.getInstance(props, null); Store store = session.getStore(); store.connect("imap-mail.outlook.com", "joshymantou@outlook.com", "mcgrady11"); Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); msgArr = inbox.getMessages(); //System.out.println(msgArr.length); /*for (int i = 0; i < msgArr.length; i++) { Message msg = msgArr[i]; Address[] in = msg.getFrom(); for (Address address : in) { System.out.println("FROM:" + address.toString()); } Multipart mp = (Multipart) msg.getContent(); BodyPart bp = mp.getBodyPart(0); /* System.out.println("SENT DATE:" + msg.getSentDate()); System.out.println("SUBJECT:" + msg.getSubject()); System.out.println("CONTENT:" + bp.getContent()); }*/ ArrayUtils.reverse(msgArr); return msgArr; } catch (Exception mex) { mex.printStackTrace(); } return msgArr; }
From source file:org.springframework.ws.transport.mail.monitor.AbstractMonitoringStrategy.java
public int getFolderOpenMode() { return deleteMessages ? Folder.READ_WRITE : Folder.READ_ONLY; }
From source file:org.jevis.emaildatasource.EMailManager.java
/** * Get list of messages/*from w w w .j ava2s . c o m*/ * * @param folder * @param conn * * @return List of Message */ private static List<Message> getMessageList(Folder folder, EMailChannelParameters chanParam) { List<Message> messageList = null; SearchTerm term = chanParam.getSearchTerms(); try { folder.open(Folder.READ_ONLY); } catch (MessagingException ex) { Logger.getLogger(EMailManager.class.getName()).log(Level.SEVERE, "EMail folder is not available to read.", ex); } Message[] msgs = null; Logger.getLogger(EMailManager.class.getName()).log(Level.INFO, "Folder is open: {0}", folder.isOpen()); if (chanParam.getProtocol().equalsIgnoreCase(EMailConstants.Protocol.IMAP)) { try { msgs = folder.search(term); } catch (MessagingException ex) { Logger.getLogger(EMailManager.class.getName()).log(Level.SEVERE, "Unable to search messages", ex); } } else if (chanParam.getProtocol().equalsIgnoreCase(EMailConstants.Protocol.POP3)) { try { // int[] msgnums = new int[1000]; // Message[] messages = folder.getMessages(msgnums); Message[] messages = folder.getMessages(); messages = filterPOP3ByDate(messages, chanParam.getLastReadout()); msgs = folder.search(term, messages); } catch (MessagingException ex) { Logger.getLogger(EMailManager.class.getName()).log(Level.SEVERE, "POP3: failed to receive messages from a folder.", ex); } } else { Logger.getLogger(EMailManager.class.getName()).log(Level.SEVERE, "Unable to search messages"); } messageList = Arrays.asList(msgs); Logger.getLogger(EMailManager.class.getName()).log(Level.INFO, "Messages found: {0}", messageList.size()); return messageList; }
From source file:org.apache.hupa.server.handler.GetRawMessageHandler.java
@Override protected RawMessageResult executeInternal(RawMessage action, ExecutionContext context) throws ActionException { User user = getUser();//from www . j a v a 2 s .c o m long uid = action.getUid(); org.apache.hupa.shared.data.IMAPFolder folder = action.getFolder(); try { IMAPStore store = cache.get(user); IMAPFolder f = (IMAPFolder) store.getFolder(folder.getFullName()); if (f.isOpen() == false) { f.open(Folder.READ_ONLY); } Message m = f.getMessageByUID(action.getUid()); ByteArrayOutputStream out = new ByteArrayOutputStream(); m.writeTo(out); if (f.isOpen()) { f.close(false); } return new RawMessageResult(out.toString()); } catch (Exception e) { logger.error("Unable to get raw content of msg for user " + user + " in folder " + folder + " with uid " + uid, e); throw new ActionException("Unable to et raw content of msg for user " + user + " in folder " + folder + " with uid " + uid); } }
From source file:com.agiletec.plugins.jpwebmail.apsadmin.webmail.folder.WebmailAction.java
public List<Message> getMessages() throws Throwable { List<Message> messageList = null; try {//from w w w .j a v a 2 s . c o m Folder folder = this.getCurrentFolder(); folder.open(Folder.READ_ONLY); CurrentFolderMessagesInfo folderInfos = (CurrentFolderMessagesInfo) this.getRequest().getSession() .getAttribute(CurrentFolderMessagesInfo.CURRENT_FOLDER_MESSAGES); //if (this.hasToReloadMessages(folder, folderInfos)) { Message messages[] = folder.getMessages(); FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.ENVELOPE); folder.fetch(messages, profile); this.setOpenedFolder(folder); messageList = Arrays.asList(messages); this.orderMessages(messageList); folderInfos = new CurrentFolderMessagesInfo(this.getCurrentFolderName(), messageList); this.getRequest().getSession().setAttribute(CurrentFolderMessagesInfo.CURRENT_FOLDER_MESSAGES, folderInfos); //} else { // messageList = folderInfos.getMessages(); //} } catch (Throwable t) { _logger.error("Error extracting messages", t); throw t; } return messageList; }