Search in sources :

Example 1 with SMTPClient

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

the class DSNMailSender_sendMailTest method testSendMail_nullWriter_assertException.

@Test
public void testSendMail_nullWriter_assertException() 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(null);
    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);
    boolean exceptionOccurred = false;
    try {
        sender.sendMail(exchange);
    } catch (IOException e) {
        exceptionOccurred = true;
    }
    assertTrue(exceptionOccurred);
    verify(client, times(1)).sendMessageData();
    verify(client, never()).completePendingCommand();
}
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) MimeMessage(javax.mail.internet.MimeMessage) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with SMTPClient

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

the class DSNMailSender method sendMail.

/**
	 * Processor handler method.  This method sends the message to SMTP gateway specified in the gateway URL.
	 * The SMTP to and from headers and taken from the actual message.
	 * @param exchange The exchange that holds the message.
	 * @throws Exception
	 */
@Handler
public void sendMail(Exchange exchange) throws Exception {
    if (gatewayHost == null || gatewayHost.isEmpty())
        throw new IllegalStateException("Gateway URL is null or empty");
    // simple SMTP converation
    if (clientFactory == null)
        throw new IllegalStateException("SMTP client cannot be null");
    if (exchange.getIn() == null || exchange.getIn().getBody() == null)
        return;
    final MimeMessage dsnMessage = (MimeMessage) exchange.getIn().getBody();
    final String recpList = dsnMessage.getHeader(MailStandard.Headers.To, ",");
    final String sender = dsnMessage.getHeader(MailStandard.Headers.From, ",");
    SMTPClient client = clientFactory.createInstance();
    client.connect(gatewayHost, gatewayPort);
    client.helo(localhost);
    if (!client.setSender(sender))
        throw new IOException("Failed to set sender.");
    final String[] recips = recpList.split(",");
    for (String recip : recips) {
        if (!client.addRecipient(recip))
            throw new IOException("Failed to set recipient " + recip);
    }
    final Writer writer = client.sendMessageData();
    if (writer == null)
        throw new IOException("Failed to get data body writer.");
    final ByteArrayOutputStream writerStream = new ByteArrayOutputStream();
    try {
        dsnMessage.writeTo(writerStream);
        IOUtils.write(writerStream.toByteArray(), writer);
        writer.close();
        client.completePendingCommand();
        if (client.getReplyCode() != SMTP_OK)
            throw new IOException("Failed complete data command with error code " + client.getReplyCode());
    } finally {
        IOUtils.closeQuietly(writerStream);
        IOUtils.closeQuietly(writer);
        client.quit();
        client.disconnect();
    }
}
Also used : SMTPClient(org.apache.commons.net.smtp.SMTPClient) MimeMessage(javax.mail.internet.MimeMessage) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Writer(java.io.Writer) Handler(org.apache.camel.Handler)

Example 3 with SMTPClient

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

the class DSNMailSender_sendMailTest method testSendMail_failureStatusCode_assertException.

@Test
public void testSendMail_failureStatusCode_assertException() 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(300);
    final SMTPClientFactory factory = createFactory(client);
    DSNMailSender sender = new DSNMailSender("smtp://localhost", factory);
    Exchange exchange = new DefaultExchange(mock(CamelContext.class));
    exchange.getIn().setBody(msg);
    boolean exceptionOccurred = false;
    try {
        sender.sendMail(exchange);
    } catch (IOException e) {
        exceptionOccurred = true;
    }
    assertTrue(exceptionOccurred);
    verify(client, times(2)).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) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with SMTPClient

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

the class DSNMailSender_sendMailTest method testSendMail_setSenderError_assertException.

@Test
public void testSendMail_setSenderError_assertException() throws Exception {
    MimeMessage msg = TestUtils.readMimeMessageFromFile("MessageWithAttachment.txt");
    final SMTPClient client = mock(SMTPClient.class);
    when(client.setSender((String) any())).thenReturn(false);
    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);
    boolean exceptionOccurred = false;
    try {
        sender.sendMail(exchange);
    } catch (IOException e) {
        exceptionOccurred = true;
    }
    assertTrue(exceptionOccurred);
    verify(client, times(1)).setSender((String) any());
    verify(client, never()).addRecipient((String) any());
}
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) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with SMTPClient

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

the class DSNMailSender_sendMailTest method testSendMail_setAddRecip_assertException.

@Test
public void testSendMail_setAddRecip_assertException() 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(false);
    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);
    boolean exceptionOccurred = false;
    try {
        sender.sendMail(exchange);
    } catch (IOException e) {
        exceptionOccurred = true;
    }
    assertTrue(exceptionOccurred);
    verify(client, times(1)).addRecipient((String) any());
    verify(client, never()).sendMessageData();
}
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) IOException(java.io.IOException) Test(org.junit.Test)

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