Search in sources :

Example 6 with AuthenticationFailedException

use of javax.mail.AuthenticationFailedException in project zm-mailbox by Zimbra.

the class SmtpTransport method protocolConnect.

@Override
protected boolean protocolConnect(String host, int port, String user, String passwd) throws MessagingException {
    boolean auth = PropUtil.getBooleanSessionProperty(session, "mail." + protocol + ".auth", false);
    String authMechanism = session.getProperty("mail." + protocol + ".sasl.mechanisms");
    if (authMechanism != null && SaslAuthenticator.XOAUTH2.equalsIgnoreCase(authMechanism)) {
        passwd = session.getProperty("mail." + protocol + ".sasl.mechanisms.oauth2.oauthToken");
    }
    if (auth && (user == null || passwd == null)) {
        return false;
    }
    if (port < 0) {
        port = PropUtil.getIntSessionProperty(session, "mail." + protocol + ".port", ssl ? SmtpConfig.DEFAULT_SSL_PORT : SmtpConfig.DEFAULT_PORT);
    }
    if (Strings.isNullOrEmpty(host)) {
        host = "localhost";
    }
    SmtpConfig config = new SmtpConfig();
    config.setHost(host);
    config.setPort(port);
    config.setDomain(session.getProperty("mail." + protocol + ".localhost"));
    config.setSecurity(ssl ? SmtpConfig.Security.SSL : PropUtil.getBooleanSessionProperty(session, "mail.smtp.starttls.enable", false) ? SmtpConfig.Security.TLS_IF_AVAILABLE : SmtpConfig.Security.NONE);
    config.setAllowPartialSend(PropUtil.getBooleanSessionProperty(session, "mail." + protocol + ".sendpartial", false));
    config.setConnectTimeout(PropUtil.getIntSessionProperty(session, "mail." + protocol + ".connectiontimeout", 0) / // msec to sec
    1000);
    config.setReadTimeout(PropUtil.getIntSessionProperty(session, "mail." + protocol + ".timeout", 0) / // msec to sec
    1000);
    config.setDsn(session.getProperty("mail." + protocol + ".dsn.notify"));
    Properties props = session.getProperties();
    Object socketFactory = props.get("mail." + protocol + ".socketFactory");
    if (socketFactory instanceof SocketFactory) {
        config.setSocketFactory((SocketFactory) socketFactory);
    }
    Object sslSocketFactory = props.get("mail." + protocol + ".ssl.socketFactory");
    if (sslSocketFactory instanceof SSLSocketFactory) {
        config.setSSLSocketFactory((SSLSocketFactory) sslSocketFactory);
    }
    if (authMechanism != null && SaslAuthenticator.XOAUTH2.equalsIgnoreCase(authMechanism)) {
        config.setMechanism(SaslAuthenticator.XOAUTH2);
        HashMap<String, String> map = new HashMap<String, String>();
        JMSession.addOAuth2Properties(passwd, map, config.getProtocol());
        config.setSaslProperties(map);
    }
    if (user != null && passwd != null) {
        config.setAuthenticationId(user);
    }
    connection = new SmtpConnection(config);
    try {
        connection.connect();
    } catch (IOException e) {
        throw new MessagingException(e.getMessage(), e);
    }
    if (auth || (user != null && passwd != null)) {
        try {
            connection.authenticate(passwd);
        } catch (LoginException e) {
            throw new AuthenticationFailedException(e.getMessage());
        } catch (IOException e) {
            throw new AuthenticationFailedException(e.getMessage());
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SocketFactory(javax.net.SocketFactory) IOException(java.io.IOException) Properties(java.util.Properties) LoginException(javax.security.auth.login.LoginException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Aggregations

AuthenticationFailedException (javax.mail.AuthenticationFailedException)6 MessagingException (javax.mail.MessagingException)5 IOException (java.io.IOException)2 MailUtils (org.gluu.oxtrust.util.MailUtils)2 SMTPMessage (com.sun.mail.smtp.SMTPMessage)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Properties (java.util.Properties)1 Address (javax.mail.Address)1 NoSuchProviderException (javax.mail.NoSuchProviderException)1 SendFailedException (javax.mail.SendFailedException)1 Session (javax.mail.Session)1 Transport (javax.mail.Transport)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1 SocketFactory (javax.net.SocketFactory)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 LoginException (javax.security.auth.login.LoginException)1