Example usage for org.apache.commons.net.smtp AuthenticatingSMTPClient login

List of usage examples for org.apache.commons.net.smtp AuthenticatingSMTPClient login

Introduction

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

Prototype

public boolean login(String hostname) throws IOException 

Source Link

Document

Login to the SMTP server by sending the HELO command with the given hostname as an argument.

Usage

From source file:net.metanotion.emailqueue.SmtpSender.java

@Override
public Connection getConnection() {
    AuthenticatingSMTPClient client = null;
    try {/* www  .  j a  v a 2 s . c om*/
        client = new AuthenticatingSMTPClient();
        client.addProtocolCommandListener(new ProtocolCommandListener() {
            @Override
            public void protocolCommandSent(final ProtocolCommandEvent event) {
                logger.trace(COMMAND_LOG_FORMAT, event.getCommand(), event.getMessage());
            }

            @Override
            public void protocolReplyReceived(final ProtocolCommandEvent event) {
                logger.trace(COMMAND_LOG_FORMAT, event.getCommand(), event.getMessage());
            }
        });
        logger.trace("Connecting");
        client.connect(this.server, this.port);
        if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
            logger.error("Did not get positive completion.");
            throw new IOException("Could not complete SMTP connection.");
        }
        startTls(client);
        logger.trace("Authenticating");
        client.auth(AuthenticatingSMTPClient.AUTH_METHOD.PLAIN, username, password);
        logger.trace("Login");
        client.login(host);
        return new Connection(client);
    } catch (final IOException | NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException ex) {
        logger.debug("Error", ex);
        try {
            if (client != null) {
                client.disconnect();
            }
        } catch (final IOException ex2) {
        }
        throw new RuntimeException(ex);
    }
}