Search in sources :

Example 71 with MimeMessage

use of javax.mail.internet.MimeMessage 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 72 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class DSNMessageGenerator_generateDSNFailureMessageTest method testGenerateDSNFailureMessage_assertMessageCreated_noCopiedSubject.

@Test
public void testGenerateDSNFailureMessage_assertMessageCreated_noCopiedSubject() throws Exception {
    Exchange exchange = new DefaultExchange(mock(CamelContext.class));
    String message = TestUtils.readMessageFromFile("MessageWithAttachment.txt");
    DefaultTxDetailParser parser = new DefaultTxDetailParser();
    InputStream str = IOUtils.toInputStream(message);
    try {
        // send original message
        final MimeMessage mimeMessage = new MimeMessage(null, str);
        mimeMessage.setHeader(MailStandard.Headers.Subject, "");
        // change the message id
        mimeMessage.saveChanges();
        Map<String, TxDetail> details = parser.getMessageDetails(mimeMessage);
        Tx originalMessage = new Tx(TxMessageType.IMF, details);
        Collection<Tx> txs = new ArrayList<Tx>();
        txs.add(originalMessage);
        DSNMessageGenerator generator = createGenerator();
        generator.generateDSNFailureMessage(txs, exchange);
        MimeMessage dsnMessage = (MimeMessage) exchange.getIn().getBody();
        assertNotNull(dsnMessage);
        // check the subject
        String newSubject = MailStandard.getHeader(dsnMessage, MailStandard.Headers.Subject);
        assertEquals("Not Delivered:", newSubject);
    } finally {
        IOUtils.closeQuietly(str);
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) CamelContext(org.apache.camel.CamelContext) Tx(org.nhindirect.common.tx.model.Tx) InputStream(java.io.InputStream) TxDetail(org.nhindirect.common.tx.model.TxDetail) ArrayList(java.util.ArrayList) DefaultTxDetailParser(org.nhindirect.common.tx.impl.DefaultTxDetailParser) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test)

Example 73 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class TestNonCompletedToDSNGeneratorMonitorRoute method testNonCompleted_multipleRecipeints_singleDSNSingleProcessAndOneIncomplete_assertDSNGeneratedAndValidTimedout.

@Test
public void testNonCompleted_multipleRecipeints_singleDSNSingleProcessAndOneIncomplete_assertDSNGeneratedAndValidTimedout() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    // send original message
    final String originalMessageId = UUID.randomUUID().toString();
    Tx originalMessage = TestUtils.makeMessage(TxMessageType.IMF, originalMessageId, "", "gm2552@cerner.com", "gm2552@direct.securehealthemail.com,ah4626@direct.securehealthemail.com,gm2552@test.com", "");
    template.sendBody("direct:start", originalMessage);
    // now send a DSN failure for only recipient
    Tx dsntxMessage = TestUtils.makeMessage(TxMessageType.DSN, UUID.randomUUID().toString(), originalMessageId, "gm2552@direct.securehealthemail.com", "gm2552@cerner.com", "gm2552@direct.securehealthemail.com", DSNStandard.DSNAction.FAILED.toString(), "");
    template.sendBody("direct:start", dsntxMessage);
    // now send an MDN success for only recipient
    Tx mdnMessage = TestUtils.makeMessage(TxMessageType.MDN, UUID.randomUUID().toString(), originalMessageId, "ah4626@direct.securehealthemail.com", "gm2552@cerner.com", "ah4626@direct.securehealthemail.com", "", MDNStandard.Disposition_Processed);
    template.sendBody("direct:start", mdnMessage);
    // single MDN sent... messages should timeout after 2 seconds
    // sleep 3 seconds to make sure it completes
    Thread.sleep(3000);
    List<Exchange> exchanges = mock.getReceivedExchanges();
    assertEquals(1, exchanges.size());
    MimeMessage dsnMessage = (MimeMessage) exchanges.get(0).getIn().getBody();
    ByteArrayOutputStream oStr = new ByteArrayOutputStream();
    dsnMessage.writeTo(oStr);
    String str = new String(oStr.toByteArray());
    assertFalse(str.contains("ah4626@direct.securehealthemail.com"));
    assertFalse(str.contains("gm2552@direct.securehealthemail.com"));
    assertTrue(str.contains("gm2552@test.com"));
}
Also used : Exchange(org.apache.camel.Exchange) Tx(org.nhindirect.common.tx.model.Tx) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 74 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class TestNonCompletedToDSNGeneratorMonitorRoute method testNonCompleted_multipleRecipeints_singleDSNAndOneIncomplete_assertDSNGeneratedAndValidTimedout.

@Test
public void testNonCompleted_multipleRecipeints_singleDSNAndOneIncomplete_assertDSNGeneratedAndValidTimedout() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    // send original message
    final String originalMessageId = UUID.randomUUID().toString();
    Tx originalMessage = TestUtils.makeMessage(TxMessageType.IMF, originalMessageId, "", "gm2552@cerner.com", "gm2552@direct.securehealthemail.com,ah4626@direct.securehealthemail.com", "");
    template.sendBody("direct:start", originalMessage);
    // now send a DSN failure for only recipient
    Tx dsntxMessage = TestUtils.makeMessage(TxMessageType.DSN, UUID.randomUUID().toString(), originalMessageId, "gm2552@direct.securehealthemail.com", "gm2552@cerner.com", "gm2552@direct.securehealthemail.com", DSNStandard.DSNAction.FAILED.toString(), "");
    template.sendBody("direct:start", dsntxMessage);
    // single MDN sent... messages should timeout after 2 seconds
    // sleep 3 seconds to make sure it completes
    Thread.sleep(3000);
    List<Exchange> exchanges = mock.getReceivedExchanges();
    assertEquals(1, exchanges.size());
    MimeMessage dsnMessage = (MimeMessage) exchanges.get(0).getIn().getBody();
    ByteArrayOutputStream oStr = new ByteArrayOutputStream();
    dsnMessage.writeTo(oStr);
    String str = new String(oStr.toByteArray());
    assertTrue(str.contains("ah4626@direct.securehealthemail.com"));
    assertFalse(str.contains("gm2552@direct.securehealthemail.com"));
}
Also used : Exchange(org.apache.camel.Exchange) Tx(org.nhindirect.common.tx.model.Tx) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 75 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class DSNMessageGenerator method generateDSNFailureMessage.

/**
	 * Generates the DSN message a replacing the existing exchange in body with the DSN message as a MimeMessage object.
	 * @param txs Collection of correlated Tx objects.
	 * @param ex The message exchange.
	 * @throws Exception
	 */
@Handler
public void generateDSNFailureMessage(Collection<Tx> txs, Exchange ex) throws Exception {
    // change the inbound message body to null
    ex.getIn().setBody(null);
    // get the message that is being tracked so we can generate an error message for it
    Tx messageToTrack = AbstractCompletionCondition.getMessageToTrack(txs);
    if (messageToTrack != null) {
        // make sure we have incomplete recipients
        final Collection<String> incompleteRecips = conditionChecker.getIncompleteRecipients(txs);
        if (incompleteRecips != null && !incompleteRecips.isEmpty()) {
            InternetAddress originalSender = null;
            String originalSubject = "";
            InternetAddress postmaster = null;
            String originalMessageId = "";
            Enumeration<Header> fullMessageHeaders = null;
            final List<DSNRecipientHeaders> recipientDSNHeaders = new ArrayList<DSNRecipientHeaders>();
            final List<Address> failedRecipAddresses = new ArrayList<Address>();
            final TxDetail sender = messageToTrack.getDetail(TxDetailType.FROM);
            if (sender != null) {
                originalSender = new InternetAddress(sender.getDetailValue());
                postmaster = new InternetAddress(postmasterMailbox + "@" + getAddressDomain(originalSender));
            }
            final TxDetail subject = messageToTrack.getDetail(TxDetailType.SUBJECT);
            if (subject != null)
                originalSubject = subject.getDetailValue();
            for (String incompleteRecip : incompleteRecips) {
                final Address failedRecipAddress = new InternetAddress(incompleteRecip);
                DSNRecipientHeaders dsnRecipHeaders = new DSNRecipientHeaders(DSNAction.FAILED, DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.UNDEFINED_STATUS), failedRecipAddress);
                recipientDSNHeaders.add(dsnRecipHeaders);
                failedRecipAddresses.add(failedRecipAddress);
            }
            ///CLOVER:OFF
            final TxDetail origMessId = messageToTrack.getDetail(TxDetailType.MSG_ID);
            if (origMessId != null)
                originalMessageId = origMessId.getDetailValue();
            ///CLOVER:ON
            final DSNMessageHeaders messageDSNHeaders = new DSNMessageHeaders(reportingMta, originalMessageId, MtaNameType.DNS);
            final TxDetail fullHeaders = messageToTrack.getDetail(TxDetailType.MSG_FULL_HEADERS);
            if (fullHeaders != null)
                fullMessageHeaders = this.convertStringToHeaders(fullHeaders.getDetailValue());
            final MimeBodyPart textBodyPart = textGenerator.generate(originalSender, failedRecipAddresses, fullMessageHeaders);
            final MimeMessage dnsMessage = generator.createDSNMessage(originalSender, originalSubject, postmaster, recipientDSNHeaders, messageDSNHeaders, textBodyPart);
            ex.getIn().setBody(dnsMessage);
        }
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Tx(org.nhindirect.common.tx.model.Tx) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) DSNRecipientHeaders(org.nhindirect.common.mail.dsn.DSNRecipientHeaders) ArrayList(java.util.ArrayList) TxDetail(org.nhindirect.common.tx.model.TxDetail) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) DSNMessageHeaders(org.nhindirect.common.mail.dsn.DSNMessageHeaders) MimeBodyPart(javax.mail.internet.MimeBodyPart) Handler(org.apache.camel.Handler)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)1146 Test (org.junit.Test)374 InternetAddress (javax.mail.internet.InternetAddress)334 MessagingException (javax.mail.MessagingException)299 Session (javax.mail.Session)222 Properties (java.util.Properties)219 MimeMultipart (javax.mail.internet.MimeMultipart)208 MimeBodyPart (javax.mail.internet.MimeBodyPart)178 Date (java.util.Date)153 IOException (java.io.IOException)137 Message (javax.mail.Message)120 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)107 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)97 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)83 InputStream (java.io.InputStream)82 ArrayList (java.util.ArrayList)81 Multipart (javax.mail.Multipart)75 DataHandler (javax.activation.DataHandler)73 ByteArrayOutputStream (java.io.ByteArrayOutputStream)72 BodyPart (javax.mail.BodyPart)70