update mail plugin

This commit is contained in:
Ralph Soika 2025-06-08 11:11:48 +02:00
parent 9556c419ca
commit 93c902ab4a
4 changed files with 68 additions and 28 deletions

5
doc/MAIL.md Normal file
View file

@ -0,0 +1,5 @@
# Mail
Testen von Mailing kann man über
https://www.mail-tester.com/

View file

@ -1,6 +0,0 @@
https://techcommunity.microsoft.com/t5/exchange/javamail-connecting-to-office-365-xoauth2-for-imap/m-p/1505026

View file

@ -1,9 +1,11 @@
package com.alexanderlogistics.mail; package com.alexanderlogistics.mail;
import java.io.UnsupportedEncodingException;
import java.util.Optional; import java.util.Optional;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.imixs.marty.profile.ProfileService;
import org.imixs.workflow.ItemCollection; import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.exceptions.PluginException; import org.imixs.workflow.exceptions.PluginException;
@ -24,8 +26,12 @@ public class MailPlugin extends org.imixs.marty.profile.MailPlugin {
@ConfigProperty(name = "mail.authenticatedSender") @ConfigProperty(name = "mail.authenticatedSender")
Optional<String> mailAuthenticatedSender; Optional<String> mailAuthenticatedSender;
@Inject
ProfileService profileService;
/** /**
* This method adds the attachments of the blob workitem to the MimeMessage * This method creates a new mail header with custom from and reply-to addresses
*
*/ */
@Override @Override
public ItemCollection run(ItemCollection documentContext, ItemCollection documentActivity) throws PluginException { public ItemCollection run(ItemCollection documentContext, ItemCollection documentActivity) throws PluginException {
@ -41,9 +47,10 @@ public class MailPlugin extends org.imixs.marty.profile.MailPlugin {
/* /*
* Now Set Sender, From and ReplyTo addresses... * Now Set Sender, From and ReplyTo addresses...
*/ */
logger.info("├── 📭 AGL: New mail message...");
// compute From address - can be overwritten by env mail.defaultSender // compute From address - can be overwritten by env mail.defaultSender
InternetAddress adr = getInternetAddress(getFrom(documentContext, documentActivity)); InternetAddress adr = getInternetAddress(getFrom(documentContext,
documentActivity));
if (adr == null) { if (adr == null) {
// in case of null we set an empty address here. This can happen in case // 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 // of a RunAs Service EJB where the user context can not be resolved to a valid
@ -54,30 +61,63 @@ public class MailPlugin extends org.imixs.marty.profile.MailPlugin {
// we will force a MessagingException... // we will force a MessagingException...
adr = new InternetAddress(""); adr = new InternetAddress("");
} else { } else {
// Test if a mailAuthenticatedSender.isPresent())
if (mailAuthenticatedSender.isPresent()) { if (mailAuthenticatedSender.isPresent()) {
logger.info("│   ├── set authenticatedSender"); logger.info("├── 📭 set authenticated sender...");
logger.info("│   ├── set sender='" + mailAuthenticatedSender.get() + "'"); String mailDefaultPattern = mailAuthenticatedSender.get();
mailMessage.setHeader("Sender", mailAuthenticatedSender.get()); String sender = mailDefaultPattern;
} // Expected format "COMPANY - {username} <webmaster@info.com>"
// default - current sender int start = mailDefaultPattern.lastIndexOf('<');
mailMessage.setFrom(adr); int end = mailDefaultPattern.lastIndexOf('>');
logger.info("│   ├── from:" + adr.getAddress()); if (start >= 0 && end >= 0) {
String senderDisplayName = mailDefaultPattern.substring(0, start);
sender = mailDefaultPattern.substring(start + 1, end);
logger.info("│ ├── sender='" + sender + "'");
mailMessage.setHeader("Sender", sender);
// create custom from...
String userName = this.getWorkflowService().getUserName();
String userDisplayName = userName;
String userEmail = userName;
ItemCollection profile = profileService.findProfileById(userName);
if (profile != null) {
userDisplayName = profile.getItemValueString("txtuserName");
userEmail = profile.getItemValueString("txtemail");
senderDisplayName = senderDisplayName.replace("{username}", userDisplayName);
} else {
logger.warning(
"├── ⚠️ no matching user profile found for " + userName);
}
InternetAddress fromAddress = new InternetAddress(sender, senderDisplayName);
mailMessage.setFrom(fromAddress);
logger.info(
"│ ├── from:" + fromAddress.getPersonal() + "<" + fromAddress.getAddress() + ">");
logger.info("│ ├── replyTo:" + userEmail);
InternetAddress[] replyToAddressList = new InternetAddress[1];
replyToAddressList[0] = getInternetAddress(userEmail);
mailMessage.setReplyTo(replyToAddressList);
} else {
// default behavior - set sender and reply only
logger.info("│ ├── sender='" + sender + "'");
mailMessage.setHeader("Sender", sender);
// Dow we have a replay to? // Dow we have a replay to?
String sReplyTo = getReplyTo(documentContext, documentActivity); String sReplyTo = getReplyTo(documentContext, documentActivity);
if ((sReplyTo == null) || (sReplyTo.isEmpty())) { if ((sReplyTo == null) || (sReplyTo.isEmpty())) {
sReplyTo = adr.getAddress(); sReplyTo = adr.getAddress();
} }
logger.info("│   ├── replyTo:" + sReplyTo); logger.info(" ├── replyTo:" + sReplyTo);
InternetAddress[] resplysAdrs = new InternetAddress[1]; InternetAddress[] resplysAdrs = new InternetAddress[1];
resplysAdrs[0] = getInternetAddress(sReplyTo); resplysAdrs[0] = getInternetAddress(sReplyTo);
mailMessage.setReplyTo(resplysAdrs); mailMessage.setReplyTo(resplysAdrs);
} }
} catch (MessagingException e) { }
throw new PluginException(MailPlugin.class.getSimpleName(), ERROR_MAIL_MESSAGE, e.getMessage(), e); }
} catch (MessagingException | UnsupportedEncodingException e) {
throw new PluginException(MailPlugin.class.getSimpleName(),
ERROR_MAIL_MESSAGE, e.getMessage(), e);
} }
} }
return result; return result;

View file

@ -1485,6 +1485,7 @@ result.isValid=true;
</imixs:item> </imixs:item>
<imixs:item name="nammailreceiver" type="xs:string"> <imixs:item name="nammailreceiver" type="xs:string">
<imixs:value><![CDATA[ralph.soika@imixs.com]]></imixs:value> <imixs:value><![CDATA[ralph.soika@imixs.com]]></imixs:value>
<imixs:value><![CDATA[test-ntxn36ljd@srv1.mail-tester.com]]></imixs:value>
</imixs:item> </imixs:item>
<imixs:item name="nammailreceivercc" type="xs:string"> <imixs:item name="nammailreceivercc" type="xs:string">
<imixs:value/> <imixs:value/>