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:org.wso2.esb.integration.common.utils.MailToTransportUtil.java

/**
 * Getting the connection to email using Mail API
 *
 * @return - Email message Store/*from   w  w  w  .jav  a  2 s .  c om*/
 * @throws ESBMailTransportIntegrationTestException - Is thrown if an error while connecting to email store
 */
private static Store getConnection() throws ESBMailTransportIntegrationTestException {
    Properties properties;
    Session session;

    properties = new Properties();
    properties.setProperty("mail.host", "imap.gmail.com");
    properties.setProperty("mail.port", "995");
    properties.setProperty("mail.transport.protocol", "imaps");

    session = Session.getInstance(properties, new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(receiver + "@" + domain, String.valueOf(receiverPassword));
        }
    });
    try {
        Store store = session.getStore("imaps");
        store.connect();
        return store;
    } catch (MessagingException e) {
        log.error("Error when creating the email store ", e);
        throw new ESBMailTransportIntegrationTestException("Error when creating the email store ", e);
    }
}

From source file:com.aurel.track.util.emailHandling.MailReader.java

public synchronized static Message[] readAll(String protocol, String server, int securityConnection, int port,
        String user, String password, boolean onlyUnreadMessages) {
    Message[] msgs = null;/*  w ww .j  av a2  s  . co m*/
    try {
        Properties props = System.getProperties();
        updateSecurityProps(protocol, server, securityConnection, props);
        Session session = Session.getDefaultInstance(props, null);

        if (LOGGER.isDebugEnabled()) {
            session.setDebug(true);
        } else {
            session.setDebug(false);
        }

        // instantiate POP3-store and connect to server
        store = session.getStore(protocol);
        boolean connected = SyncStore.connect(store, protocol, server, port, user, password);
        if (!connected) {
            return msgs;
        }
        connPoint = SyncStore.getConnPoint(store, protocol, server, port, user, password);
        store = connPoint.getStore();

        // access default folder
        folder = store.getDefaultFolder();

        // can't find default folder
        if (folder == null) {
            throw new Exception("No default folder");
        }

        // messages are always in folder INBOX
        folder = folder.getFolder("INBOX");

        // can't find INBOX
        if (folder == null) {
            throw new Exception("No POP3 INBOX");
        }

        // open folder
        folder.open(Folder.READ_WRITE);

        // retrieve messages
        if (onlyUnreadMessages) {
            FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
            msgs = folder.search(ft);
        } else {
            msgs = folder.getMessages();
        }
    } catch (Exception ex) {
        if (LogThrottle.isReady("MailReader1", 240)) {
            LOGGER.error(ExceptionUtils.getStackTrace(ex));
        }
    }
    return msgs;
}

From source file:edu.ku.brc.helpers.EMailHelper.java

/**
 * Retrieves a list of all the available messages. For POP3 it checks the local mailbox and downloads any on the server.
 *  For IMAP it returns any in the INBOX.
 * @param msgList the list to be filled// w  w  w . j  a  v a 2s .  com
 * @return true if not erros, false if an error occurred.
 */
public static boolean getAvailableMsgs(java.util.List<javax.mail.Message> msgList) {
    boolean status = false; // assume it will fail

    msgList.clear();

    try {
        String usernameStr = AppPreferences.getRemote().get("settings.email.username", null); //$NON-NLS-1$
        String passwordStr = Encryption
                .decrypt(AppPreferences.getRemote().get("settings.email.password", null)); //$NON-NLS-1$
        String emailStr = AppPreferences.getRemote().get("settings.email.email", null); //$NON-NLS-1$
        String smtpStr = AppPreferences.getRemote().get("settings.email.smtp", null); //$NON-NLS-1$
        String serverNameStr = AppPreferences.getRemote().get("settings.email.servername", null); //$NON-NLS-1$
        String acctTypeStr = AppPreferences.getRemote().get("settings.email.accounttype", null); //$NON-NLS-1$
        String localMailBoxStr = AppPreferences.getRemote().get("settings.email.localmailbox", null); //$NON-NLS-1$

        EMailHelper.AccountType acctType = EMailHelper.getAccountType(acctTypeStr);

        if (!hasEMailSettings(usernameStr, passwordStr, emailStr, smtpStr, serverNameStr, acctTypeStr,
                localMailBoxStr)) {
            JOptionPane.showMessageDialog(UIRegistry.getMostRecentWindow(),
                    getResourceString("EMailHelper.EMAIL_SET_NOT_VALID")); //$NON-NLS-1$
        }

        // Open Local Box if POP
        if (acctTypeStr.equals(getResourceString("EMailHelper.POP3"))) //$NON-NLS-1$
        {
            try {
                Properties props = new Properties();
                Session session = Session.getDefaultInstance(props);

                Store store = session.getStore(new URLName("mstor:" + localMailBoxStr)); //$NON-NLS-1$
                store.connect();
                status = getMessagesFromInbox(store, msgList); // closes everything

            } catch (Exception ex) {
                edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
                edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(EMailHelper.class, ex);

                instance.lastErrorMsg = ex.toString();
                ex.printStackTrace();
                status = false;
            }
        } else {
            throw new RuntimeException("Unknown Account Type [" + acctTypeStr + "] must be POP3 or IMAP"); // XXX FIXME //$NON-NLS-1$ //$NON-NLS-2$
        }

        // Try to download message from pop account
        try {
            Properties props = System.getProperties();
            Session session = Session.getInstance(props, null);

            if (acctType == AccountType.POP3) {
                Store store = session.getStore("pop3"); //$NON-NLS-1$
                store.connect(serverNameStr, usernameStr, passwordStr);
                status = getMessagesFromInbox(store, msgList); // closes everything

            } else if (acctType == AccountType.IMAP) {
                Store store = session.getStore("imap"); //$NON-NLS-1$
                store.connect(serverNameStr, usernameStr, passwordStr);
                status = getMessagesFromInbox(store, msgList); // closes everything

            } else {
                String msgStr = getResourceString("EMailHelper.UNKNOWN_ACCT_TYPE")// //$NON-NLS-1$
                        + acctTypeStr + getResourceString("EMailHelper.ACCT_TYPE_MUST_BE"); // XXX// //$NON-NLS-2$

                instance.lastErrorMsg = msgStr;
                throw new RuntimeException(msgStr); // XXX FIXME
            }
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(EMailHelper.class, ex);
            instance.lastErrorMsg = ex.toString();
            status = false;
        }

    } catch (Exception ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(EMailHelper.class, ex);
        instance.lastErrorMsg = ex.toString();
        status = false;
    }
    return status;
}

From source file:net.wastl.webmail.authenticators.IMAPSAuthenticator.java

@Override
public void init(Storage store) {
    storage = store;/*from  w  w w  .j a v a2  s . c  o  m*/
    final Session session = Session.getDefaultInstance(System.getProperties(), null);
    try {
        st = session.getStore("imaps");
    } catch (final NoSuchProviderException e) {
        log.error("Initialization for 'imaps' failed", e);
    }
}

From source file:net.wastl.webmail.authenticators.POPSAuthenticator.java

@Override
public void init(Storage store) {
    storage = store;//from w  ww  .  ja  v a 2 s .c om
    final Session session = Session.getDefaultInstance(System.getProperties(), null);
    try {
        st = session.getStore("pop3s");
    } catch (final NoSuchProviderException e) {
        log.error("Initialization for 'pop3s' failed", e);
    }
}

From source file:nl.ordina.bag.etl.mail.loader.IMAPMutatiesFileLoader.java

@Override
public void processMessages() {
    try {/*from w  w w  .j ava 2s  . c o  m*/
        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:com.hs.mail.web.controller.FetchAccountFormController.java

private Store connect(String userName, String password) throws MessagingException {
    Session session = Session.getInstance(System.getProperties(), null);
    Store store = session.getStore("imap");
    store.connect("localhost", userName, password);
    return store;
}

From source file:nl.ordina.bag.etl.mail.loader.POP3MutatiesFileLoader.java

@Override
public void processMessages() {
    try {/*w w w.  ja  va  2s . c  o  m*/
        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 {
            Message[] messages = folder.getMessages();
            for (Message message : messages) {
                backup(message);
                messageHandler.handle(message);
                message.setFlags(new Flags(Flags.Flag.DELETED), true);
            }
        } finally {
            folder.close(true);
            store.close();
        }
    } catch (MessagingException | IOException | JAXBException e) {
        throw new ProcessingException(e);
    }
}

From source file:net.wastl.webmail.authenticators.IMAPAuthenticator.java

public void init(Storage store) {
    storage = store;//from   ww w.  j  a  va 2 s .  co  m
    Session session = Session.getDefaultInstance(System.getProperties(), null);
    try {
        st = session.getStore("imap");
    } catch (NoSuchProviderException e) {
        log.error("Initialization failed", e);
    }
}

From source file:net.wastl.webmail.authenticators.POPAuthenticator.java

public void init(Storage store) {
    storage = store;/*from  ww w.j a  v  a  2s . c  o  m*/
    Session session = Session.getDefaultInstance(System.getProperties(), null);
    try {
        st = session.getStore("pop3");
    } catch (NoSuchProviderException e) {
        log.error("Initialization failed", e);
    }
}