javax.mail.event.TransportEvent Java Examples

The following examples show how to use javax.mail.event.TransportEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SmtpJmsTransportTest.java    From javamail with Apache License 2.0 7 votes vote down vote up
@Test
public void testDoNotCheckFormHeader() throws Exception {
    Properties properties = new Properties();
    properties.setProperty("mail.smtpjms.validateFrom", "false");
    SmtpJmsTransport transport = new SmtpJmsTransport(Session.getInstance(properties), new URLName("jms"));
    Message message = Mockito.mock(Message.class);
    TransportListener listener = Mockito.mock(TransportListener.class);
    Address[] to = new Address[]{ new InternetAddress("[email protected]") };
    transport.addTransportListener(listener);
    transport.sendMessage(message, to);
    waitForListeners();
    ArgumentCaptor<TransportEvent> transportEventArgumentCaptor = ArgumentCaptor.forClass(TransportEvent.class);
    verify(listener).messageDelivered(transportEventArgumentCaptor.capture());
    TransportEvent event = transportEventArgumentCaptor.getValue();
    assertEquals(message, event.getMessage());
    assertEquals(TransportEvent.MESSAGE_DELIVERED, event.getType());
    assertArrayEquals(to, event.getValidSentAddresses());
}
 
Example #2
Source File: TransportTest.java    From javamail with Apache License 2.0 6 votes vote down vote up
@Test
public void testListenerOnSuccess() throws Exception {
    Address[] to = new Address[] {new InternetAddress("[email protected]")};
    Message message = generateMessage();
    TransportListener transportListener = Mockito.mock(TransportListener.class);
    AbstractFileTransport transport = (AbstractFileTransport) session.getTransport("filemsg");
    transport.addTransportListener(transportListener);
    transport.sendMessage(message, to);
    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(to, event.getValidSentAddresses());
    verify(transportListener).messageDelivered(any(TransportEvent.class));
}
 
Example #3
Source File: MockTransport.java    From javamail-mock2 with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #4
Source File: SmtpJmsTransportTest.java    From javamail with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailOnJms() throws Exception {
    Message message = Mockito.mock(Message.class);
    Address[] to = new Address[] { new InternetAddress("[email protected]") };
    when(message.getFrom()).thenReturn(new Address[] { new InternetAddress("[email protected]") });
    doThrow(new JMSException("mock")).when(queueSender).send(any(javax.jms.Message.class));
    try {
        transport.sendMessage(message, to);
        fail();
    } catch (MessagingException ex) {
        waitForListeners();
        ArgumentCaptor<TransportEvent> transportEventArgumentCaptor = ArgumentCaptor.forClass(TransportEvent.class);
        verify(transportListener).messageNotDelivered(transportEventArgumentCaptor.capture());
        TransportEvent event = transportEventArgumentCaptor.getValue();
        assertEquals(message, event.getMessage());
        assertEquals(TransportEvent.MESSAGE_NOT_DELIVERED, event.getType());
        assertArrayEquals(new Address[0], event.getValidSentAddresses());
        assertArrayEquals(to, event.getValidUnsentAddresses());
    }
}
 
Example #5
Source File: SmtpJmsTransportTest.java    From javamail with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendWithoutFrom() throws Exception {
    Message message = Mockito.mock(Message.class);
    Address[] sendTo = new Address[] { new InternetAddress("[email protected]") };
    try {
        transport.sendMessage(message, sendTo);
        fail();
    } catch (MessagingException ex) {
        waitForListeners();
        ArgumentCaptor<TransportEvent> transportEventArgumentCaptor = ArgumentCaptor.forClass(TransportEvent.class);
        verify(transportListener, times(1)).messageNotDelivered(transportEventArgumentCaptor.capture());
        TransportEvent event = transportEventArgumentCaptor.getValue();
        assertEquals(message, event.getMessage());
        assertEquals(TransportEvent.MESSAGE_NOT_DELIVERED, event.getType());
        assertArrayEquals(new Address[0], event.getInvalidAddresses());
        assertArrayEquals(new Address[0], event.getValidSentAddresses());
        assertArrayEquals(sendTo, event.getValidUnsentAddresses());
    }
}
 
Example #6
Source File: TransportTest.java    From javamail with Apache License 2.0 6 votes vote down vote up
@Test
public void testListenerOnError() throws Exception {
    TransportListener transportListener = Mockito.mock(TransportListener.class);
    AbstractFileTransport transport = (AbstractFileTransport) session.getTransport("filemsg");
    transport.addTransportListener(transportListener);
    MimeMessage message = Mockito.mock(MimeMessage.class);
    when(message.getFrom()).thenReturn(new Address[]{new InternetAddress("[email protected]")});
    doThrow(new IOException()).when(message).writeTo(any(OutputStream.class));
    try {
        transport.sendMessage(message, new Address[]{new InternetAddress("[email protected]")});
        Assert.fail("Exception expected");
    } catch (MessagingException ex) {
        waitForListeners();
        verify(transportListener, times(1)).messageNotDelivered(any(TransportEvent.class));
    }
}
 
Example #7
Source File: SmtpJmsTransportTest.java    From javamail with Apache License 2.0 5 votes vote down vote up
@Test(expected = MessagingException.class)
public void testErrorWritingMessage() throws Exception {
    Message message = Mockito.mock(Message.class);
    when(message.getFrom()).thenReturn(new Address[] { new InternetAddress("[email protected]") });
    when(message.getHeader(eq("Message-ID"))).thenReturn(new String[] {"mockMID"});
    doThrow(new IOException("mock")).when(message).writeTo(any(OutputStream.class));
    try {
        transport.sendMessage(message, new Address[]{new InternetAddress("[email protected]")});
    } finally {
        waitForListeners();
        verify(transportListener).messageNotDelivered(any(TransportEvent.class));
    }
}
 
Example #8
Source File: TransportTest.java    From javamail with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #9
Source File: SmtpJmsTransportTest.java    From javamail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSend() throws Exception {
    final Address[] sendTo = new Address[] { new InternetAddress("[email protected]") };
    Message message = Mockito.mock(Message.class);
    when(message.getHeader(eq(SmtpJmsTransport.X_SEND_PRIORITY))).thenReturn(new String[]{"low"});
    when(message.getFrom()).thenReturn(new Address[] { new InternetAddress("[email protected]") });
    when(message.getSubject()).thenReturn("msgSubject!");
    transport.sendMessage(message, sendTo);
    ArgumentCaptor<byte[]> bytesCaptor = ArgumentCaptor.forClass(byte[].class);
    verify(bytesMessage, times(1)).writeBytes(bytesCaptor.capture());
    byte[] messageBuffer = bytesCaptor.getValue();
    assertNotNull(messageBuffer);
    ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(messageBuffer));
    String protocol = input.readUTF();
    Address[] inAddr = (Address[]) input.readObject();
    assertEquals(1, inAddr.length);
    assertArrayEquals(sendTo, inAddr);
    assertEquals("smtp", protocol);
    verify(message, times(1)).writeTo(any(ObjectOutputStream.class));
    waitForListeners();
    ArgumentCaptor<TransportEvent> transportEventArgumentCaptor = ArgumentCaptor.forClass(TransportEvent.class);
    verify(transportListener, times(1)).messageDelivered(transportEventArgumentCaptor.capture());
    TransportEvent event = transportEventArgumentCaptor.getValue();
    assertEquals(message, event.getMessage());
    assertArrayEquals(new Address[0], event.getInvalidAddresses());
    assertArrayEquals(sendTo, event.getValidSentAddresses());
    assertArrayEquals(new Address[0], event.getValidUnsentAddresses());
}
 
Example #10
Source File: SmtpJmsTransportTest.java    From javamail with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoAddresses() throws Exception {
    Message message = Mockito.mock(Message.class);
    when(message.getFrom()).thenReturn(new Address[] { new InternetAddress("[email protected]") });
    try {
        transport.sendMessage(message, new Address[]{}); // empty address list
        fail();
    } catch (MessagingException ex) {
        waitForListeners();
        verify(transportListener).messageNotDelivered(any(TransportEvent.class));
    }
}
 
Example #11
Source File: AbstractTransportTest.java    From javamail with Apache License 2.0 5 votes vote down vote up
@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));
    }
}
 
Example #12
Source File: Test_SmtpSender.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #13
Source File: EmailNotifier.java    From streamline with Apache License 2.0 5 votes vote down vote up
/**
 * {@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);
    }
}
 
Example #14
Source File: SmtpJmsTransportTest.java    From javamail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWitEmptyFrom() throws Exception {
    Message message = Mockito.mock(Message.class);
    when(message.getFrom()).thenReturn(new Address[0]);
    try {
        transport.sendMessage(message, new Address[]{new InternetAddress("[email protected]")});
        fail();
    } catch (MessagingException ex) {
        waitForListeners();
        verify(transportListener, times(1)).messageNotDelivered(any(TransportEvent.class));
    }
}
 
Example #15
Source File: SmtpJmsTransport.java    From javamail with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example #16
Source File: MailTransportListener.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
public void messageNotDelivered(
                                 TransportEvent e ) {

    processResult(DELIVERY_STATE.ERROR_DELIVERING);
}
 
Example #17
Source File: MailTransportListener.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
public void messagePartiallyDelivered(
                                       TransportEvent e ) {

    processResult(DELIVERY_STATE.ERROR_DELIVERING);
}
 
Example #18
Source File: EmailNotifier.java    From streamline with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc} This fails the notification by invoking {@link NotificationContext#fail(String)}
 */
public void messageNotDelivered(TransportEvent event) {
    LOG.debug("Got messageNotDelivered event {}", event);
    handleFail(event);
}
 
Example #19
Source File: MailTransportListener.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
public void messageDelivered(
                              TransportEvent e ) {

    processResult(DELIVERY_STATE.DELIVERED);
}
 
Example #20
Source File: AbstractTransport.java    From javamail with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example #21
Source File: NopTransport.java    From javamail with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #22
Source File: EmailNotifier.java    From streamline with Apache License 2.0 4 votes vote down vote up
private void handleFail(TransportEvent event) {
    String notificationId = msgNotificationMap.remove(event.getMessage());
    if (notificationId != null) {
        ctx.fail(notificationId);
    }
}
 
Example #23
Source File: EmailNotifier.java    From streamline with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 * <p/>
 * TODO: See if partially delivered should be handled separately
 */
public void messagePartiallyDelivered(TransportEvent event) {
    LOG.debug("Got messagePartiallyDelivered event {}", event);
    handleFail(event);
}