Example usage for javax.mail Session getStore

List of usage examples for javax.mail Session getStore

Introduction

In this page you can find the example usage for javax.mail Session getStore.

Prototype

public Store getStore(Provider provider) throws NoSuchProviderException 

Source Link

Document

Get an instance of the store specified by Provider.

Usage

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 {//  w  w w . j a 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();
}

From source file:org.apache.axis2.transport.mail.MailRequestResponseClient.java

@Setup
@SuppressWarnings("unused")
private void setUp(MailTestEnvironment env, MailChannel channel) throws MessagingException {
    this.channel = channel;
    Session session = channel.getReplySession();
    session.setDebug(log.isTraceEnabled());
    store = session.getStore(env.getProtocol());
    MailTestEnvironment.Account sender = channel.getSender();
    store.connect(sender.getLogin(), sender.getPassword());
}

From source file:com.trivago.mail.pigeon.mail.MailFacade.java

public void readBounceAccount() throws MessagingException {
    Session session = Session.getDefaultInstance(Settings.create().getConfiguration().getProperties("mail.*"));
    Store store = session.getStore("pop3");

    store.connect();/*from w  ww  .  ja  v  a2s . c  o  m*/

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    if (folder.hasNewMessages()) {
        // Get directory
        Message message[] = folder.getMessages();
        BounceFacade bounceFacade = new BounceFacade();

        for (Message msg : message) {
            boolean isBounce = bounceFacade.processBounce(msg);
            if (isBounce) {
                msg.setFlag(Flags.Flag.SEEN, true);
                msg.saveChanges();
            }
        }
    }

}

From source file:com.yfiton.notifiers.email.EmailNotifierTest.java

private boolean checkEmailReception(String subject, String host, String username, String password)
        throws MessagingException {
    Properties properties = new Properties();
    properties.put("mail.store.protocol", "imaps");
    Session session = Session.getInstance(properties);

    Store store = null;//from w w  w.ja v  a  2s .  c  o  m

    try {
        store = session.getStore("imaps");
        store.connect(host, username, password);

        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);

        Message[] messages = inbox.getMessages();

        for (Message message : messages) {
            if (message.getSubject().equals(subject)) {
                message.setFlag(Flags.Flag.DELETED, true);
                return true;
            }
        }

        inbox.close(true);
    } finally {
        try {
            if (store != null) {
                store.close();
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    return false;
}

From source file:com.silverpeas.mailinglist.service.job.TestZimbraConnection.java

@Test
public void testOpenImapConnection() {
    URL url = this.getClass().getClassLoader().getResource("truststore.jks");
    String path = url.getPath();//from   ww w .  j  a v a  2s.  c  om
    System.setProperty("javax.net.ssl.trustStore", path);
    System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
    Store mailAccount = null;
    Folder inbox = null;
    Session mailSession = Session.getInstance(System.getProperties());
    try {
        mailSession.setDebug(true);
        mailAccount = mailSession.getStore(props.getProperty("mail.server.protocol"));
        mailAccount.connect(props.getProperty("mail.server.host"),
                Integer.parseInt(props.getProperty("mail.server.port")), props.getProperty("mail.server.login"),
                props.getProperty("mail.server.password"));
        inbox = mailAccount.getFolder("INBOX");
        if (inbox == null) {
            throw new MessagingException("No POP3 INBOX");
        }
        // -- Open the folder for read write --
        inbox.open(Folder.READ_WRITE);

        // -- Get the message wrappers and process them --
        Message[] msgs = inbox.getMessages();
        FetchProfile profile = new FetchProfile();
        profile.add(FetchProfile.Item.FLAGS);
        inbox.fetch(msgs, profile);
    } catch (MessagingException mex) {
        SilverTrace.error("mailingList", "MessageChecker.checkNewMessages", "mail.processing.error", mex);
    } catch (Exception mex) {
        SilverTrace.error("mailingList", "MessageChecker.checkNewMessages", "mail.processing.error", mex);
    } finally {
        // -- Close down nicely --
        try {
            if (inbox != null) {
                inbox.close(false);
            }
            if (mailAccount != null) {
                mailAccount.close();
            }
        } catch (Exception ex2) {
            SilverTrace.error("mailingList", "MessageChecker.checkNewMessages", "mail.processing.error", ex2);
        }
    }

}

From source file:com.google.code.rptm.mailarchive.DefaultMailingListArchive.java

public void retrieveMessages(String mailingList, YearMonth month, MimeMessageProcessor processor,
        MailingListArchiveEventListener eventListener) throws MailingListArchiveException {
    Session session = Session.getDefaultInstance(new Properties());
    try {// w  w w .  jav  a2 s.c om
        Store store = session.getStore(new URLName("mstor:" + getMboxFile(mailingList, month, eventListener)));
        store.connect();
        try {
            Folder folder = store.getDefaultFolder();
            folder.open(Folder.READ_ONLY);
            for (Message msg : folder.getMessages()) {
                if (!processor.processMessage((MimeMessage) msg)) {
                    break;
                }
            }
        } finally {
            store.close();
        }
    } catch (MessagingException ex) {
        throw new MailingListArchiveException("JavaMail exception: " + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new MailingListArchiveException("I/O exception: " + ex.getMessage(), ex);
    }
}

From source file:com.silverpeas.mailinglist.service.job.TestYahooMailConnection.java

@Test
public void testOpenImapConnection() throws Exception {
    Store mailAccount = null;//from ww  w .j a v  a2s . c  o  m
    Folder inbox = null;
    Session mailSession = Session.getInstance(System.getProperties());
    try {
        mailSession.setDebug(true);
        mailAccount = mailSession.getStore(props.getProperty("mail.server.protocol"));
        mailAccount.connect(props.getProperty("mail.server.host"),
                Integer.parseInt(props.getProperty("mail.server.port")), props.getProperty("mail.server.login"),
                props.getProperty("mail.server.password"));
        inbox = mailAccount.getFolder("INBOX");
        if (inbox == null) {
            throw new MessagingException("No POP3 INBOX");
        }
        // -- Open the folder for read write --
        inbox.open(Folder.READ_WRITE);

        // -- Get the message wrappers and process them --
        javax.mail.Message[] msgs = inbox.getMessages();
        FetchProfile profile = new FetchProfile();
        profile.add(FetchProfile.Item.FLAGS);
        inbox.fetch(msgs, profile);
        MailProcessor processor = new MailProcessor();
        MessageListener mailingList = mock(MessageListener.class);
        when(mailingList.checkSender(anyString())).thenReturn(Boolean.TRUE);
        when(mailingList.getComponentId()).thenReturn("mailingList38");
        MessageEvent event = new MessageEvent();
        for (javax.mail.Message message : msgs) {
            processor.prepareMessage((MimeMessage) message, mailingList, event);
        }
        assertThat(event.getMessages(), is(notNullValue()));
        assertThat(event.getMessages().size(), is(msgs.length));
        for (com.silverpeas.mailinglist.service.model.beans.Message message : event.getMessages()) {
            assertThat(message, is(notNullValue()));
            assertThat(message.getMessageId(), is(notNullValue()));
        }
    } finally {
        // -- Close down nicely --
        if (inbox != null) {
            inbox.close(false);
        }
        if (mailAccount != null) {
            mailAccount.close();
        }
    }
}

From source file:com.ikon.util.MailUtils.java

/**
 * Test IMAP connection//from w  w  w.  j av a 2s.co m
 */
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:org.apache.hupa.server.InMemoryIMAPStoreCache.java

public CachedIMAPStore createCachedIMAPStore(User user) throws NoSuchProviderException {
    Session ses = createSession(user);
    IMAPStore store = (IMAPStore) ses.getStore(user.getSettings().getImapSecure() ? "imaps" : "imap");
    CachedIMAPStore ret = new CachedIMAPStore(store, 300);
    ret.setSession(ses);/* ww w.jav a  2 s. co  m*/
    return ret;
}

From source file:org.openadaptor.auxil.connector.mail.MailConnection.java

/**
 * Creates a connection to the remote server
 *
 * @throws MessagingException if there is a comms error
 *//*from  w  ww .ja va2 s .  co  m*/
public void connect() throws MessagingException {
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);

    Session session = Session.getDefaultInstance(props, null);
    store = session.getStore(protocol);
    store.connect(host, user, password);

    log.debug("Connected to:     " + host);
    log.debug("  Port:           " + port);
    log.debug("  Username:       " + user);
    log.debug("  Protocol:       " + protocol);
    log.debug("  Default folder: " + store.getDefaultFolder());
    log.debug("  URL name:       " + store.getURLName());
}