From b87ccbe1232eb5618b57267482acd178b6c25ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Fri, 17 Jun 2022 17:39:41 +0200 Subject: [PATCH] configurable debug for sending mail --- .../main/java/org/xbib/groovy/smtp/SMTP.java | 93 ++++++++++++++++--- 1 file changed, 80 insertions(+), 13 deletions(-) diff --git a/groovy-mail/src/main/java/org/xbib/groovy/smtp/SMTP.java b/groovy-mail/src/main/java/org/xbib/groovy/smtp/SMTP.java index 03909af..863914b 100644 --- a/groovy-mail/src/main/java/org/xbib/groovy/smtp/SMTP.java +++ b/groovy-mail/src/main/java/org/xbib/groovy/smtp/SMTP.java @@ -1,5 +1,6 @@ package org.xbib.groovy.smtp; +import java.util.List; import javax.mail.Address; import javax.mail.Authenticator; import javax.mail.Message; @@ -27,22 +28,25 @@ public class SMTP { private final String password; - private SMTP(String url, String username, String password) { + private final boolean debug; + + private SMTP(String url, String username, String password, boolean debug) { this.url = url; this.username = username; this.password = password; + this.debug = debug; } public static SMTP newInstance() { - return new SMTP(DEFAULT_URL, null, null); + return new SMTP(DEFAULT_URL, null, null, false); } public static SMTP newInstance(String url) { - return new SMTP(url, null, null); + return new SMTP(url, null, null, false); } - public static SMTP newInstance(String url, String username, String password) { - return new SMTP(url, username, password); + public static SMTP newInstance(String url, String username, String password, boolean debug) { + return new SMTP(url, username, password, debug); } public String getURL() { @@ -50,17 +54,77 @@ public class SMTP { } public void send(String subject, String from, String to, String text) throws Exception { - Address[] toAddr = { new InternetAddress(to) }; + if (from == null) { + return; + } + if (to == null) { + return; + } + String[] s = to.split(","); + Address[] toAddr = new Address[s.length]; + for (int i = 0; i < s.length; i++) { + toAddr[i] = new InternetAddress(s[i]); + } send(subject, new InternetAddress(from), null, toAddr, null, null, text); } + public void send(String subject, String from, List to, String text) throws Exception { + if (from == null) { + return; + } + if (to == null) { + return; + } + if (to.size() > 0) { + Address[] toAddr = new Address[to.size()]; + for (int i = 0; i < to.size(); i++) { + toAddr[i] = new InternetAddress(to.get(i)); + } + send(subject, new InternetAddress(from), null, toAddr, null, null, text); + } + } + + public void send(String subject, String from, List to, List cc, List bcc, String text) throws Exception { + if (from == null) { + return; + } + if (to == null) { + return; + } + Address[] toAddr = null; + if (to.size() > 0) { + toAddr = new Address[to.size()]; + for (int i = 0; i < to.size(); i++) { + toAddr[i] = new InternetAddress(to.get(i)); + } + } + Address[] ccAddr = null; + if (cc != null && cc.size() > 0) { + ccAddr = new Address[cc.size()]; + for (int i = 0; i < cc.size(); i++) { + ccAddr[i] = new InternetAddress(cc.get(i)); + } + } + Address[] bccAddr = null; + if (bcc != null && bcc.size() > 0) { + bccAddr = new Address[bcc.size()]; + for (int i = 0; i < bcc.size(); i++) { + bccAddr[i] = new InternetAddress(bcc.get(i)); + } + } + send(subject, new InternetAddress(from), null, toAddr, ccAddr, bccAddr, text); + } + public void send(String subject, Address from, Address[] to, String text) throws Exception { send(subject, from, null, to, null, null, text); } public void send(String subject, - Address from, Address[] replyTo, - Address[] to, Address[] cc, Address[] bcc, + Address from, + Address[] replyTo, + Address[] to, + Address[] cc, + Address[] bcc, String text) throws Exception { Multipart multipart = new MimeMultipart("mixed"); MimeBodyPart mimeBodyPart = new MimeBodyPart(); @@ -71,8 +135,11 @@ public class SMTP { } public void send(String subject, - Address from, Address[] replyTo, - Address[] to, Address[] cc, Address[] bcc, + Address from, + Address[] replyTo, + Address[] to, + Address[] cc, + Address[] bcc, Multipart multipart) throws Exception { WithContext action = ctx -> { Message message = new MimeMessage(ctx.session); @@ -101,7 +168,7 @@ public class SMTP { try { if (url != null) { ctx = new SmtpContext(); - ctx.properties = createEnvironment(url); + ctx.properties = createEnvironment(url, debug); ctx.session = username != null ? Session.getDefaultInstance(ctx.properties, new SMTPAuthenticator()) : Session.getDefaultInstance(ctx.properties); @@ -116,7 +183,7 @@ public class SMTP { } } - private static Properties createEnvironment(String urlSpec) { + private static Properties createEnvironment(String urlSpec, boolean debug) { URI uri = URI.create(urlSpec); Properties env = new Properties(); env.setProperty("mail.smtp.auth", "false"); @@ -124,7 +191,7 @@ public class SMTP { env.setProperty("mail.smtp.port", Integer.toString(uri.getPort())); boolean secure = uri.getScheme().equals("smtps") || 995 == uri.getPort(); env.setProperty("mail.smtp.ssl.enable", secure ? "true" : "false"); - env.setProperty("mail.debug", "true"); + env.setProperty("mail.debug", Boolean.toString(debug)); return env; }