Search in sources :

Example 1 with EmailFailedException

use of org.jaffa.modules.printing.services.exceptions.EmailFailedException in project jaffa-framework by jaffa-projects.

the class FormDelivery method sendEmail.

/**
 * Email out the generated document as an attachment
 * @param request Print Request containing possible email info
 * @param document document to put as the email attachment
 * @param dom A context object that can be used to extract values when generating e-mail
 * subject and body information. This object should be a java bean or a map
 * @throws ApplicationExceptions Thrown if any funtional issue ocurred when executing
 * @throws FrameworkException Thrown if any framework issue ocurred when executing
 */
public static void sendEmail(PrintRequest request, File document, Object dom) throws FrameworkException, ApplicationExceptions {
    String documentName = null;
    try {
        // Set up parameters
        Object[] params = null;
        if (request instanceof FormPrintRequest) {
            FormPrintRequest fpr = (FormPrintRequest) request;
            params = new Object[] { fpr.getFormName(), fpr.getFormAlternateName(), fpr.getVariation() };
            documentName = fpr.getFormName();
        } else {
            // Just use the short class name
            documentName = StringHelper.getShortClassName(request.getClass());
        }
        String fromAddress = request.getEmailFromAddress();
        // Use the resource bundle to get subject and body of this message
        String subject = request.getEmailSubject();
        if (subject == null) {
            String token = "label.Jaffa.Printing.Email." + documentName + ".subject";
            subject = MessageHelper.findMessage(token, params);
        }
        String body = request.getEmailMessage();
        if (body == null) {
            String token = "label.Jaffa.Printing.Email." + documentName + ".body";
            body = MessageHelper.findMessage(token, params);
        }
        // @TODO Pass the databean DOM in and use velocity templating to to token replacement of
        // the subject and body for better messages. Expose request and dom to the template
        EmailerBean eb = new EmailerBean();
        eb.sendMail(fromAddress, StringHelper.parseString(request.getEmailToAddresses(), ";"), subject, body, new File[] { document });
    } catch (MessagingException e) {
        log.error("Failed to send email with attachment " + document.getAbsolutePath(), e);
        throw new ApplicationExceptions(new EmailFailedException(documentName));
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) MessagingException(javax.mail.MessagingException) EmailerBean(org.jaffa.util.EmailerBean) EmailFailedException(org.jaffa.modules.printing.services.exceptions.EmailFailedException)

Aggregations

MessagingException (javax.mail.MessagingException)1 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)1 EmailFailedException (org.jaffa.modules.printing.services.exceptions.EmailFailedException)1 EmailerBean (org.jaffa.util.EmailerBean)1