Search in sources :

Example 61 with Store

use of javax.mail.Store in project mailim by zengsn.

the class EmailUtil method checkEmail.

public static boolean checkEmail(String email, String pwd) {
    String host = getDefaultAddr(email);
    String user = getUsername(email);
    Store store = login(host, user, pwd);
    if (store == null)
        return false;
    else
        return true;
}
Also used : Store(javax.mail.Store)

Example 62 with Store

use of javax.mail.Store in project common by zenlunatics.

the class MailLists method fetchAndSend.

// --------------------------------------------------------------------------
@AdminTask({ "list", "account name", "host", "password" })
public synchronized void fetchAndSend(String list, String account_name, String host, String password, DBConnection db) throws MessagingException {
    Session session = Session.getInstance(new Properties(), null);
    Folder folder = null;
    Store store = null;
    try {
        store = session.getStore(m_store_protocol);
        try {
            store.connect(host, account_name, password);
        } catch (AuthenticationFailedException e) {
            if (e.toString().indexOf("temporarily unavailable") == -1)
                try {
                    TimeUnit.SECONDS.sleep(15);
                    store.connect(host, account_name, password);
                } catch (AuthenticationFailedException e1) {
                    if (e1.toString().indexOf("temporarily unavailable") == -1)
                        sendError(list, "fetchAndSend", "store.connect", null, e1);
                    return;
                } catch (InterruptedException e1) {
                    sendError(list, "fetchAndSend", "store.connect", null, e1);
                    return;
                }
            else
                return;
        } catch (MailConnectException e) {
            m_site.log(e);
            if (host.equals("172.21.0.1") || host.equals("sonoracohousing.com"))
                return;
        }
        if (store.isConnected()) {
            folder = store.getFolder("INBOX");
            try {
                folder.open(Folder.READ_WRITE);
            } catch (MessagingException e) {
                if (e.toString().indexOf("* BYE") != -1)
                    return;
                throw e;
            }
            Message[] messages = folder.getMessages();
            long now = new Date().getTime();
            for (Message message : messages) {
                if (message.getHeader("X-ignore") != null || message.getHeader("auto-submitted") != null || message.getHeader("Auto-Submitted") != null || message.getHeader("X-Autoreply") != null || message.getHeader("X-Autorespond") != null) {
                    message.setFlag(Flags.Flag.DELETED, true);
                    continue;
                }
                Date received_date = message.getReceivedDate();
                long minutes = (now - received_date.getTime()) / 60000;
                if (minutes > 30)
                    sendError(list, "fetchAndSend", "message " + minutes + " minutes old", null, null);
                Address[] recipients = message.getRecipients(RecipientType.TO);
                if (recipients != null) {
                    String to = recipients[0].toString();
                    if (to == null)
                        System.out.println("to is null");
                    else if (to.indexOf('+') != -1)
                        message.setFlag(Flags.Flag.DELETED, true);
                }
                if (handleMessage(list, message, true, db))
                    message.setFlag(Flags.Flag.DELETED, true);
            }
        }
    } catch (MessagingException e) {
        if (e.toString().indexOf("Connection dropped by server?") == -1 && e.toString().indexOf("BYE JavaMail") == -1)
            m_site.log(e);
    // sendError(list, "fetchAndSend", null, e);
    } finally {
        try {
            if (folder != null && folder.isOpen())
                folder.close(true);
        } catch (Exception e) {
            sendError(list, "fetchAndSend", "closing folder", null, e);
        }
        store.close();
    }
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) AuthenticationFailedException(javax.mail.AuthenticationFailedException) MessagingException(javax.mail.MessagingException) Store(javax.mail.Store) MailConnectException(com.sun.mail.util.MailConnectException) Properties(java.util.Properties) Folder(javax.mail.Folder) Date(java.util.Date) LocalDate(java.time.LocalDate) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLException(java.sql.SQLException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) MailConnectException(com.sun.mail.util.MailConnectException) IOException(java.io.IOException) Session(javax.mail.Session) AdminTask(web.AdminTask)

Example 63 with Store

use of javax.mail.Store in project Openfire by igniterealtime.

the class POP3AuthProvider method authenticate.

@Override
public void authenticate(String username, String password) throws UnauthorizedException {
    if (username == null || password == null) {
        throw new UnauthorizedException();
    }
    if (username.contains("@")) {
        // Check that the specified domain matches the server's domain
        int index = username.indexOf("@");
        String domain = username.substring(index + 1);
        if (domain.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
            username = username.substring(0, index);
        }
    } else {
        // Unknown domain. Return authentication failed.
        throw new UnauthorizedException();
    }
    Log.debug("POP3AuthProvider.authenticate(" + username + ", ******)");
    // If cache is enabled, see if the auth is in cache.
    if (authCache != null && authCache.containsKey(username)) {
        String hash = authCache.get(username);
        if (StringUtils.hash(password).equals(hash)) {
            return;
        }
    }
    Properties mailProps = new Properties();
    mailProps.setProperty("mail.debug", String.valueOf(debugEnabled));
    Session session = Session.getInstance(mailProps, null);
    Store store;
    try {
        store = session.getStore(useSSL ? "pop3s" : "pop3");
    } catch (NoSuchProviderException e) {
        Log.error(e.getMessage(), e);
        throw new UnauthorizedException(e);
    }
    try {
        if (authRequiresDomain) {
            store.connect(host, port, username + "@" + domain, password);
        } else {
            store.connect(host, port, username, password);
        }
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
        throw new UnauthorizedException(e);
    }
    if (!store.isConnected()) {
        throw new UnauthorizedException("Could not authenticate user");
    }
    try {
        store.close();
    } catch (Exception e) {
    // Ignore.
    }
    // If cache is enabled, add the item to cache.
    if (authCache != null) {
        authCache.put(username, StringUtils.hash(password));
    }
    // See if the user exists in the database. If not, automatically create them.
    UserManager userManager = UserManager.getInstance();
    try {
        userManager.getUser(username);
    } catch (UserNotFoundException unfe) {
        String email = username + "@" + (domain != null ? domain : host);
        try {
            Log.debug("POP3AuthProvider: Automatically creating new user account for " + username);
            // Create user; use a random password for better safety in the future.
            // Note that we have to go to the user provider directly -- because the
            // provider is read-only, UserManager will usually deny access to createUser.
            UserManager.getUserProvider().createUser(username, StringUtils.randomString(8), null, email);
        } catch (UserAlreadyExistsException uaee) {
        // Ignore.
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) UserManager(org.jivesoftware.openfire.user.UserManager) Store(javax.mail.Store) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) Properties(java.util.Properties) NoSuchProviderException(javax.mail.NoSuchProviderException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) NoSuchProviderException(javax.mail.NoSuchProviderException) Session(javax.mail.Session)

Example 64 with Store

use of javax.mail.Store in project ats-framework by Axway.

the class ImapStorage method getFolder.

public ImapFolder getFolder(SearchTerm searchTerm) throws RbvStorageException {
    try {
        if (searchTerm.getClass().getName().equals(ImapFolderSearchTerm.class.getName())) {
            ImapFolderSearchTerm imapSearchTerm = (ImapFolderSearchTerm) searchTerm;
            Store store = session.getStore("imap");
            return new ImapFolder(store, imapServerHost, imapSearchTerm.getFolderName(), imapSearchTerm.getUserName(), imapSearchTerm.getPassword());
        } else if (searchTerm.getClass().getName().equals(ImapEncryptedFolderSearchTerm.class.getName())) {
            ImapEncryptedFolderSearchTerm imapSearchTerm = (ImapEncryptedFolderSearchTerm) searchTerm;
            Store store = session.getStore("imap");
            return new ImapEncryptedFolder(store, imapServerHost, imapSearchTerm.getFolderName(), imapSearchTerm.getUserName(), imapSearchTerm.getPassword(), imapSearchTerm.getEncryptor());
        } else {
            throw new RbvStorageException("Search term " + searchTerm.getClass().getSimpleName() + " is not supported");
        }
    } catch (NoSuchProviderException e) {
        throw new RuntimeException("Unable to get IMAP store for host " + imapServerHost, e);
    }
}
Also used : Store(javax.mail.Store) RbvStorageException(com.axway.ats.rbv.model.RbvStorageException) NoSuchProviderException(javax.mail.NoSuchProviderException)

Example 65 with Store

use of javax.mail.Store in project ats-framework by Axway.

the class MimePackage method closeStoreConnection.

/**
 * Close connection
 * <b>Note</b>Internal method
 * @throws MessagingException
 */
public void closeStoreConnection(boolean storeConnected) throws MessagingException {
    if (storeConnected) {
        // the folder is empty when the message is not loaded from IMAP server, but from a file
        Folder imapFolder = message.getFolder();
        if (imapFolder == null) {
            // in case of nested package but still originating from IMAP server
            imapFolder = partOfImapFolder;
        }
        if (imapFolder != null) {
            Store store = imapFolder.getStore();
            if (store != null && store.isConnected()) {
                // closing store closes and its folders
                log.debug("Closing store (" + store.toString() + ") and associated folders");
                store.close();
            }
        }
    }
}
Also used : Store(javax.mail.Store) Folder(javax.mail.Folder)

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