@Override public void sendMessage(final Message msg, final Address[] addresses) throws MessagingException { for (final Address a : addresses) { final MockMailbox mailbox = MockMailbox.get(a); if (mailbox.getInbox().isSimulateError()) { notifyTransportListeners(TransportEvent.MESSAGE_NOT_DELIVERED, new Address[0], addresses, new Address[0], msg); throw new MessagingException("Simulated error sending message to " + a); } mailbox.getInbox().add(new MimeMessage((MimeMessage) msg)); notifyTransportListeners(TransportEvent.MESSAGE_DELIVERED, addresses, new Address[0], new Address[0], msg); } }
private void logEvent(String message, TransportEvent event) { if (event.getInvalidAddresses() != null && event.getInvalidAddresses().length > 0) { m_invalidAddresses.addAll(Arrays.asList(event.getInvalidAddresses())); m_category.error(message + ": invalid addresses: " + StringUtils.arrayToDelimitedString(event.getInvalidAddresses(), ", ")); } if (event.getValidSentAddresses() != null && event.getValidSentAddresses().length > 0) { m_validSentAddresses.addAll(Arrays.asList(event.getValidSentAddresses())); m_category.debug(message + ": valid sent addresses: " + StringUtils.arrayToDelimitedString(event.getValidSentAddresses(), ", ")); } if (event.getValidUnsentAddresses() != null && event.getValidUnsentAddresses().length > 0) { m_validUnsentAddresses.addAll(Arrays.asList(event.getValidUnsentAddresses())); m_category.error(message + ": valid unsent addresses: " + StringUtils.arrayToDelimitedString(event.getValidUnsentAddresses(), ", ")); } }
@Override public void sendMessage( Message arg0, Address[] arg1 ) throws MessagingException { log.info("Fake message sending"); if (deliveryState == DELIVERY_STATE.DELIVERED) { notifyTransportListeners(TransportEvent.MESSAGE_DELIVERED, null, null, null, null); } else if (deliveryState == DELIVERY_STATE.PARTIALLY_DELIVERED) { notifyTransportListeners(TransportEvent.MESSAGE_PARTIALLY_DELIVERED, null, null, null, null); } else if (deliveryState == DELIVERY_STATE.ERROR_DELIVERING) { notifyTransportListeners(TransportEvent.MESSAGE_NOT_DELIVERED, null, null, null, null); } }
/** * {@inheritDoc} This acks the notification by invoking {@link NotificationContext#ack(String)} */ public void messageDelivered(TransportEvent event) { LOG.debug("Got messageDelivered event {}", event); String notificationId = msgNotificationMap.remove(event.getMessage()); if (notificationId != null) { ctx.ack(notificationId); } }
@Override public void sendMessage(Message message, Address[] addresses) throws MessagingException { validateAndPrepare(message, addresses); FileOutputStream fw = null; File file = null; try { String baseName = Long.toString(System.currentTimeMillis(), 32); file = createMessageFile(baseName); fw = new FileOutputStream(file); writeMessage(message, fw); notifyTransportListeners(TransportEvent.MESSAGE_DELIVERED, message.getAllRecipients(), ADDRESSES_EMPTY, ADDRESSES_EMPTY, message); } catch (IOException e) { notifyTransportListeners(TransportEvent.MESSAGE_NOT_DELIVERED, ADDRESSES_EMPTY, message.getAllRecipients(), ADDRESSES_EMPTY, message); if (file == null) { throw new MessagingException("Cennot create message file", e); } else { throw new MessagingException("Failed to write file " + file.getAbsolutePath(), e); } } finally { if (fw != null) { try { fw.close(); } catch (IOException ex) { Logger.getLogger(getClass().getName()).log(Level.WARNING, "Cannot close " + fw, ex); } } } }
@Test public void transportNOPTest() throws Exception { TransportListener transportListener = Mockito.mock(TransportListener.class); Message message = generateMessage(); Transport transport = session.getTransport("nop"); transport.addTransportListener(transportListener); transport.sendMessage(message, toAddress); waitForListeners(); ArgumentCaptor<TransportEvent> transportEventArgumentCaptor = ArgumentCaptor.forClass(TransportEvent.class); verify(transportListener).messageDelivered(transportEventArgumentCaptor.capture()); TransportEvent event = transportEventArgumentCaptor.getValue(); assertEquals(message, event.getMessage()); assertEquals(TransportEvent.MESSAGE_DELIVERED, event.getType()); assertArrayEquals(toAddress, event.getValidSentAddresses()); }
@Test(expected = MessagingException.class) public void testNoFromNull() throws Exception { try { message.setFrom((String) null); transport.validateAndPrepare(message, new Address[]{}); } finally { waitForListeners(); verify(transportListener).messageNotDelivered(any(TransportEvent.class)); } }
public void messageDelivered( TransportEvent e ) { processResult(DELIVERY_STATE.DELIVERED); }
public void messageNotDelivered( TransportEvent e ) { processResult(DELIVERY_STATE.ERROR_DELIVERING); }
public void messagePartiallyDelivered( TransportEvent e ) { processResult(DELIVERY_STATE.ERROR_DELIVERING); }
/** * {@inheritDoc} */ @Override public void messageDelivered(TransportEvent e) { log.debug("Message delivered"); finish(); }
/** * {@inheritDoc} */ @Override public void messageNotDelivered(TransportEvent e) { log.debug("Message not delivered"); finish(); }
/** * {@inheritDoc} */ @Override public void messagePartiallyDelivered(TransportEvent e) { log.debug("Message partially delivered"); finish(); }
/** * {@inheritDoc} This fails the notification by invoking {@link NotificationContext#fail(String)} */ public void messageNotDelivered(TransportEvent event) { LOG.debug("Got messageNotDelivered event {}", event); handleFail(event); }
private void handleFail(TransportEvent event) { String notificationId = msgNotificationMap.remove(event.getMessage()); if (notificationId != null) { ctx.fail(notificationId); } }
@Override public void sendMessage(Message msg, Address[] addresses) throws MessagingException { validateAndPrepare(msg, addresses); logger.info("Message {subject=" + msg.getSubject() + ", to=" + Arrays.asList(addresses) + "}"); notifyTransportListeners(TransportEvent.MESSAGE_DELIVERED, msg.getAllRecipients(), ADDRESSES_EMPTY, ADDRESSES_EMPTY, msg); }
private void fail(Message message, Address[] addresses, String msg) throws MessagingException { Address[] empty = new Address[0]; notifyTransportListeners(TransportEvent.MESSAGE_NOT_DELIVERED, empty, addresses, empty, message); throw new MessagingException(msg); }
private void fail(Message message, Address[] addresses, String msg) throws MessagingException { notifyTransportListeners(TransportEvent.MESSAGE_NOT_DELIVERED, ADDRESSES_EMPTY, addresses, ADDRESSES_EMPTY, message); throw new MessagingException(msg); }
public void messageDelivered(TransportEvent event) { logEvent("message delivered", event); }
public void messageNotDelivered(TransportEvent event) { logEvent("message not delivered", event); }
public void messagePartiallyDelivered(TransportEvent event) { logEvent("message partially delivered", event); }
/** * {@inheritDoc} */ @Override public void messageDelivered(TransportEvent e) { logger.debug("Message delivered"); finish(); }
/** * {@inheritDoc} */ @Override public void messageNotDelivered(TransportEvent e) { logger.debug("Message not delivered"); finish(); }
/** * {@inheritDoc} */ @Override public void messagePartiallyDelivered(TransportEvent e) { logger.debug("Message partially delivered"); finish(); }