Search in sources :

Example 16 with Store

use of javax.mail.Store in project opennms by OpenNMS.

the class JavaReadMailer method retrieveMessages.

/*
     * TODO: Need readers that:
     *   - use FetchProfiles
     *   - make use of pre-fetch nature of getMessages()
     *   - A reader for the entire system could be implemented using events/event listeners
     * TODO: Need to make this more efficient... probably needs state so that message contents can be retrieved from the
     *       store after they are read via this message.
     */
/**
 * <p>retrieveMessages</p>
 *
 * @param term a {@link javax.mail.search.SearchTerm} object.
 * @return a {@link java.util.List} object.
 * @throws org.opennms.javamail.JavaMailerException if any.
 */
public List<Message> retrieveMessages(SearchTerm term) throws JavaMailerException {
    Message[] msgs;
    Folder mailFolder = null;
    final ReadmailHost readmailHost = getReadmailHost(m_config);
    final UserAuth userAuth = getUserAuth(m_config);
    try {
        Store store = m_session.getStore(readmailHost.getReadmailProtocol().getTransport());
        store.connect(readmailHost.getHost(), (int) readmailHost.getPort(), userAuth.getUserName(), userAuth.getPassword());
        mailFolder = store.getFolder(m_config.getMailFolder());
        mailFolder.open(Folder.READ_WRITE);
        msgs = mailFolder.search(term);
    } catch (NoSuchProviderException e) {
        throw new JavaMailerException("No provider matching:" + readmailHost.getReadmailProtocol().getTransport() + " from config:" + m_config.getName(), e);
    } catch (MessagingException e) {
        throw new JavaMailerException("Problem reading messages from configured mail store", e);
    }
    List<Message> msgList = Arrays.asList(msgs);
    return msgList;
}
Also used : Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) Store(javax.mail.Store) ReadmailHost(org.opennms.netmgt.config.javamail.ReadmailHost) Folder(javax.mail.Folder) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) NoSuchProviderException(javax.mail.NoSuchProviderException)

Example 17 with Store

use of javax.mail.Store in project epadd by ePADD.

the class EmailStore method getNMessages.

/**
 * returns # of messages in each of the given folders
 */
public int[] getNMessages(String[] fnames) throws MessagingException {
    Store store = connect();
    int[] x = new int[fnames.length];
    for (int i = 0; i < x.length; i++) {
        Pair<Folder, Integer> pair = openFolder(store, fnames[i]);
        Folder f = pair.getFirst();
        if (f == null) {
            x[i] = -1;
            continue;
        }
        int count = pair.getSecond();
        if (count != -1)
            f.close(false);
        x[i] = count;
    }
    try {
        store.close();
    } catch (Exception e) {
        log.warn("Exception in closing folder " + this + ":" + e);
    }
    return x;
}
Also used : Store(javax.mail.Store) Folder(javax.mail.Folder) MessagingException(javax.mail.MessagingException)

Example 18 with Store

use of javax.mail.Store in project epadd by ePADD.

the class EmailStore method getNMessages.

/**
 * returns # of messages in the given folder
 */
public int getNMessages(String fname) throws MessagingException {
    // first check if we've already cached it
    if (this.folderInfos != null)
        for (FolderInfo fi : this.folderInfos) if (fi.longName.equals(fname))
            return fi.messageCount;
    Store store = connect();
    Pair<Folder, Integer> pair = openFolder(store, fname);
    Folder f = pair.getFirst();
    if (f == null)
        return -1;
    int count = pair.getSecond();
    if (count != -1)
        f.close(false);
    store.close();
    return count;
}
Also used : Store(javax.mail.Store) Folder(javax.mail.Folder)

Example 19 with Store

use of javax.mail.Store in project epadd by ePADD.

the class ImapPopEmailStore method connect.

// connects to the store, returns it as well as stores it in this.store
public Store connect() throws MessagingException {
    if (// should be at least imap or pop
    Util.nullOrEmpty(connectOptions.protocol))
        return null;
    // Get a Session object
    // can customize javamail properties here, see e.g. http://java.sun.com/products/javamail/javadocs/com/sun/mail/imap/package-summary.html
    // login form will prepend the magic string xoauth, if oauth is being used
    String oauthToken = "";
    // defined in loginform
    String OAUTH_MAGIC_STRING = "xoauth";
    if (connectOptions.password.startsWith(OAUTH_MAGIC_STRING)) {
        if ("xoauth".length() < connectOptions.password.length()) {
            oauthToken = connectOptions.password.substring(OAUTH_MAGIC_STRING.length());
            mstoreProps.put("mail.imaps.sasl.enable", "true");
            mstoreProps.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
            // mstoreProps.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
            log.info("Using oauth login for store " + this);
        }
    }
    session = Session.getInstance(mstoreProps, null);
    // session.setDebug(DEBUG);
    Store st = session.getStore(connectOptions.protocol);
    // no password if oauth
    st.connect(connectOptions.server, connectOptions.port, connectOptions.userName, Util.nullOrEmpty(oauthToken) ? connectOptions.password : "");
    this.store = st;
    return st;
}
Also used : Store(javax.mail.Store)

Example 20 with Store

use of javax.mail.Store in project epadd by ePADD.

the class VerifyEmailSetup method run.

public static Pair<Boolean, String> run() {
    PrintStream savedOut = System.out;
    InputStream savedIn = System.in;
    try {
        // Get a Properties object
        Properties props = System.getProperties();
        // Get a Session object
        Session session = Session.getInstance(props, null);
        session.setDebug(true);
        String filename = System.getProperty("java.io.tmpdir") + File.separatorChar + "verifyEmailSetup";
        PrintStream ps = new PrintStream(new FileOutputStream(filename));
        System.setOut(ps);
        System.setErr(ps);
        // Get a Store object
        Store store = null;
        store = session.getStore("imaps");
        // not the real password. unfortunately, the checkmuse a/c will get blocked by google.
        store.connect("imap.gmail.com", 993, "checkmuse", "");
        // Folder folder = store.getFolder("[Gmail]/Sent Mail");
        // int totalMessages = folder.getMessageCount();
        // System.err.println (totalMessages + " messages!");
        ps.close();
        String contents = Util.getFileContents(filename);
        System.out.println(contents);
        return new Pair<>(Boolean.TRUE, contents);
    } catch (AuthenticationFailedException e) {
        /* its ok if auth failed. we only want to check if IMAPS network route is blocked.
			 when network is blocked, we'll get something like
			javax.mail.MessagingException: No route to host; 
			nested exception is: 
			java.net.NoRouteToHostException: No route to host 
			...
			*/
        log.info("Verification succeeded: " + Util.stackTrace(e));
        return new Pair<>(Boolean.TRUE, "");
    } catch (Exception e) {
        log.warn("Verification failed: " + Util.stackTrace(e));
        // stack track reveals too much about our code... Util.stackTrace(e));
        return new Pair<>(Boolean.FALSE, e.toString());
    } finally {
        System.setOut(savedOut);
        System.setIn(savedIn);
    }
}
Also used : PrintStream(java.io.PrintStream) AuthenticationFailedException(javax.mail.AuthenticationFailedException) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) Store(javax.mail.Store) Properties(java.util.Properties) AuthenticationFailedException(javax.mail.AuthenticationFailedException) Session(javax.mail.Session) Pair(edu.stanford.muse.util.Pair)

Aggregations

Store (javax.mail.Store)70 Folder (javax.mail.Folder)51 Message (javax.mail.Message)42 MimeMessage (javax.mail.internet.MimeMessage)31 Session (javax.mail.Session)25 Properties (java.util.Properties)21 MessagingException (javax.mail.MessagingException)20 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 IMAPFolder (com.sun.mail.imap.IMAPFolder)10 NoSuchProviderException (javax.mail.NoSuchProviderException)9 IOException (java.io.IOException)8 URLName (javax.mail.URLName)8 Flags (javax.mail.Flags)7 Date (java.util.Date)6 InternetAddress (javax.mail.internet.InternetAddress)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 InputStream (java.io.InputStream)4 Test (org.junit.Test)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)4