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

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

Introduction

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

Prototype

public String getReplyString() 

Source Link

Document

Returns the entire text of the last SMTP server response exactly as it was received, including all end of line markers in NETASCII format.

Usage

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);//w  ww  . ja v  a  2s .c  om
    }

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

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  ww .  j  a v  a  2s  .  c  om
    }

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