Search in sources :

Example 1 with DELIVERY_STATE

use of com.axway.ats.action.mail.model.MailTransportListener.DELIVERY_STATE in project ats-framework by Axway.

the class MailSender method send.

/**
 * Sends a MIME package by invoking the following actions:
 *
 * <blockquote> 1. Tags the package, this can be later used for IMAP verification<br>
 * 2. Sings the package when a signer is specified<br>
 * 3. Encrypts the package when a encryptor is specified<br>
 * 4. Sends it </blockquote>
 *
 * @see com.axway.ats.action.model.PackageSender#send(com.axway.ats.action.objects.model.Package)
 */
@Override
@PublicAtsApi
public void send(Package sourcePackage) throws ActionException {
    if (!(sourcePackage instanceof MimePackage)) {
        throw new WrongPackageException("Could not send '" + sourcePackage.getClass().getSimpleName() + "' packages. " + MimePackage.class.getSimpleName() + " is expected");
    }
    // initialize the SMTP session
    initSession();
    MimePackage mimePackage = (MimePackage) sourcePackage;
    // tag the package
    mimePackage.tag();
    // sign the package if needed
    mimePackage = sign(mimePackage);
    // encrypt the package if needed
    mimePackage = encrypt(mimePackage);
    // then send
    final DELIVERY_STATE messageDeliveryState;
    try {
        log.info("Connect to mail server " + mailHost + " at port " + mailPort);
        Object messageSendingMutex = new Object();
        MailTransportListener transListener = new MailTransportListener(messageSendingMutex);
        transport.addTransportListener(transListener);
        transport.connect();
        log.info("Sending " + mimePackage.getDescription());
        transport.sendMessage(mimePackage.getMimeMessage(), extractAllRecipients(mimePackage));
        synchronized (messageSendingMutex) {
            /*
                 * Wait some time for message delivery.
                 *
                 * We are either notified by the mail transport listener when the send has finished(successfully or not)
                 * or we have reached the wait timeout
                 */
            messageSendingMutex.wait(configurator.getMailTimeout());
        }
        messageDeliveryState = transListener.getDeliveryState();
        transport.close();
        transport.removeTransportListener(transListener);
    } catch (MessagingException e) {
        throw new ActionException("Could not send package via SMTP to host '" + mailHost + "' and port " + mailPort, e);
    } catch (InterruptedException e) {
        throw new ActionException("Could not send package", e);
    }
    // evaluate the mail send result
    if (messageDeliveryState == DELIVERY_STATE.DELIVERED) {
        log.info(mimePackage.getDescription() + " " + messageDeliveryState);
    } else {
        throw new ActionException("Result of sending " + mimePackage.getDescription() + ": " + messageDeliveryState.toString());
    }
}
Also used : MimePackage(com.axway.ats.action.objects.MimePackage) WrongPackageException(com.axway.ats.action.objects.model.WrongPackageException) MessagingException(javax.mail.MessagingException) MailTransportListener(com.axway.ats.action.mail.model.MailTransportListener) ActionException(com.axway.ats.action.model.ActionException) DELIVERY_STATE(com.axway.ats.action.mail.model.MailTransportListener.DELIVERY_STATE) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

MailTransportListener (com.axway.ats.action.mail.model.MailTransportListener)1 DELIVERY_STATE (com.axway.ats.action.mail.model.MailTransportListener.DELIVERY_STATE)1 ActionException (com.axway.ats.action.model.ActionException)1 MimePackage (com.axway.ats.action.objects.MimePackage)1 WrongPackageException (com.axway.ats.action.objects.model.WrongPackageException)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 MessagingException (javax.mail.MessagingException)1