Search in sources :

Example 11 with Store

use of javax.mail.Store in project camel by apache.

the class MailSortTermTwoTest method prepareMailbox.

private void prepareMailbox() throws Exception {
    // connect to mailbox
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    Store store = sender.getSession().getStore("pop3");
    store.connect("localhost", 25, "bill", "secret");
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.expunge();
    // inserts 3 messages, one with earlier, one with later sent date and one with invalid subject (not returned in search)
    Message[] messages = new Message[3];
    messages[0] = new MimeMessage(sender.getSession());
    messages[0].setText("Earlier date");
    messages[0].setHeader("Message-ID", "0");
    messages[0].setSentDate(new Date(10000));
    messages[0].setSubject("Camel");
    messages[1] = new MimeMessage(sender.getSession());
    messages[1].setText("Later date");
    messages[1].setHeader("Message-ID", "1");
    messages[1].setSentDate(new Date(20000));
    messages[1].setSubject("Camel");
    messages[2] = new MimeMessage(sender.getSession());
    messages[2].setText("Even later date");
    messages[2].setHeader("Message-ID", "2");
    messages[2].setSentDate(new Date(30000));
    messages[2].setSubject("Invalid");
    folder.appendMessages(messages);
    folder.close(true);
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Store(javax.mail.Store) Folder(javax.mail.Folder) Date(java.util.Date)

Example 12 with Store

use of javax.mail.Store in project jodd by oblac.

the class Pop3Server method createSession.

/**
	 * {@inheritDoc}
	 */
public ReceiveMailSession createSession() {
    Session session = Session.getInstance(sessionProperties, authenticator);
    Store store;
    try {
        store = getStore(session);
    } catch (NoSuchProviderException nspex) {
        throw new MailException("Failed to create POP3 session", nspex);
    }
    return new ReceiveMailSession(session, store);
}
Also used : Store(javax.mail.Store) NoSuchProviderException(javax.mail.NoSuchProviderException) Session(javax.mail.Session)

Example 13 with Store

use of javax.mail.Store in project gocd by gocd.

the class Pop3MailClient method getInboxFolder.

private Folder getInboxFolder() throws MessagingException {
    Properties pop3Props = new Properties();
    pop3Props.setProperty("mail.pop3.host", host);
    Authenticator auth = new PopupAuthenticator();
    Session session = Session.getInstance(pop3Props, auth);
    URLName url = new URLName("pop3", host, port, "", username, password);
    Store store = session.getStore(url);
    store.connect();
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    return folder;
}
Also used : URLName(javax.mail.URLName) Store(javax.mail.Store) Properties(java.util.Properties) Folder(javax.mail.Folder) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session)

Example 14 with Store

use of javax.mail.Store in project nhin-d by DirectProject.

the class MessageServiceImplService method requestStatus.

@Override
public StatusResponseType requestStatus(StatusRefType body) {
    List<String> msgs = body.getMessageID();
    StatusResponseType response = new StatusResponseType();
    try {
        checkAuth(response);
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(imapProps, auth);
        session.setDebug(true);
        if (msgs.size() > 0) {
            Store store = session.getStore(new javax.mail.URLName("imaps://" + username));
            store.connect(getImapHost(), Integer.valueOf(getImapPort()).intValue(), username, password);
            for (int x = 0; x < msgs.size(); x++) {
                String msgid = msgs.get(x);
                MessageIDTerm messageIdTerm = new MessageIDTerm(msgid);
                IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
                folder.open(Folder.READ_ONLY);
                SearchTerm st = messageIdTerm;
                IMAPMessage[] msgsearch = (IMAPMessage[]) folder.search(st);
                if (msgsearch.length > 0) {
                    Flags flags = msgsearch[0].getFlags();
                    Flag[] inboxflags = flags.getSystemFlags();
                    String[] listofflags = new String[inboxflags.length];
                    listofflags = setSystemFlags(inboxflags);
                    setMessageIdStatus(msgid, listofflags, response.getMessageIDAndStatus());
                }
            }
        }
    } catch (AddressException e) {
        log.error(e);
    } catch (MessagingException e) {
        log.error(e);
    } catch (Exception e) {
        log.error(e);
    }
    return response;
}
Also used : MessageIDTerm(javax.mail.search.MessageIDTerm) MessagingException(javax.mail.MessagingException) IMAPFolder(com.sun.mail.imap.IMAPFolder) Store(javax.mail.Store) Flags(javax.mail.Flags) SearchTerm(javax.mail.search.SearchTerm) Flag(javax.mail.Flags.Flag) StatusResponseType(org.nhindirect.schema.edge.ws.StatusResponseType) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) IOException(java.io.IOException) AddressException(javax.mail.internet.AddressException) IMAPMessage(com.sun.mail.imap.IMAPMessage) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session)

Example 15 with Store

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

the class MailTransportMonitor method readTestMessage.

private PollStatus readTestMessage(final MailTransportParameters mailParms) {
    LOG.debug("readTestMessage: Beginning read mail test.");
    PollStatus status = PollStatus.unavailable("Test not completed.");
    final long interval = mailParms.getReadTestAttemptInterval();
    if (mailParms.isEnd2EndTestInProgress()) {
        LOG.debug("Initially delaying read test: {} because end to end test is in progress.", mailParms.getReadTestAttemptInterval());
        if (delayTest(status, interval) == PollStatus.SERVICE_UNKNOWN) {
            return status;
        }
    }
    Store mailStore = null;
    Folder mailFolder = null;
    try {
        final JavaMailer readMailer = new JavaMailer(mailParms.getJavamailProperties());
        setReadMailProperties(mailParms, readMailer);
        final TimeoutTracker tracker = new TimeoutTracker(mailParms.getParameterMap(), mailParms.getRetries(), mailParms.getTimeout());
        for (tracker.reset(); tracker.shouldRetry(); tracker.nextAttempt()) {
            tracker.startAttempt();
            if (tracker.getAttempt() > 0) {
                if (delayTest(status, interval) == PollStatus.SERVICE_UNKNOWN) {
                    LOG.warn("readTestMessage: Status set to: {} during delay, exiting test.", status);
                    break;
                }
            }
            LOG.debug("readTestMessage: reading mail attempt: {}, elapsed time: {}ms.", (tracker.getAttempt() + 1), String.format("%.2f", tracker.elapsedTimeInMillis()));
            try {
                mailStore = readMailer.getSession().getStore();
                mailFolder = retrieveMailFolder(mailParms, mailStore);
                mailFolder.open(Folder.READ_WRITE);
            } catch (final MessagingException e) {
                if (tracker.shouldRetry()) {
                    LOG.warn("readTestMessage: error reading INBOX", e);
                    closeStore(mailStore, mailFolder);
                    // try again to get mail Folder from Store
                    continue;
                } else {
                    LOG.warn("readTestMessage: error reading INBOX", e);
                    return PollStatus.down(e.getLocalizedMessage());
                }
            }
            if (mailFolder.isOpen() && (mailParms.getReadTest().getSubjectMatch() != null || mailParms.isEnd2EndTestInProgress())) {
                status = processMailSubject(mailParms, mailFolder);
                if (status.getStatusCode() == PollStatus.SERVICE_AVAILABLE) {
                    break;
                }
            }
        }
    } catch (final JavaMailerException e) {
        status = PollStatus.down(e.getLocalizedMessage());
    } finally {
        closeStore(mailStore, mailFolder);
    }
    return status;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) MessagingException(javax.mail.MessagingException) Store(javax.mail.Store) JavaMailerException(org.opennms.javamail.JavaMailerException) JavaMailer(org.opennms.javamail.JavaMailer) Folder(javax.mail.Folder)

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