use of de.metas.email.EMailSentStatus in project metasfresh-webui-api by metasfresh.
the class MailRestController method sendEmail.
private WebuiEmail sendEmail(final WebuiEmail webuiEmail) {
final String emailId = webuiEmail.getEmailId();
//
// Create the email object
final I_AD_Client adClient = Services.get(IClientDAO.class).retriveClient(Env.getCtx(), userSession.getAD_Client_ID());
final String mailCustomType = null;
final I_AD_User from = Services.get(IUserDAO.class).retrieveUser(webuiEmail.getFrom().getIdAsInt());
final List<String> toList = extractEMailAddreses(webuiEmail.getTo()).collect(ImmutableList.toImmutableList());
if (toList.isEmpty()) {
throw new FillMandatoryException("To");
}
final String to = toList.get(0);
final String subject = webuiEmail.getSubject();
final String message = webuiEmail.getMessage();
final boolean html = false;
final EMail email = Services.get(IMailBL.class).createEMail(adClient, mailCustomType, from, to, subject, message, html);
toList.stream().skip(1).forEach(email::addTo);
webuiEmail.getAttachments().stream().map(webuiAttachment -> {
final byte[] content = mailAttachmentsRepo.getAttachmentAsByteArray(emailId, webuiAttachment);
return EMailAttachment.of(webuiAttachment.getDisplayName(), content);
}).forEach(email::addAttachment);
//
// Actually send the email
final EMailSentStatus sentStatus = email.send();
if (!sentStatus.isSentOK()) {
throw new AdempiereException("Failed sending the email: " + sentStatus.getSentMsg());
}
//
// Delete temporary attachments
mailAttachmentsRepo.deleteAttachments(emailId, webuiEmail.getAttachments());
// Mark the webui email as sent
return webuiEmail.toBuilder().sent(true).build();
}
Aggregations