Search in sources :

Example 6 with SMTPClient

use of org.apache.commons.net.smtp.SMTPClient in project nhin-d by DirectProject.

the class DSNMailSender_sendMailTest method testSendMail_mailSent_noExceptions.

@Test
public void testSendMail_mailSent_noExceptions() throws Exception {
    MimeMessage msg = TestUtils.readMimeMessageFromFile("MessageWithAttachment.txt");
    final SMTPClient client = mock(SMTPClient.class);
    when(client.setSender((String) any())).thenReturn(true);
    when(client.addRecipient((String) any())).thenReturn(true);
    when(client.sendMessageData()).thenReturn(new StringWriter());
    when(client.getReplyCode()).thenReturn(250);
    final SMTPClientFactory factory = createFactory(client);
    DSNMailSender sender = new DSNMailSender("smtp://localhost", factory);
    Exchange exchange = new DefaultExchange(mock(CamelContext.class));
    exchange.getIn().setBody(msg);
    sender.sendMail(exchange);
    verify(client, times(1)).getReplyCode();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) CamelContext(org.apache.camel.CamelContext) SMTPClient(org.apache.commons.net.smtp.SMTPClient) StringWriter(java.io.StringWriter) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test)

Example 7 with SMTPClient

use of org.apache.commons.net.smtp.SMTPClient in project gerrit by GerritCodeReview.

the class SmtpEmailSender method send.

@Override
public void send(final Address from, Collection<Address> rcpt, final Map<String, EmailHeader> callerHeaders, String textBody, @Nullable String htmlBody) throws EmailException {
    if (!isEnabled()) {
        throw new EmailException("Sending email is disabled");
    }
    StringBuilder rejected = new StringBuilder();
    try {
        final SMTPClient client = open();
        try {
            if (!client.setSender(from.email())) {
                throw new EmailException("Server " + smtpHost + " rejected from address " + from.email());
            }
            /* Do not prevent the email from being sent to "good" users simply
         * because some users get rejected.  If not, a single rejected
         * project watcher could prevent email for most actions on a project
         * from being sent to any user!  Instead, queue up the errors, and
         * throw an exception after sending the email to get the rejected
         * error(s) logged.
         */
            for (Address addr : rcpt) {
                if (!client.addRecipient(addr.email())) {
                    String error = client.getReplyString();
                    rejected.append("Server ").append(smtpHost).append(" rejected recipient ").append(addr).append(": ").append(error);
                }
            }
            try (Writer messageDataWriter = client.sendMessageData()) {
                if (messageDataWriter == null) {
                    /* Include rejected recipient error messages here to not lose that
             * information. That piece of the puzzle is vital if zero recipients
             * are accepted and the server consequently rejects the DATA command.
             */
                    throw new EmailException(rejected.append("Server ").append(smtpHost).append(" rejected DATA command: ").append(client.getReplyString()).toString());
                }
                render(messageDataWriter, callerHeaders, textBody, htmlBody);
                if (!client.completePendingCommand()) {
                    throw new EmailException("Server " + smtpHost + " rejected message body: " + client.getReplyString());
                }
                client.logout();
                if (rejected.length() > 0) {
                    throw new EmailException(rejected.toString());
                }
            }
        } finally {
            client.disconnect();
        }
    } catch (IOException e) {
        throw new EmailException("Cannot send outgoing email", e);
    }
}
Also used : AuthSMTPClient(org.apache.commons.net.smtp.AuthSMTPClient) SMTPClient(org.apache.commons.net.smtp.SMTPClient) Address(com.google.gerrit.entities.Address) EmailException(com.google.gerrit.exceptions.EmailException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Aggregations

SMTPClient (org.apache.commons.net.smtp.SMTPClient)7 IOException (java.io.IOException)6 MimeMessage (javax.mail.internet.MimeMessage)6 CamelContext (org.apache.camel.CamelContext)5 Exchange (org.apache.camel.Exchange)5 DefaultExchange (org.apache.camel.impl.DefaultExchange)5 Test (org.junit.Test)5 StringWriter (java.io.StringWriter)4 Writer (java.io.Writer)2 Address (com.google.gerrit.entities.Address)1 EmailException (com.google.gerrit.exceptions.EmailException)1 BufferedWriter (java.io.BufferedWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Handler (org.apache.camel.Handler)1 AuthSMTPClient (org.apache.commons.net.smtp.AuthSMTPClient)1