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();
}
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();
}
}
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();
}
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());
}
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();
}
Aggregations