Search in sources :

Example 11 with NoSuchProviderException

use of javax.mail.NoSuchProviderException 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 12 with NoSuchProviderException

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

the class SmtpServer method createSession.

/**
	 * {@inheritDoc}
	 */
public SendMailSession createSession() {
    Properties sessionProperties = createSessionProperties();
    if (additionalProperties != null) {
        sessionProperties.putAll(additionalProperties);
    }
    Session mailSession = Session.getInstance(sessionProperties, authenticator);
    Transport mailTransport;
    try {
        mailTransport = getTransport(mailSession);
    } catch (NoSuchProviderException nspex) {
        throw new MailException(nspex);
    }
    return new SendMailSession(mailSession, mailTransport);
}
Also used : Properties(java.util.Properties) Transport(javax.mail.Transport) NoSuchProviderException(javax.mail.NoSuchProviderException) Session(javax.mail.Session)

Example 13 with NoSuchProviderException

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

the class ImapServer 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 IMAP session", nspex);
    }
    return new ReceiveMailSession(session, store);
}
Also used : Store(javax.mail.Store) NoSuchProviderException(javax.mail.NoSuchProviderException) Session(javax.mail.Session)

Example 14 with NoSuchProviderException

use of javax.mail.NoSuchProviderException 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 {
            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 15 with NoSuchProviderException

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

the class JavaMailer method sendMessage.

/**
     * Send message.
     *
     * @param message a {@link javax.mail.Message} object.
     * @throws org.opennms.javamail.JavaMailerException if any.
     */
public void sendMessage(Message message) throws JavaMailerException {
    Transport t = null;
    try {
        t = getSession().getTransport(getTransport());
        LOG.debug("for transport name '{}' got: {}@{}", getTransport(), t.getClass().getName(), Integer.toHexString(t.hashCode()));
        LoggingTransportListener listener = new LoggingTransportListener();
        t.addTransportListener(listener);
        if (t.getURLName().getProtocol().equals("mta")) {
            // JMTA throws an AuthenticationFailedException if we call connect()
            LOG.debug("transport is 'mta', not trying to connect()");
        } else if (isAuthenticate()) {
            LOG.debug("authenticating to {}", getMailHost());
            t.connect(getMailHost(), getSmtpPort(), getUser(), getPassword());
        } else {
            LOG.debug("not authenticating to {}", getMailHost());
            t.connect(getMailHost(), getSmtpPort(), null, null);
        }
        t.sendMessage(message, message.getAllRecipients());
        listener.assertAllMessagesDelivered();
    } catch (NoSuchProviderException e) {
        LOG.error("Couldn't get a transport: {}", e, e);
        throw new JavaMailerException("Couldn't get a transport: " + e, e);
    } catch (MessagingException e) {
        LOG.error("Java Mailer messaging exception: {}", e, e);
        throw new JavaMailerException("Java Mailer messaging exception: " + e, e);
    } finally {
        try {
            if (t != null && t.isConnected()) {
                t.close();
            }
        } catch (MessagingException e) {
            throw new JavaMailerException("Java Mailer messaging exception on transport close: " + e, e);
        }
    }
}
Also used : MessagingException(javax.mail.MessagingException) Transport(javax.mail.Transport) NoSuchProviderException(javax.mail.NoSuchProviderException)

Aggregations

NoSuchProviderException (javax.mail.NoSuchProviderException)16 Session (javax.mail.Session)10 MessagingException (javax.mail.MessagingException)9 Store (javax.mail.Store)7 Properties (java.util.Properties)6 Folder (javax.mail.Folder)5 Message (javax.mail.Message)5 Transport (javax.mail.Transport)5 UserAuth (org.opennms.netmgt.config.javamail.UserAuth)3 IOException (java.io.IOException)2 ReadmailHost (org.opennms.netmgt.config.javamail.ReadmailHost)2 ActionException (com.axway.ats.action.model.ActionException)1 RbvStorageException (com.axway.ats.rbv.model.RbvStorageException)1 POP3SSLStore (com.sun.mail.pop3.POP3SSLStore)1 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1