Example usage for javax.mail Transport addTransportListener

List of usage examples for javax.mail Transport addTransportListener

Introduction

In this page you can find the example usage for javax.mail Transport addTransportListener.

Prototype

public synchronized void addTransportListener(TransportListener l) 

Source Link

Document

Add a listener for Transport events.

Usage

From source file:org.apache.jmeter.protocol.smtp.sampler.protocol.SendMailCommand.java

/**
 * Sends message to mailserver, waiting for delivery if using synchronous
 * mode.//from   ww w  .  j  av a2s . c  o  m
 *
 * @param message
 *            Message previously prepared by prepareMessage()
 * @throws MessagingException
 *             when problems sending the mail arise
 * @throws IOException
 *             TODO can not see how
 * @throws InterruptedException
 *             when interrupted while waiting for delivery in synchronous
 *             modus
 */
public void execute(Message message) throws MessagingException, IOException, InterruptedException {

    Transport tr = null;
    try {
        tr = session.getTransport(getProtocol());
        SynchronousTransportListener listener = null;

        if (synchronousMode) {
            listener = new SynchronousTransportListener();
            tr.addTransportListener(listener);
        }

        if (useAuthentication) {
            tr.connect(smtpServer, username, password);
        } else {
            tr.connect();
        }

        tr.sendMessage(message, message.getAllRecipients());

        if (listener != null /*synchronousMode==true*/) {
            listener.attend(); // listener cannot be null here
        }
    } finally {
        if (tr != null) {
            try {
                tr.close();
            } catch (Exception e) {
                // NOOP
            }
        }
        logger.debug("transport closed");
    }

    logger.debug("message sent");
    return;
}

From source file:org.silverpeas.core.mail.engine.SmtpMailSender.java

/**
 * This method performs the treatment of the technical send:
 * <ul>/*from  ww w  .  jav  a  2 s .c  om*/
 * <li>connection to the SMTP server</li>
 * <li>sending</li>
 * <li>closing the connection</li>
 * </ul>
 * @param mail the original data from which the given {@link MimeMessage} has been initialized.
 * @param smtpConfiguration the SMTP configuration.
 * @param session the current mail session.
 * @param messageToSend the technical message to send.
 * @param batchedToAddresses the receivers of the message.
 * @throws MessagingException
 */
private void performSend(final MailToSend mail, final SmtpConfiguration smtpConfiguration, Session session,
        MimeMessage messageToSend, List<InternetAddress[]> batchedToAddresses) throws MessagingException {

    // Creating a Transport connection (TCP)
    final Transport transport;
    if (smtpConfiguration.isSecure()) {
        transport = session.getTransport(SmtpConfiguration.SECURE_TRANSPORT);
    } else {
        transport = session.getTransport(SmtpConfiguration.SIMPLE_TRANSPORT);
    }

    // Adding send reporting listener
    transport.addTransportListener(new SmtpMailSendReportListener(mail));

    try {
        if (smtpConfiguration.isAuthenticate()) {
            transport.connect(smtpConfiguration.getServer(), smtpConfiguration.getPort(),
                    smtpConfiguration.getUsername(), smtpConfiguration.getPassword());
        } else {
            transport.connect(smtpConfiguration.getServer(), smtpConfiguration.getPort(), null, null);
        }

        for (InternetAddress[] toAddressBatch : batchedToAddresses) {
            messageToSend.setRecipients(mail.getTo().getRecipientType().getTechnicalType(), toAddressBatch);
            transport.sendMessage(messageToSend, toAddressBatch);
        }

    } finally {
        try {
            transport.close();
        } catch (Exception e) {
            SilverTrace.error("mail", "SmtpMailSender.send()", "root.EX_IGNORED", "ClosingTransport", e);
        }
    }
}