List of usage examples for javax.mail Folder setFlags
public synchronized void setFlags(int[] msgnums, Flags flag, boolean value) throws MessagingException
From source file:mover.java
public static void main(String argv[]) { int start = 1; int end = -1; int optind;//from w ww . jav a 2 s.co m for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { // protocol protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { // host host = argv[++optind]; } else if (argv[optind].equals("-U")) { // user user = argv[++optind]; } else if (argv[optind].equals("-P")) { // password password = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-s")) { // Source mbox src = argv[++optind]; } else if (argv[optind].equals("-d")) { // Destination mbox dest = argv[++optind]; } else if (argv[optind].equals("-x")) { // Expunge ? expunge = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]"); System.out.println("\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]"); System.out.println("\t The -x option => EXPUNGE deleted messages"); System.out.println("\t msgnum1 => start of message-range; msgnum2 => end of message-range"); System.exit(1); } else { break; } } if (optind < argv.length) start = Integer.parseInt(argv[optind++]); // start msg if (optind < argv.length) end = Integer.parseInt(argv[optind++]); // end msg try { // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); // 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 source Folder Folder folder = store.getFolder(src); if (folder == null || !folder.exists()) { System.out.println("Invalid folder: " + src); System.exit(1); } folder.open(Folder.READ_WRITE); int count = folder.getMessageCount(); if (count == 0) { // No messages in the source folder System.out.println(folder.getName() + " is empty"); // Close folder, store and return folder.close(false); store.close(); return; } // Open destination folder, create if reqd Folder dfolder = store.getFolder(dest); if (!dfolder.exists()) dfolder.create(Folder.HOLDS_MESSAGES); if (end == -1) end = count; // Get the message objects to copy Message[] msgs = folder.getMessages(start, end); System.out.println("Moving " + msgs.length + " messages"); if (msgs.length != 0) { folder.copyMessages(msgs, dfolder); folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true); // Dump out the Flags of the moved messages, to insure that // all got deleted for (int i = 0; i < msgs.length; i++) { if (!msgs[i].isSet(Flags.Flag.DELETED)) System.out.println("Message # " + msgs[i] + " not deleted"); } } // Close folders and store folder.close(expunge); store.close(); } catch (MessagingException mex) { Exception ex = mex; do { System.out.println(ex.getMessage()); if (ex instanceof MessagingException) ex = ((MessagingException) ex).getNextException(); else ex = null; } while (ex != null); } }
From source file:MainClass.java
public static void main(String argv[]) { int start = 1; int end = -1; int optind;//www. j a v a 2 s. c o m for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { // protocol protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { // host host = argv[++optind]; } else if (argv[optind].equals("-U")) { // user user = argv[++optind]; } else if (argv[optind].equals("-P")) { // password password = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-s")) { // Source mbox src = argv[++optind]; } else if (argv[optind].equals("-d")) { // Destination mbox dest = argv[++optind]; } else if (argv[optind].equals("-x")) { // Expunge ? expunge = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]"); System.out.println("\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]"); System.out.println("\t The -x option => EXPUNGE deleted messages"); System.out.println("\t msgnum1 => start of message-range; msgnum2 => end of message-range"); System.exit(1); } else { break; } } if (optind < argv.length) start = Integer.parseInt(argv[optind++]); // start msg if (optind < argv.length) end = Integer.parseInt(argv[optind++]); // end msg try { // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); // 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 source Folder Folder folder = store.getFolder(src); if (folder == null || !folder.exists()) { System.out.println("Invalid folder: " + folder.getName()); System.exit(1); } folder.open(Folder.READ_WRITE); int count = folder.getMessageCount(); if (count == 0) { // No messages in the source folder System.out.println(folder.getName() + " is empty"); // Close folder, store and return folder.close(false); store.close(); return; } // Open destination folder, create if reqd Folder dfolder = store.getFolder(dest); if (!dfolder.exists()) dfolder.create(Folder.HOLDS_MESSAGES); if (end == -1) end = count; // Get the message objects to copy Message[] msgs = folder.getMessages(start, end); System.out.println("Moving " + msgs.length + " messages"); if (msgs.length != 0) { folder.copyMessages(msgs, dfolder); folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true); // Dump out the Flags of the moved messages, to insure that // all got deleted for (int i = 0; i < msgs.length; i++) { if (!msgs[i].isSet(Flags.Flag.DELETED)) System.out.println("Message # " + msgs[i] + " not deleted"); } } // Close folders and store folder.close(expunge); store.close(); } catch (MessagingException mex) { Exception ex = mex; do { System.out.println(ex.getMessage()); if (ex instanceof MessagingException) ex = ((MessagingException) ex).getNextException(); else ex = null; } while (ex != null); } }
From source file:org.wso2.carbon.registry.es.utils.EmailUtil.java
/** * This method delete all the sent mails. Can be used after a particular test class * * @throws MessagingException//from www . ja v a 2s .com * @throws IOException */ public static void deleteSentMails() throws MessagingException, IOException { Properties props = new Properties(); props.load(new FileInputStream(new File(TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator + "smtp.properties"))); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString()); Folder sentMail = store.getFolder("[Gmail]/Sent Mail"); sentMail.open(Folder.READ_WRITE); Message[] messages = sentMail.getMessages(); Flags deleted = new Flags(Flags.Flag.DELETED); sentMail.setFlags(messages, deleted, true); sentMail.close(true); store.close(); }
From source file:org.jasig.portlet.emailpreview.dao.javamail.JavamailAccountDaoImpl.java
@Override public boolean deleteMessages(MailStoreConfiguration config, String[] uuids) { Authenticator auth = credentialsProvider.getAuthenticator(); Folder inbox = null; try {//from w w w . j a v a2 s. c o m // Retrieve user's inbox Session session = openMailSession(config, auth); inbox = getUserInbox(session, config.getInboxFolderName()); // Verify that we can even perform this operation if (!(inbox instanceof UIDFolder)) { String msg = "Delete feature is supported only for UIDFolder instances"; throw new UnsupportedOperationException(msg); } inbox.open(Folder.READ_WRITE); Message[] msgs = ((UIDFolder) inbox).getMessagesByUID(getMessageUidsAsLong(uuids)); inbox.setFlags(msgs, new Flags(Flag.DELETED), true); return true; // Indicate success } catch (MessagingException e) { log.error("Messaging exception while deleting messages", e); } finally { if (inbox != null) { try { inbox.close(false); } catch (Exception e) { log.warn("Can't close correctly javamail inbox connection"); } try { inbox.getStore().close(); } catch (Exception e) { log.warn("Can't close correctly javamail store connection"); } } } return false; // We failed if we reached this point }
From source file:org.jasig.portlet.emailpreview.dao.javamail.JavamailAccountDaoImpl.java
@Override public boolean setMessageReadStatus(MailStoreConfiguration config, String[] uuids, boolean read) { Authenticator auth = credentialsProvider.getAuthenticator(); Folder inbox = null; try {/*from w w w . jav a 2 s .c om*/ // Retrieve user's inbox Session session = openMailSession(config, auth); inbox = getUserInbox(session, config.getInboxFolderName()); // Verify that we can even perform this operation log info if it isn't capable of operation if (!(inbox instanceof UIDFolder)) { String msg = "Toggle unread feature is supported only for UIDFolder instances"; log.info(msg); return false; } inbox.open(Folder.READ_WRITE); Message[] msgs = ((UIDFolder) inbox).getMessagesByUID(getMessageUidsAsLong(uuids)); inbox.setFlags(msgs, new Flags(Flag.SEEN), read); return true; // Indicate success } catch (MessagingException e) { log.error("Messaging exception while deleting messages", e); } finally { if (inbox != null) { try { inbox.close(false); } catch (Exception e) { log.warn("Can't close correctly javamail inbox connection"); } try { inbox.getStore().close(); } catch (Exception e) { log.warn("Can't close correctly javamail store connection"); } } } return false; // We failed if we reached this point }
From source file:net.wastl.webmail.server.WebMailSession.java
/** * Copy or move the selected messages from folder fromfolder to folder tofolder. */// w w w .jav a2s . c o m public void copyMoveMessage(String fromfolder, String tofolder, HTTPRequestHeader head, boolean move) throws MessagingException { Folder from = getFolder(fromfolder); Folder to = getFolder(tofolder); if (user.wantsSetFlags()) { if (from.isOpen() && from.getMode() == Folder.READ_ONLY) { from.close(false); from.open(Folder.READ_WRITE); } else if (!from.isOpen()) { from.open(Folder.READ_WRITE); } if (to.isOpen() && to.getMode() == Folder.READ_ONLY) { to.close(false); to.open(Folder.READ_WRITE); } else if (!to.isOpen()) { to.open(Folder.READ_WRITE); } } else { if (!from.isOpen()) { from.open(Folder.READ_ONLY); } if (to.isOpen() && to.getMode() == Folder.READ_ONLY) { to.close(false); to.open(Folder.READ_WRITE); } else if (!to.isOpen()) { to.open(Folder.READ_WRITE); } } int m[] = getSelectedMessages(head, from.getMessageCount()); Message msgs[] = from.getMessages(m); from.copyMessages(msgs, to); if (move && user.wantsSetFlags()) { from.setFlags(m, new Flags(Flags.Flag.DELETED), true); if (user.getBoolVar("autoexpunge")) { from.close(true); to.close(true); } else { if (need_expunge_folders == null) { need_expunge_folders = new Vector<String>(); } need_expunge_folders.addElement(fromfolder); from.close(false); to.close(false); } } else { from.close(false); if (user.getBoolVar("autoexpunge")) { to.close(true); } else { to.close(false); } } from.open(Folder.READ_WRITE); to.open(Folder.READ_WRITE); refreshFolderInformation(fromfolder); refreshFolderInformation(tofolder); }
From source file:net.wastl.webmail.server.WebMailSession.java
/** Change the Flags of the messages the user selected. /* w w w . j a va 2 s . c o m*/ */ public void setFlags(String folderhash, HTTPRequestHeader head) throws MessagingException { if (head.isContentSet("copymovemsgs") && head.getContent("COPYMOVE").equals("COPY")) { copyMoveMessage(folderhash, head.getContent("TO"), head, false); } else if (head.isContentSet("copymovemsgs") && head.getContent("COPYMOVE").equals("MOVE")) { copyMoveMessage(folderhash, head.getContent("TO"), head, true); } else if (head.isContentSet("flagmsgs")) { log.debug("setting message flags"); Folder folder = getFolder(folderhash); //log.debug("Processing Request Header..."); /* Get selected messages */ int msgs[] = getSelectedMessages(head, folder.getMessageCount()); //log.debug("get flags..."); /* Get selected flags */ Flags fl = new Flags(Flags.Flag.USER); if (head.getContent("MESSAGE FLAG").equals("DELETED")) { fl = new Flags(Flags.Flag.DELETED); if (need_expunge_folders == null) { need_expunge_folders = new Vector<String>(); } need_expunge_folders.addElement(folderhash); } else if (head.getContent("MESSAGE FLAG").equals("SEEN")) { fl = new Flags(Flags.Flag.SEEN); } else if (head.getContent("MESSAGE FLAG").equals("RECENT")) { fl = new Flags(Flags.Flag.RECENT); } else if (head.getContent("MESSAGE FLAG").equals("ANSWERED")) { fl = new Flags(Flags.Flag.ANSWERED); } else if (head.getContent("MESSAGE FLAG").equals("DRAFT")) { fl = new Flags(Flags.Flag.DRAFT); } boolean value = true; if (head.getContent("MARK").equals("UNMARK")) { value = false; } //log.debug("Done!"); //log.debug("Setting flags..."); if (user.wantsSetFlags()) { if (folder.isOpen() && folder.getMode() == Folder.READ_ONLY) { folder.close(false); folder.open(Folder.READ_WRITE); } else if (!folder.isOpen()) { folder.open(Folder.READ_WRITE); } folder.setFlags(msgs, fl, value); if (user.getBoolVar("autoexpunge")) { folder.close(true); if (need_expunge_folders != null) { need_expunge_folders.removeElement(folderhash); } } else { folder.close(false); } } folder.open(Folder.READ_WRITE); refreshFolderInformation(folderhash); } }
From source file:org.nuxeo.ecm.platform.mail.utils.MailCoreHelper.java
protected static void doCheckMail(DocumentModel currentMailFolder, CoreSession coreSession) throws MessagingException { String email = (String) currentMailFolder.getPropertyValue(EMAIL_PROPERTY_NAME); String password = (String) currentMailFolder.getPropertyValue(PASSWORD_PROPERTY_NAME); if (!StringUtils.isEmpty(email) && !StringUtils.isEmpty(password)) { mailService = getMailService();/*from w w w .j ava 2s. c om*/ MessageActionPipe pipe = mailService.getPipe(PIPE_NAME); Visitor visitor = new Visitor(pipe); Thread.currentThread().setContextClassLoader(Framework.class.getClassLoader()); // initialize context ExecutionContext initialExecutionContext = new ExecutionContext(); initialExecutionContext.put(MIMETYPE_SERVICE_KEY, getMimeService()); initialExecutionContext.put(PARENT_PATH_KEY, currentMailFolder.getPathAsString()); initialExecutionContext.put(CORE_SESSION_KEY, coreSession); initialExecutionContext.put(LEAVE_ON_SERVER_KEY, Boolean.TRUE); // TODO should be an attribute in 'protocol' // schema Folder rootFolder = null; Store store = null; try { String protocolType = (String) currentMailFolder.getPropertyValue(PROTOCOL_TYPE_PROPERTY_NAME); initialExecutionContext.put(PROTOCOL_TYPE_KEY, protocolType); // log.debug(PROTOCOL_TYPE_KEY + ": " + (String) initialExecutionContext.get(PROTOCOL_TYPE_KEY)); String host = (String) currentMailFolder.getPropertyValue(HOST_PROPERTY_NAME); String port = (String) currentMailFolder.getPropertyValue(PORT_PROPERTY_NAME); Boolean socketFactoryFallback = (Boolean) currentMailFolder .getPropertyValue(SOCKET_FACTORY_FALLBACK_PROPERTY_NAME); String socketFactoryPort = (String) currentMailFolder .getPropertyValue(SOCKET_FACTORY_PORT_PROPERTY_NAME); Boolean starttlsEnable = (Boolean) currentMailFolder .getPropertyValue(STARTTLS_ENABLE_PROPERTY_NAME); String sslProtocols = (String) currentMailFolder.getPropertyValue(SSL_PROTOCOLS_PROPERTY_NAME); Long emailsLimit = (Long) currentMailFolder.getPropertyValue(EMAILS_LIMIT_PROPERTY_NAME); long emailsLimitLongValue = emailsLimit == null ? EMAILS_LIMIT_DEFAULT : emailsLimit.longValue(); String imapDebug = Framework.getProperty(IMAP_DEBUG, "false"); Properties properties = new Properties(); properties.put("mail.store.protocol", protocolType); // properties.put("mail.host", host); // Is IMAP connection if (IMAP.equals(protocolType)) { properties.put("mail.imap.host", host); properties.put("mail.imap.port", port); properties.put("mail.imap.starttls.enable", starttlsEnable.toString()); properties.put("mail.imap.debug", imapDebug); properties.put("mail.imap.partialfetch", "false"); } else if (IMAPS.equals(protocolType)) { properties.put("mail.imaps.host", host); properties.put("mail.imaps.port", port); properties.put("mail.imaps.starttls.enable", starttlsEnable.toString()); properties.put("mail.imaps.ssl.protocols", sslProtocols); properties.put("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.put("mail.imaps.socketFactory.fallback", socketFactoryFallback.toString()); properties.put("mail.imaps.socketFactory.port", socketFactoryPort); properties.put("mail.imap.partialfetch", "false"); properties.put("mail.imaps.partialfetch", "false"); } else if (POP3S.equals(protocolType)) { properties.put("mail.pop3s.host", host); properties.put("mail.pop3s.port", port); properties.put("mail.pop3s.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.put("mail.pop3s.socketFactory.fallback", socketFactoryFallback.toString()); properties.put("mail.pop3s.socketFactory.port", socketFactoryPort); properties.put("mail.pop3s.ssl.protocols", sslProtocols); } else { // Is POP3 connection properties.put("mail.pop3.host", host); properties.put("mail.pop3.port", port); } properties.put("user", email); properties.put("password", password); Session session = Session.getInstance(properties); store = session.getStore(); store.connect(email, password); String folderName = INBOX; // TODO should be an attribute in 'protocol' schema rootFolder = store.getFolder(folderName); // need RW access to update message flags rootFolder.open(Folder.READ_WRITE); Message[] allMessages = rootFolder.getMessages(); // VDU log.debug("nbr of messages in folder:" + allMessages.length); FetchProfile fetchProfile = new FetchProfile(); fetchProfile.add(FetchProfile.Item.FLAGS); fetchProfile.add(FetchProfile.Item.ENVELOPE); fetchProfile.add(FetchProfile.Item.CONTENT_INFO); fetchProfile.add("Message-ID"); fetchProfile.add("Content-Transfer-Encoding"); rootFolder.fetch(allMessages, fetchProfile); if (rootFolder instanceof IMAPFolder) { // ((IMAPFolder)rootFolder).doCommand(FetchProfile) } List<Message> unreadMessagesList = new ArrayList<Message>(); for (Message message : allMessages) { Flags flags = message.getFlags(); int unreadMessagesListSize = unreadMessagesList.size(); if (flags != null && !flags.contains(Flag.SEEN) && unreadMessagesListSize < emailsLimitLongValue) { unreadMessagesList.add(message); if (unreadMessagesListSize == emailsLimitLongValue - 1) { break; } } } Message[] unreadMessagesArray = unreadMessagesList.toArray(new Message[unreadMessagesList.size()]); // perform email import visitor.visit(unreadMessagesArray, initialExecutionContext); // perform flag update globally Flags flags = new Flags(); flags.add(Flag.SEEN); boolean leaveOnServer = (Boolean) initialExecutionContext.get(LEAVE_ON_SERVER_KEY); if ((IMAP.equals(protocolType) || IMAPS.equals(protocolType)) && leaveOnServer) { flags.add(Flag.SEEN); } else { flags.add(Flag.DELETED); } rootFolder.setFlags(unreadMessagesArray, flags, true); } finally { if (rootFolder != null && rootFolder.isOpen()) { rootFolder.close(true); } if (store != null) { store.close(); } } } }