javax.mail.event.TransportListener Java Examples

The following examples show how to use javax.mail.event.TransportListener. 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: SmtpJmsTransportTest.java    From javamail with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, TestContextFactory.class.getName());
    QueueConnectionFactory queueConnectionFactory = Mockito.mock(QueueConnectionFactory.class);
    Queue queue = Mockito.mock(Queue.class);
    Context context = Mockito.mock(Context.class);
    TestContextFactory.context = context;
    when(context.lookup(eq("jms/queueConnectionFactory"))).thenReturn(queueConnectionFactory);
    when(context.lookup(eq("jms/mailQueue"))).thenReturn(queue);
    queueSender = Mockito.mock(QueueSender.class);
    QueueConnection queueConnection = Mockito.mock(QueueConnection.class);
    when(queueConnectionFactory.createQueueConnection()).thenReturn(queueConnection);
    when(queueConnectionFactory.createQueueConnection(anyString(), anyString())).thenReturn(queueConnection);
    QueueSession queueSession = Mockito.mock(QueueSession.class);
    bytesMessage = Mockito.mock(BytesMessage.class);
    when(queueSession.createBytesMessage()).thenReturn(bytesMessage);
    when(queueConnection.createQueueSession(anyBoolean(), anyInt())).thenReturn(queueSession);
    when(queueSession.createSender(eq(queue))).thenReturn(queueSender);
    transport = new SmtpJmsTransport(Session.getDefaultInstance(new Properties()), new URLName("jms"));
    transportListener = Mockito.mock(TransportListener.class);
    transport.addTransportListener(transportListener);
}
 
Example #3
Source File: AbstractTransportTest.java    From javamail with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws MessagingException, IOException {
    Properties properties = new Properties();
    properties.put("mail.files.path", "target" + File.separatorChar + "output");
    Session session = Session.getDefaultInstance(properties);
    message = new MimeMessage(session);
    message.setFrom("Test <[email protected]>");
    connectionListener = Mockito.mock(ConnectionListener.class);
    transportListener = Mockito.mock(TransportListener.class);
    transport = new AbstractTransport(session, new URLName("AbstractDev")) {
        @Override
        public void sendMessage(Message message, Address[] addresses) throws MessagingException {
            validateAndPrepare(message, addresses);
        }
    };
    transport.addConnectionListener(connectionListener);
    transport.addTransportListener(transportListener);
}
 
Example #4
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 #5
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 #6
Source File: SmtpConnectionFactory.java    From smtp-connection-pool with Apache License 2.0 5 votes vote down vote up
public SmtpConnectionFactory(Session session, TransportStrategy transportStrategy, ConnectionStrategy connectionStrategy, boolean invalidateConnectionOnException, Collection<TransportListener> defaultTransportListeners) {
  this.session = session;
  this.transportFactory = transportStrategy;
  this.connectionStrategy = connectionStrategy;
  this.invalidateConnectionOnException = invalidateConnectionOnException;
  this.defaultTransportListeners = new ArrayList<>(defaultTransportListeners);
}
 
Example #7
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 #8
Source File: EmailNotifier.java    From streamline with Apache License 2.0 5 votes vote down vote up
/**
 * Return a {@link Transport} object from the session registering the passed in transport listener
 * for delivery notifications.
 */
private Transport getEmailTransport(Session session, TransportListener listener) {
    try {
        Transport transport = session.getTransport();
        transport.addTransportListener(listener);
        if (!transport.isConnected()) {
            transport.connect();
        }
        LOG.debug("Email transport {}, transport listener {}", transport, listener);
        return transport;
    } catch (MessagingException ex) {
        LOG.error("Got exception while initializing transport", ex);
        throw new NotifierRuntimeException("Got exception while initializing transport", ex);
    }
}
 
Example #9
Source File: DefaultClosableSmtpConnection.java    From smtp-connection-pool with Apache License 2.0 4 votes vote down vote up
public void clearListeners() {
  for (TransportListener transportListener : transportListeners) {
    delegate.removeTransportListener(transportListener);
  }
  transportListeners.clear();
}
 
Example #10
Source File: DefaultClosableSmtpConnection.java    From smtp-connection-pool with Apache License 2.0 4 votes vote down vote up
public void removeTransportListener(TransportListener l) {
  transportListeners.remove(l);
  delegate.removeTransportListener(l);
}
 
Example #11
Source File: DefaultClosableSmtpConnection.java    From smtp-connection-pool with Apache License 2.0 4 votes vote down vote up
public void addTransportListener(TransportListener l) {
  transportListeners.add(l);
  delegate.addTransportListener(l);
}
 
Example #12
Source File: SmtpConnectionFactory.java    From smtp-connection-pool with Apache License 2.0 4 votes vote down vote up
public List<TransportListener> getDefaultListeners() {
  return Collections.unmodifiableList(defaultTransportListeners);
}
 
Example #13
Source File: SmtpConnectionFactory.java    From smtp-connection-pool with Apache License 2.0 4 votes vote down vote up
public SmtpConnectionFactory(Session session, TransportStrategy transportFactory, ConnectionStrategy connectionStrategy, boolean invalidateConnectionOnException) {
  this(session, transportFactory, connectionStrategy, invalidateConnectionOnException, Collections.<TransportListener>emptyList());
}
 
Example #14
Source File: SmtpConnectionFactoryBuilder.java    From smtp-connection-pool with Apache License 2.0 4 votes vote down vote up
public SmtpConnectionFactoryBuilder defaultTransportListeners(TransportListener... listeners) {
  defaultTransportListeners = Arrays.asList(requireNonNull(listeners));
  return this;
}
 
Example #15
Source File: SmtpConnectionFactory.java    From smtp-connection-pool with Apache License 2.0 3 votes vote down vote up
private void initDefaultListeners(ClosableSmtpConnection smtpTransport) {
  for (TransportListener transportListener : defaultTransportListeners) {
    smtpTransport.addTransportListener(transportListener);
  }
}
 
Example #16
Source File: ClosableSmtpConnection.java    From smtp-connection-pool with Apache License 2.0 2 votes vote down vote up
/**
 * Add a new {@link TransportListener}
 *
 * @param l
 */
void addTransportListener(TransportListener l);
 
Example #17
Source File: ClosableSmtpConnection.java    From smtp-connection-pool with Apache License 2.0 2 votes vote down vote up
/**
 * Remove the provided {@link TransportListener}
 *
 * @param l
 */
void removeTransportListener(TransportListener l);