package com.alexanderlogistics.mail; import java.util.Optional; import java.util.logging.Logger; import org.eclipse.microprofile.config.inject.ConfigProperty; import org.imixs.workflow.ItemCollection; import org.imixs.workflow.exceptions.PluginException; import jakarta.inject.Inject; import jakarta.mail.MessagingException; import jakarta.mail.internet.InternetAddress; import jakarta.mail.internet.MimeMessage; /** * Dieses Plugin überschreibt einfach nur From und Sender im Header. Ist in * neuen Versionen von Imixs-Workflow 6.2.2 automatisch gelöst. * */ public class MailPlugin extends org.imixs.marty.profile.MailPlugin { private static final Logger logger = Logger.getLogger(MailPlugin.class.getName()); @Inject @ConfigProperty(name = "mail.authenticatedSender") Optional mailAuthenticatedSender; /** * This method adds the attachments of the blob workitem to the MimeMessage */ @Override public ItemCollection run(ItemCollection documentContext, ItemCollection documentActivity) throws PluginException { // run default functionality ItemCollection result = super.run(documentContext, documentActivity); boolean debug = true; // immer debuggen!! // now get the Mail Session object and adapt sender and from!! MimeMessage mailMessage = (MimeMessage) super.getMailMessage(); if (mailMessage != null) { try { /* * Now Set Sender, From and ReplyTo addresses... */ logger.info("├── 📭 AGL: New mail message..."); // compute From address - can be overwritten by env mail.defaultSender InternetAddress adr = getInternetAddress(getFrom(documentContext, documentActivity)); if (adr == null) { // in case of null we set an empty address here. This can happen in case // of a RunAs Service EJB where the user context can not be resolved to a valid // smtp address if (debug) { logger.warning("│   ├── from address was resolved to null"); } // we will force a MessagingException... adr = new InternetAddress(""); } else { // Test if a mailAuthenticatedSender.isPresent()) if (mailAuthenticatedSender.isPresent()) { logger.info("│   ├── set authenticatedSender"); logger.info("│   ├── set sender='" + mailAuthenticatedSender.get() + "'"); mailMessage.setHeader("Sender", mailAuthenticatedSender.get()); } // default - current sender mailMessage.setFrom(adr); logger.info("│   ├── from:" + adr.getAddress()); // Dow we have a replay to? String sReplyTo = getReplyTo(documentContext, documentActivity); if ((sReplyTo == null) || (sReplyTo.isEmpty())) { sReplyTo = adr.getAddress(); } logger.info("│   ├── replyTo:" + sReplyTo); InternetAddress[] resplysAdrs = new InternetAddress[1]; resplysAdrs[0] = getInternetAddress(sReplyTo); mailMessage.setReplyTo(resplysAdrs); } } catch (MessagingException e) { throw new PluginException(MailPlugin.class.getSimpleName(), ERROR_MAIL_MESSAGE, e.getMessage(), e); } } return result; } }