use of eu.ggnet.dwoss.mandator.api.value.partial.MandatorMailAttachment in project dwoss by gg-net.
the class DocumentSupporterOperation method mail.
/**
* This method send document to the e-Mail address that is in the customer set.
* <p/>
* @param document This is the Document that will be send.
* @throws UserInfoException if the sending of the Mail is not successful.
* @throws RuntimeException if problems exist in the JasperExporter
*/
@Override
public void mail(Document document, DocumentViewType jtype) throws UserInfoException, RuntimeException {
UiCustomer customer = customerService.asUiCustomer(document.getDossier().getCustomerId());
String customerMailAddress = customerService.asCustomerMetaData(document.getDossier().getCustomerId()).getEmail();
if (customerMailAddress == null) {
throw new UserInfoException("Kunde hat keine E-Mail Hinterlegt! Senden einer E-Mail ist nicht Möglich!");
}
String doctype = (jtype == DocumentViewType.DEFAULT ? document.getType().getName() : jtype.getName());
try (InputStream is = mandator.getMailTemplateLocation().toURL().openStream();
InputStreamReader templateReader = new InputStreamReader(is)) {
String text = new MailDocumentParameter(customer.toTitleNameLine(), doctype).eval(IOUtils.toString(templateReader));
MultiPartEmail email = mandator.prepareDirectMail();
email.setCharset("UTF-8");
email.addTo(customerMailAddress);
email.setSubject(document.getType().getName() + " | " + document.getDossier().getIdentifier());
email.setMsg(text + mandator.getDefaultMailSignature());
email.attach(new ByteArrayDataSource(JasperExportManager.exportReportToPdf(jasper(document, jtype)), "application/pdf"), "Dokument.pdf", "Das ist das Dokument zu Ihrem Aufrag als PDF.");
for (MandatorMailAttachment mma : mandator.getDefaultMailAttachment()) {
email.attach(mma.getAttachmentData().toURL(), mma.getAttachmentName(), mma.getAttachmentDescription());
}
email.send();
} catch (EmailException ex) {
L.error("Error on Mail sending", ex);
throw new UserInfoException("Das senden der Mail war nicht erfolgreich!\n" + ex.getMessage());
} catch (IOException | JRException e) {
throw new RuntimeException(e);
}
}
Aggregations