Example usage for javax.mail AuthenticationFailedException AuthenticationFailedException

List of usage examples for javax.mail AuthenticationFailedException AuthenticationFailedException

Introduction

In this page you can find the example usage for javax.mail AuthenticationFailedException AuthenticationFailedException.

Prototype

public AuthenticationFailedException(String message) 

Source Link

Document

Constructs an AuthenticationFailedException with the specified detail message.

Usage

From source file:org.sourceforge.net.javamail4ews.util.Util.java

public static ExchangeService getExchangeService(String host, int port, String user, String password,
        Session pSession) throws MessagingException {
    if (user == null) {
        return null;
    }/*w ww . ja va  2  s.  c om*/
    if (password == null) {
        return null;
    }

    String version = getConfiguration(pSession).getString("org.sourceforge.net.javamail4ews.ExchangeVersion",
            "");
    ExchangeVersion serverVersion = null;
    if (!version.isEmpty()) {
        try {
            serverVersion = Enum.valueOf(ExchangeVersion.class, version);
        } catch (IllegalArgumentException e) {
            logger.info("Unknown version for exchange server: '" + version
                    + "' using default : no version specified");
        }
    }
    boolean enableTrace = getConfiguration(pSession)
            .getBoolean("org.sourceforge.net.javamail4ews.util.Util.EnableServiceTrace");
    ExchangeService service = null;
    if (serverVersion != null) {
        service = new ExchangeService(serverVersion);
    } else {
        service = new ExchangeService();
    }
    Integer connectionTimeout = getConnectionTimeout(pSession);
    Integer protocolTimeout = getProtocolTimeout(pSession);
    if (connectionTimeout != null) {
        logger.debug("setting timeout to {} using connection timeout value", connectionTimeout);
        service.setTimeout(connectionTimeout.intValue());
    }
    if (protocolTimeout != null) {
        logger.debug("setting protocol timeout to {} is ignored", protocolTimeout);
    }
    service.setTraceEnabled(enableTrace);

    ExchangeCredentials credentials = new WebCredentials(user, password);
    service.setCredentials(credentials);

    try {
        service.setUrl(new URI(host));
    } catch (URISyntaxException e) {
        throw new MessagingException(e.getMessage(), e);
    }

    try {
        //Bind to check if connection parameters are valid
        if (getConfiguration(pSession)
                .getBoolean("org.sourceforge.net.javamail4ews.util.Util.VerifyConnectionOnConnect")) {
            logger.debug("Connection settings : trying to verify them");
            Folder.bind(service, WellKnownFolderName.Inbox);
            logger.info("Connection settings verified.");
        } else {
            logger.info("Connection settings not verified yet.");
        }
        return service;
    } catch (Exception e) {
        Throwable cause = e.getCause();
        if (cause != null) {
            if (cause instanceof ConnectException) {
                Exception nested = (ConnectException) cause;
                throw new MessagingException(nested.getMessage(), nested);
            }
        }
        throw new AuthenticationFailedException(e.getMessage());
    }
}