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

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

Introduction

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

Prototype

public int getReplyCode() 

Source Link

Document

Returns the integer value of the reply code of the last SMTP reply.

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  a2s  .  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);// ww  w .  j  a  v  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);
    }
}