Search in sources :

Example 1 with EMailSentStatus

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();
}
Also used : FillMandatoryException(org.adempiere.exceptions.FillMandatoryException) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) WebuiEmailRemovedEvent(de.metas.ui.web.mail.WebuiMailRepository.WebuiEmailRemovedEvent) IntegerLookupValue(de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue) ITrx(org.adempiere.ad.trx.api.ITrx) Env(org.compiere.util.Env) Autowired(org.springframework.beans.factory.annotation.Autowired) UnaryOperator(java.util.function.UnaryOperator) DocumentCollection(de.metas.ui.web.window.model.DocumentCollection) ApiOperation(io.swagger.annotations.ApiOperation) Map(java.util.Map) JSONEmail(de.metas.ui.web.mail.json.JSONEmail) PostMapping(org.springframework.web.bind.annotation.PostMapping) ImmutableSet(com.google.common.collect.ImmutableSet) EMail(de.metas.email.EMail) NonNull(lombok.NonNull) Set(java.util.Set) EventListener(org.springframework.context.event.EventListener) RestController(org.springframework.web.bind.annotation.RestController) JSONLookupValuesList(de.metas.ui.web.window.datatypes.json.JSONLookupValuesList) UserSession(de.metas.ui.web.session.UserSession) Services(org.adempiere.util.Services) List(java.util.List) I_AD_Client(org.compiere.model.I_AD_Client) Stream(java.util.stream.Stream) DocumentPrint(de.metas.ui.web.window.model.DocumentCollection.DocumentPrint) IMailBL(de.metas.email.IMailBL) ApiModel(io.swagger.annotations.ApiModel) LogManager(de.metas.logging.LogManager) EMailSentStatus(de.metas.email.EMailSentStatus) WebConfig(de.metas.ui.web.config.WebConfig) DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) JSONDocumentChangedEvent(de.metas.ui.web.window.datatypes.json.JSONDocumentChangedEvent) Supplier(com.google.common.base.Supplier) IUserBL(org.adempiere.user.api.IUserBL) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) IUserDAO(org.adempiere.user.api.IUserDAO) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) RequestBody(org.springframework.web.bind.annotation.RequestBody) ImmutableList(com.google.common.collect.ImmutableList) GetMapping(org.springframework.web.bind.annotation.GetMapping) LookupValuesList(de.metas.ui.web.window.datatypes.LookupValuesList) I_AD_User(org.compiere.model.I_AD_User) JSONLookupValue(de.metas.ui.web.window.datatypes.json.JSONLookupValue) LookupValue(de.metas.ui.web.window.datatypes.LookupValue) MADBoilerPlate(de.metas.letters.model.MADBoilerPlate) Properties(java.util.Properties) Logger(org.slf4j.Logger) JSONDocumentPath(de.metas.ui.web.window.datatypes.json.JSONDocumentPath) IOException(java.io.IOException) WebuiEmailBuilder(de.metas.ui.web.mail.WebuiEmail.WebuiEmailBuilder) BoilerPlateContext(de.metas.letters.model.MADBoilerPlate.BoilerPlateContext) AdempiereException(org.adempiere.exceptions.AdempiereException) IClientDAO(org.adempiere.service.IClientDAO) Check(de.metas.printing.esb.base.util.Check) EMailAttachment(de.metas.email.EMailAttachment) JSONEmailRequest(de.metas.ui.web.mail.json.JSONEmailRequest) MultipartFile(org.springframework.web.multipart.MultipartFile) I_AD_User(org.compiere.model.I_AD_User) EMailSentStatus(de.metas.email.EMailSentStatus) FillMandatoryException(org.adempiere.exceptions.FillMandatoryException) EMail(de.metas.email.EMail) IUserDAO(org.adempiere.user.api.IUserDAO) AdempiereException(org.adempiere.exceptions.AdempiereException) IClientDAO(org.adempiere.service.IClientDAO) I_AD_Client(org.compiere.model.I_AD_Client) IMailBL(de.metas.email.IMailBL)

Aggregations

Supplier (com.google.common.base.Supplier)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 EMail (de.metas.email.EMail)1 EMailAttachment (de.metas.email.EMailAttachment)1 EMailSentStatus (de.metas.email.EMailSentStatus)1 IMailBL (de.metas.email.IMailBL)1 MADBoilerPlate (de.metas.letters.model.MADBoilerPlate)1 BoilerPlateContext (de.metas.letters.model.MADBoilerPlate.BoilerPlateContext)1 LogManager (de.metas.logging.LogManager)1 Check (de.metas.printing.esb.base.util.Check)1 WebConfig (de.metas.ui.web.config.WebConfig)1 WebuiEmailBuilder (de.metas.ui.web.mail.WebuiEmail.WebuiEmailBuilder)1 WebuiEmailRemovedEvent (de.metas.ui.web.mail.WebuiMailRepository.WebuiEmailRemovedEvent)1 JSONEmail (de.metas.ui.web.mail.json.JSONEmail)1 JSONEmailRequest (de.metas.ui.web.mail.json.JSONEmailRequest)1 UserSession (de.metas.ui.web.session.UserSession)1 DocumentPath (de.metas.ui.web.window.datatypes.DocumentPath)1 LookupValue (de.metas.ui.web.window.datatypes.LookupValue)1 IntegerLookupValue (de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue)1