Search in sources :

Example 51 with Store

use of javax.mail.Store in project xwiki-platform by xwiki.

the class MailPluginApi method getMail.

public Message[] getMail(String provider, String server, String user, String password) throws MessagingException {
    // Get a session. Use a blank Properties object.
    Session session = Session.getInstance(new Properties());
    // Get a Store object
    Store store = session.getStore(provider);
    store.connect(server, user, password);
    try {
        // Get "INBOX"
        Folder fldr = store.getFolder("INBOX");
        fldr.open(Folder.READ_ONLY);
        return fldr.getMessages();
    } finally {
        store.close();
    }
}
Also used : Store(javax.mail.Store) Properties(java.util.Properties) Folder(javax.mail.Folder) Session(javax.mail.Session)

Example 52 with Store

use of javax.mail.Store in project data-transfer-project by google.

the class ImapMailHelper method getFolderContents.

public MailModelWrapper getFolderContents(String host, String account, String password, @Nullable String folderName, PaginationInformation paginationInformation) throws MessagingException, IOException {
    Properties props = createProperties(host, account, debug);
    Session session = Session.getInstance(props);
    Store store = session.getStore(PROTOCOL);
    log("getFolderContents connecting to store");
    try {
        store.connect(host, account, password);
    } catch (MessagingException e) {
        log("Exception connecting to: %s, error: %s", host, e.getMessage());
        throw e;
    }
    // If no folder specified, return the default folder
    if (folderName == null) {
        log("getMessages from default folder");
        Folder defaultFolder;
        try {
            defaultFolder = store.getDefaultFolder();
        } catch (MessagingException e) {
            log("Exception getting default folder: %s", e.getMessage());
            throw e;
        }
        return getMessages(host, account, password, defaultFolder, false, paginationInformation);
    }
    log("getMessages for specific folder: %s", folderName);
    // Fetch the contents of the specified folder
    Folder folder = store.getFolder(folderName);
    if (folder == null || !folder.exists()) {
        throw new IOException("Folder not found, name: " + folderName);
    }
    return getMessages(host, account, password, folder, true, paginationInformation);
}
Also used : MessagingException(javax.mail.MessagingException) Store(javax.mail.Store) IOException(java.io.IOException) Properties(java.util.Properties) Folder(javax.mail.Folder) Session(javax.mail.Session)

Example 53 with Store

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

the class AbstractNotificationTaskITCase method verifyMail.

protected boolean verifyMail(final String sender, final String subject, final String mailAddress) throws Exception {
    LOG.info("Waiting for notification to be sent...");
    greenMail.waitForIncomingEmail(1);
    boolean found = false;
    Session session = Session.getDefaultInstance(System.getProperties());
    Store store = session.getStore("pop3");
    store.connect(POP3_HOST, POP3_PORT, mailAddress, mailAddress);
    Folder inbox = store.getFolder("INBOX");
    assertNotNull(inbox);
    inbox.open(Folder.READ_WRITE);
    Message[] messages = inbox.getMessages();
    for (Message message : messages) {
        if (sender.equals(message.getFrom()[0].toString()) && subject.equals(message.getSubject())) {
            found = true;
            message.setFlag(Flags.Flag.DELETED, true);
        }
    }
    inbox.close(true);
    store.close();
    return found;
}
Also used : Message(javax.mail.Message) Store(javax.mail.Store) Folder(javax.mail.Folder) Session(javax.mail.Session)

Example 54 with Store

use of javax.mail.Store in project ofbiz-framework by apache.

the class JavaMailContainer method start.

/**
 * Start the container
 *
 * @return true if server started
 * @throws org.apache.ofbiz.base.container.ContainerException
 */
@Override
public boolean start() throws ContainerException {
    ContainerConfig.Configuration cfg = ContainerConfig.getConfiguration(name, configFile);
    String dispatcherName = ContainerConfig.getPropertyValue(cfg, "dispatcher-name", "JavaMailDispatcher");
    String delegatorName = ContainerConfig.getPropertyValue(cfg, "delegator-name", "default");
    this.deleteMail = "true".equals(ContainerConfig.getPropertyValue(cfg, "delete-mail", "false"));
    this.delegator = DelegatorFactory.getDelegator(delegatorName);
    this.dispatcher = ServiceContainer.getLocalDispatcher(dispatcherName, delegator);
    this.timerDelay = ContainerConfig.getPropertyValue(cfg, "poll-delay", 300000);
    // maximum size in bytes
    this.maxSize = ContainerConfig.getPropertyValue(cfg, "maxSize", 1000000);
    // load the userLogin object
    String runAsUser = ContainerConfig.getPropertyValue(cfg, "run-as-user", "system");
    try {
        this.userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", runAsUser).queryOne();
    } catch (GenericEntityException e) {
        Debug.logError(e, "Unable to load run-as-user UserLogin; cannot start container", module);
        return false;
    }
    // load the MCA configuration
    ServiceMcaUtil.readConfig();
    // load the listeners
    List<ContainerConfig.Configuration.Property> configs = cfg.getPropertiesWithValue("store-listener");
    for (ContainerConfig.Configuration.Property prop : configs) {
        Session session = this.makeSession(prop);
        Store store = this.getStore(session);
        stores.put(store, session);
        store.addStoreListener(new LoggingStoreListener());
    }
    // start the polling timer
    if (stores != null) {
        pollTimer.scheduleAtFixedRate(new PollerTask(dispatcher, userLogin), timerDelay, timerDelay, TimeUnit.MILLISECONDS);
    } else {
        Debug.logWarning("No JavaMail Store(s) configured; poller disabled.", module);
    }
    return true;
}
Also used : ContainerConfig(org.apache.ofbiz.base.container.ContainerConfig) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Store(javax.mail.Store) Session(javax.mail.Session)

Example 55 with Store

use of javax.mail.Store in project ofbiz-framework by apache.

the class JavaMailContainer method getStore.

protected Store getStore(Session session) throws ContainerException {
    // create the store object
    Store store;
    try {
        store = session.getStore();
    } catch (NoSuchProviderException e) {
        throw new ContainerException(e);
    }
    // re-write the URLName including the password for this store
    if (store != null && store.getURLName() != null) {
        URLName urlName = this.updateUrlName(store.getURLName(), session.getProperties());
        if (Debug.verboseOn()) {
            Debug.logVerbose("URLName - " + urlName.toString(), module);
        }
        try {
            store = session.getStore(urlName);
        } catch (NoSuchProviderException e) {
            throw new ContainerException(e);
        }
    }
    if (store == null) {
        throw new ContainerException("No store configured!");
    }
    // test the store
    try {
        store.connect();
        store.close();
    } catch (MessagingException e) {
        Debug.logError("Unable to connect to mail store : " + store.getURLName().toString() + " : " + e.getMessage(), module);
    }
    return store;
}
Also used : ContainerException(org.apache.ofbiz.base.container.ContainerException) MessagingException(javax.mail.MessagingException) URLName(javax.mail.URLName) Store(javax.mail.Store) NoSuchProviderException(javax.mail.NoSuchProviderException)

Aggregations

Store (javax.mail.Store)65 Folder (javax.mail.Folder)49 Message (javax.mail.Message)40 MimeMessage (javax.mail.internet.MimeMessage)30 Session (javax.mail.Session)23 Properties (java.util.Properties)18 MessagingException (javax.mail.MessagingException)16 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 IMAPFolder (com.sun.mail.imap.IMAPFolder)8 IOException (java.io.IOException)8 URLName (javax.mail.URLName)8 Flags (javax.mail.Flags)7 NoSuchProviderException (javax.mail.NoSuchProviderException)7 Date (java.util.Date)6 InternetAddress (javax.mail.internet.InternetAddress)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 Test (org.junit.Test)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)4 InputStream (java.io.InputStream)3