Example usage for org.apache.commons.net.smtp AuthSMTPClient isConnected

List of usage examples for org.apache.commons.net.smtp AuthSMTPClient isConnected

Introduction

In this page you can find the example usage for org.apache.commons.net.smtp AuthSMTPClient isConnected.

Prototype

public boolean isConnected() 

Source Link

Document

Returns true if the client is currently connected to a server.

Usage

From source file:com.google.gerrit.server.mail.SmtpEmailSender.java

private SMTPClient open() throws EmailException {
    final AuthSMTPClient client = new AuthSMTPClient("UTF-8");

    if (smtpEncryption == Encryption.SSL) {
        client.enableSSL(sslVerify);/*from w  w  w .  j ava2  s . c  o  m*/
    }

    try {
        client.connect(smtpHost, smtpPort);
        if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
            throw new EmailException("SMTP server rejected connection");
        }
        if (!client.login()) {
            String e = client.getReplyString();
            throw new EmailException("SMTP server rejected HELO/EHLO greeting: " + e);
        }

        if (smtpEncryption == Encryption.TLS) {
            if (!client.startTLS(smtpHost, smtpPort, sslVerify)) {
                throw new EmailException("SMTP server does not support TLS");
            }
            if (!client.login()) {
                String e = client.getReplyString();
                throw new EmailException("SMTP server rejected login: " + e);
            }
        }

        if (smtpUser != null && !client.auth(smtpUser, smtpPass)) {
            String e = client.getReplyString();
            throw new EmailException("SMTP server rejected auth: " + e);
        }
    } catch (IOException e) {
        if (client.isConnected()) {
            try {
                client.disconnect();
            } catch (IOException e2) {
            }
        }
        throw new EmailException(e.getMessage(), e);
    } catch (EmailException e) {
        if (client.isConnected()) {
            try {
                client.disconnect();
            } catch (IOException e2) {
            }
        }
        throw e;
    }
    return client;
}

From source file:com.google.gerrit.server.mail.send.SmtpEmailSender.java

private SMTPClient open() throws EmailException {
    final AuthSMTPClient client = new AuthSMTPClient(UTF_8.name());

    if (smtpEncryption == Encryption.SSL) {
        client.enableSSL(sslVerify);//from   ww w . jav  a2  s  . c  o  m
    }

    client.setConnectTimeout(connectTimeout);
    try {
        client.connect(smtpHost, smtpPort);
        int replyCode = client.getReplyCode();
        String replyString = client.getReplyString();
        if (!SMTPReply.isPositiveCompletion(replyCode)) {
            throw new EmailException(
                    String.format("SMTP server rejected connection: %d: %s", replyCode, replyString));
        }
        if (!client.login()) {
            throw new EmailException("SMTP server rejected HELO/EHLO greeting: " + replyString);
        }

        if (smtpEncryption == Encryption.TLS) {
            if (!client.startTLS(smtpHost, smtpPort, sslVerify)) {
                throw new EmailException("SMTP server does not support TLS");
            }
            if (!client.login()) {
                throw new EmailException("SMTP server rejected login: " + replyString);
            }
        }

        if (smtpUser != null && !client.auth(smtpUser, smtpPass)) {
            throw new EmailException("SMTP server rejected auth: " + replyString);
        }
        return client;
    } catch (IOException | EmailException e) {
        if (client.isConnected()) {
            try {
                client.disconnect();
            } catch (IOException e2) {
                //Ignored
            }
        }
        if (e instanceof EmailException) {
            throw (EmailException) e;
        }
        throw new EmailException(e.getMessage(), e);
    }
}