From 36049f1e76d45ce0ef7cf5cf529be16fb99b7958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Thu, 5 May 2022 17:40:25 +0200 Subject: [PATCH] enable multiple addresses in SMTP --- .../org/xbib/groovy/crypt/CryptUtil.groovy | 13 +-- .../groovy/crypt/random/RandomUtil.groovy | 6 ++ groovy-mail/gradle.properties | 2 +- .../main/java/org/xbib/groovy/smtp/SMTP.java | 86 ++++++++++++++++--- 4 files changed, 85 insertions(+), 22 deletions(-) diff --git a/groovy-crypt/src/main/groovy/org/xbib/groovy/crypt/CryptUtil.groovy b/groovy-crypt/src/main/groovy/org/xbib/groovy/crypt/CryptUtil.groovy index daba2e8..f826f67 100644 --- a/groovy-crypt/src/main/groovy/org/xbib/groovy/crypt/CryptUtil.groovy +++ b/groovy-crypt/src/main/groovy/org/xbib/groovy/crypt/CryptUtil.groovy @@ -1,5 +1,7 @@ package org.xbib.groovy.crypt +import org.xbib.groovy.crypt.random.RandomUtil + import javax.crypto.Mac import javax.crypto.SecretKeyFactory import javax.crypto.spec.PBEKeySpec @@ -9,7 +11,6 @@ import java.nio.ByteOrder import java.nio.charset.StandardCharsets import java.security.MessageDigest import java.security.NoSuchAlgorithmException -import java.security.SecureRandom /** * A utility class for invoking encryption methods and returning LDAP password string, @@ -17,8 +18,6 @@ import java.security.SecureRandom */ class CryptUtil { - private static final Random random = new SecureRandom() - static String hexDigest(String plainText, String algo, String prefix) throws NoSuchAlgorithmException { if (plainText == null) { return null @@ -152,13 +151,7 @@ class CryptUtil { } static String randomHexString(int length) { - randomBytes(length).encodeHex() - } - - static byte[] randomBytes(int length) { - byte[] b = new byte[length] - random.nextBytes(b) - b + RandomUtil.randomBytes(length).encodeHex() } static ByteBuffer htonl(int value) { diff --git a/groovy-crypt/src/main/groovy/org/xbib/groovy/crypt/random/RandomUtil.groovy b/groovy-crypt/src/main/groovy/org/xbib/groovy/crypt/random/RandomUtil.groovy index 222e660..066ae61 100644 --- a/groovy-crypt/src/main/groovy/org/xbib/groovy/crypt/random/RandomUtil.groovy +++ b/groovy-crypt/src/main/groovy/org/xbib/groovy/crypt/random/RandomUtil.groovy @@ -11,4 +11,10 @@ class RandomUtil { secureRandom.nextBytes(b) b.encodeHex().toString() } + + static byte[] randomBytes(int length) { + byte[] b = new byte[length] + secureRandom.nextBytes(b) + b + } } diff --git a/groovy-mail/gradle.properties b/groovy-mail/gradle.properties index d9455e2..952967a 100644 --- a/groovy-mail/gradle.properties +++ b/groovy-mail/gradle.properties @@ -1 +1 @@ -version = 2.2.0 \ No newline at end of file +version = 2.2.1 \ No newline at end of file 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..1d61132 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(); @@ -101,7 +165,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 +180,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 +188,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; }