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;
}
Aggregations