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

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

Introduction

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

Prototype

@Override
public void disconnect() throws IOException 

Source Link

Document

Closes the connection to the SMTP server and sets to null some internal data so that the memory may be reclaimed by the garbage collector.

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  .  jav a2 s.  co 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);// ww  w . j a va 2  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);
    }
}