List of usage examples for javax.mail Store close
public synchronized void close() throws MessagingException
From source file:nl.ordina.bag.etl.mail.loader.IMAPMutatiesFileLoader.java
@Override public void processMessages() { try {/*from w w w . ja v a 2 s .com*/ Session session = Session.getDefaultInstance(new Properties(), null); Store store = session.getStore(protocol); if (port == 0) store.connect(host, username, password); else store.connect(host, port, username, password); Folder folder = store.getFolder(folderName); if (folder == null) throw new RuntimeException("Folder " + folderName + " not found!"); folder.open(Folder.READ_WRITE); try { FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false); Message messages[] = folder.search(ft); for (Message message : messages) { messageHandler.handle(message); message.setFlags(new Flags(Flags.Flag.SEEN), true); } } finally { folder.close(true); store.close(); } } catch (MessagingException | IOException | JAXBException e) { throw new ProcessingException(e); } }
From source file:org.syncope.core.notification.NotificationTest.java
private boolean verifyMail(final String sender, final String subject) { LOG.info("Waiting for notification to be sent..."); try {// w ww.j av a2 s .com Thread.sleep(10000); } catch (InterruptedException e) { } boolean found = false; try { Session session = Session.getDefaultInstance(System.getProperties()); Store store = session.getStore("pop3"); store.connect(pop3Host, mailAddress, mailPassword); Folder inbox = store.getFolder("INBOX"); assertNotNull(inbox); inbox.open(Folder.READ_WRITE); Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { if (sender.equals(messages[i].getFrom()[0].toString()) && subject.equals(messages[i].getSubject())) { found = true; messages[i].setFlag(Flag.DELETED, true); } } inbox.close(true); store.close(); } catch (Throwable t) { LOG.error("Unexpected exception", t); fail("Unexpected exception while fetching e-mail"); } return found; }
From source file:org.apache.axis2.transport.mail.MailClient.java
public int checkInbox(int mode) throws MessagingException, IOException { int numMessages = 0; if (mode == 0) { return 0; }/*from w w w . j a v a2s .co m*/ boolean show = (mode & SHOW_MESSAGES) > 0; boolean clear = (mode & CLEAR_MESSAGES) > 0; String action = (show ? "Show" : "") + ((show && clear) ? " and " : "") + (clear ? "Clear" : ""); log.info(action + " INBOX for " + from); Store store = session.getStore(); store.connect(); Folder root = store.getDefaultFolder(); Folder inbox = root.getFolder("inbox"); inbox.open(Folder.READ_WRITE); Message[] msgs = inbox.getMessages(); numMessages = msgs.length; if ((msgs.length == 0) && show) { log.info("No messages in inbox"); } for (int i = 0; i < msgs.length; i++) { MimeMessage msg = (MimeMessage) msgs[i]; if (show) { log.info(" From: " + msg.getFrom()[0]); log.info(" Subject: " + msg.getSubject()); log.info(" Content: " + msg.getContent()); } if (clear) { msg.setFlag(Flags.Flag.DELETED, true); } } inbox.close(true); store.close(); return numMessages; }
From source file:org.xwiki.contrib.mail.internal.AbstractMailStore.java
/** * @param folder//from ww w . j ava 2s.c o m * @param message * @throws MessagingException */ public void write(String folder, Message message) throws MessagingException { // getLogger().info("Delivering " + message + " to " + this.location + " / " + folder); final Store store = getJavamailStore(true); store.connect(); final Folder mailFolder = store.getDefaultFolder().getFolder(folder); if (!mailFolder.exists()) { mailFolder.create(Folder.HOLDS_MESSAGES); } mailFolder.open(Folder.READ_WRITE); // If message is already archived, do nothing final Message existingMessage = read(folder, message.getHeader("Message-ID")[0]); if (existingMessage == null) { // The Store Provider may add some headers to the message to store, but IMAPMessage are read-only // So we clone the message before storing it final MimeMessage cloned = cloneEmail(message); mailFolder.appendMessages(new Message[] { cloned }); } mailFolder.close(true); store.close(); }
From source file:com.ikon.util.MailUtils.java
/** * Test IMAP connection// w ww . jav a 2s . c om */ public static void testConnection(MailAccount ma) throws IOException { log.debug("testConnection({})", ma); Session session = Session.getDefaultInstance(getProperties()); Store store = null; Folder folder = null; try { store = session.getStore(ma.getMailProtocol()); store.connect(ma.getMailHost(), ma.getMailUser(), ma.getMailPassword()); folder = store.getFolder(ma.getMailFolder()); folder.open(Folder.READ_WRITE); folder.close(false); } catch (NoSuchProviderException e) { throw new IOException(e.getMessage()); } catch (MessagingException e) { throw new IOException(e.getMessage()); } finally { // Try to close folder if (folder != null && folder.isOpen()) { try { folder.close(false); } catch (MessagingException e) { throw new IOException(e.getMessage()); } } // Try to close store if (store != null) { try { store.close(); } catch (MessagingException e) { throw new IOException(e.getMessage()); } } } log.debug("testConnection: void"); }
From source file:de.saly.elasticsearch.imap.AbstractIMAPRiverUnitTest.java
protected void deleteMailsFromUserMailbox(final Properties props, final String folderName, final int start, final int deleteCount, final String user, final String password) throws MessagingException { final Store store = Session.getInstance(props).getStore(); store.connect(user, password);/*from w w w . ja v a 2s . c o m*/ checkStoreForTestConnection(store); final Folder f = store.getFolder(folderName); f.open(Folder.READ_WRITE); final int msgCount = f.getMessageCount(); final Message[] m = deleteCount == -1 ? f.getMessages() : f.getMessages(start, Math.min(msgCount, deleteCount + start - 1)); int d = 0; for (final Message message : m) { message.setFlag(Flag.DELETED, true); logger.info("Delete msgnum: {} with sid {}", message.getMessageNumber(), message.getSubject()); d++; } f.close(true); logger.info("Deleted " + d + " messages"); store.close(); }
From source file:org.apache.syncope.core.notification.NotificationTest.java
private boolean verifyMail(final String sender, final String subject) throws Exception { LOG.info("Waiting for notification to be sent..."); try {// w ww . java 2s . c o m Thread.sleep(1000); } catch (InterruptedException e) { } boolean found = false; Session session = Session.getDefaultInstance(System.getProperties()); Store store = session.getStore("pop3"); store.connect(pop3Host, pop3Port, mailAddress, mailPassword); Folder inbox = store.getFolder("INBOX"); assertNotNull(inbox); inbox.open(Folder.READ_WRITE); Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { if (sender.equals(messages[i].getFrom()[0].toString()) && subject.equals(messages[i].getSubject())) { found = true; messages[i].setFlag(Flag.DELETED, true); } } inbox.close(true); store.close(); return found; }
From source file:org.apache.syncope.core.logic.NotificationTest.java
private boolean verifyMail(final String sender, final String subject) throws Exception { LOG.info("Waiting for notification to be sent..."); try {/*from ww w . jav a 2 s . c o m*/ Thread.sleep(1000); } catch (InterruptedException e) { } boolean found = false; Session session = Session.getDefaultInstance(System.getProperties()); Store store = session.getStore("pop3"); store.connect(POP3_HOST, POP3_PORT, MAIL_ADDRESS, MAIL_PASSWORD); Folder inbox = store.getFolder("INBOX"); assertNotNull(inbox); inbox.open(Folder.READ_WRITE); Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { if (sender.equals(messages[i].getFrom()[0].toString()) && subject.equals(messages[i].getSubject())) { found = true; messages[i].setFlag(Flag.DELETED, true); } } inbox.close(true); store.close(); return found; }
From source file:com.szmslab.quickjavamail.receive.MailReceiver.java
/** * ????/*w w w .j a v a 2 s .c om*/ * * @param callback * ??1??? * @throws Exception */ public void execute(ReceiveIterationCallback callback) throws Exception { final Session session = useDefaultSession ? Session.getDefaultInstance(properties.getProperties(), properties.getAuthenticator()) : Session.getInstance(properties.getProperties(), properties.getAuthenticator()); session.setDebug(isDebug); Store store = null; Folder folder = null; try { store = session.getStore(properties.getProtocol()); store.connect(); folder = store.getFolder(folderName); folder.open(readonly ? Folder.READ_ONLY : Folder.READ_WRITE); final Message messages[] = folder.getMessages(); for (Message message : messages) { MessageLoader loader = new MessageLoader(message, !readonly); boolean isContinued = callback.iterate(loader); if (!readonly && loader.isDeleted()) { message.setFlag(Flags.Flag.DELETED, loader.isDeleted()); } if (!isContinued) { break; } } } finally { if (folder != null) { try { folder.close(!readonly); } catch (MessagingException e) { System.out.println(e); } } if (store != null) { try { store.close(); } catch (MessagingException e) { System.out.println(e); } } } }
From source file:com.treesbearfruit.icloudnotes.NotesSaver.java
public NotesSaver(String username, String password, String j, String wheretosave) throws Exception { // Get system properties Properties props = System.getProperties(); Session session = Session.getDefaultInstance(props); // Get the store Store store = session.getStore("imaps"); String noteFolderLabel = ""; if (j.toLowerCase().equals("apple")) { noteFolderLabel = "Notes"; store.connect("imap.mail.me.com", username, password); // username without @icloud.com } else if (j.toLowerCase().equals("google")) { noteFolderLabel = "Notes"; store.connect("imap.gmail.com", username, password); } else {//from w ww .ja v a 2 s . co m throw new Exception("Notesprovider not implemented!"); } String timestamp = new java.text.SimpleDateFormat("dd_MM_yyyy_hh_mm_ss").format(new Date()); String backup_directory = wheretosave + (wheretosave.endsWith(File.separator) ? "" : File.separator) + noteFolderLabel + "_" + timestamp + File.separator; // saves main folder save(store, backup_directory, noteFolderLabel); // folder..s Folder mainnotefolder = store.getFolder(noteFolderLabel); System.out.println("found " + mainnotefolder.list().length + " note folders"); Folder[] f = mainnotefolder.list(); for (Folder fd : f) { String backup_directory_i = backup_directory + fd.getName(); save(store, backup_directory_i, noteFolderLabel + "/" + fd.getName()); } // Close connection store.close(); }