mail plugin
This commit is contained in:
commit
87cd3204c2
4 changed files with 68 additions and 28 deletions
5
doc/MAIL.md
Normal file
5
doc/MAIL.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# Mail
|
||||
|
||||
Testen von Mailing kann man über
|
||||
|
||||
https://www.mail-tester.com/
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
|
||||
|
||||
|
||||
https://techcommunity.microsoft.com/t5/exchange/javamail-connecting-to-office-365-xoauth2-for-imap/m-p/1505026
|
||||
|
||||
|
||||
|
|
@ -1,9 +1,11 @@
|
|||
package com.alexanderlogistics.mail;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Optional;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
import org.imixs.marty.profile.ProfileService;
|
||||
import org.imixs.workflow.ItemCollection;
|
||||
import org.imixs.workflow.exceptions.PluginException;
|
||||
|
||||
|
|
@ -24,8 +26,12 @@ public class MailPlugin extends org.imixs.marty.profile.MailPlugin {
|
|||
@ConfigProperty(name = "mail.authenticatedSender")
|
||||
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
|
||||
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...
|
||||
*/
|
||||
logger.info("├── 📭 AGL: New mail message...");
|
||||
|
||||
// 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) {
|
||||
// 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
|
||||
|
|
@ -54,30 +61,63 @@ public class MailPlugin extends org.imixs.marty.profile.MailPlugin {
|
|||
// 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());
|
||||
logger.info("├── 📭 set authenticated sender...");
|
||||
String mailDefaultPattern = mailAuthenticatedSender.get();
|
||||
String sender = mailDefaultPattern;
|
||||
// Expected format "COMPANY - {username} <webmaster@info.com>"
|
||||
int start = mailDefaultPattern.lastIndexOf('<');
|
||||
int end = mailDefaultPattern.lastIndexOf('>');
|
||||
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);
|
||||
|
||||
// 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);
|
||||
// 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?
|
||||
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);
|
||||
} catch (MessagingException | UnsupportedEncodingException e) {
|
||||
throw new PluginException(MailPlugin.class.getSimpleName(),
|
||||
ERROR_MAIL_MESSAGE, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -1485,6 +1485,7 @@ result.isValid=true;
|
|||
</imixs:item>
|
||||
<imixs:item name="nammailreceiver" type="xs:string">
|
||||
<imixs:value><![CDATA[ralph.soika@imixs.com]]></imixs:value>
|
||||
<imixs:value><![CDATA[test-ntxn36ljd@srv1.mail-tester.com]]></imixs:value>
|
||||
</imixs:item>
|
||||
<imixs:item name="nammailreceivercc" type="xs:string">
|
||||
<imixs:value/>
|
||||
|
|
|
|||
Loading…
Reference in a new issue