List of usage examples for org.apache.commons.net.smtp AuthenticatingSMTPClient getReplyCode
public int getReplyCode()
From source file:net.metanotion.emailqueue.SmtpSender.java
@Override public Connection getConnection() { AuthenticatingSMTPClient client = null; try {/*from w ww. j av a2 s. c o m*/ 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); } }
From source file:com.ironiacorp.email.CommonsNetEmailDispatcher.java
@Override public void sendEmails(Email... emails) { AuthenticatingSMTPClient client = null; boolean result = false; int port = connectionType.port; if (serverPort != 0) { port = serverPort;//w w w.ja v a 2 s .com } try { client = new AuthenticatingSMTPClient(); client.connect(serverName, port); if (connectionType == EmailServerConnectionType.SSL || connectionType == EmailServerConnectionType.TLS) { result = client.execTLS(); if (result == false) { throw new IllegalArgumentException("Cannot start secure connection"); } } if (authenticationRequired) { result = client.auth(AUTH_METHOD.PLAIN, username, password); if (result == false) { throw new IllegalArgumentException("Incorrect username or password"); } } // After connection attempt, you should check the reply code to verify success. int reply = client.getReplyCode(); if (!SMTPReply.isPositiveCompletion(reply)) { client.disconnect(); } for (Email email : emails) { SimpleSMTPHeader header; Iterator<Recipient> i = email.getRecipients().iterator(); Writer writer = client.sendMessageData(); String to = null; while (i.hasNext()) { Recipient recipient = i.next(); if (to == null && email.getVisibility(recipient) == Visibility.TO) { to = recipient.getEmail(); } else { client.addRecipient(recipient.getEmail()); } } header = new SimpleSMTPHeader(email.getSender().getEmail(), null, email.getSubject()); writer.write(header.toString()); result = client.completePendingCommand(); if (result == false) { throw new IllegalArgumentException("Could not send the email"); } client.reset(); } } catch (Exception e) { throw new UnsupportedOperationException("Could not send the email", e); } finally { if (client != null && client.isConnected()) { try { client.disconnect(); } catch (IOException e) { } } } }