enable multiple addresses in SMTP

This commit is contained in:
Jörg Prante 2022-05-05 17:40:25 +02:00
parent 3d1011ce71
commit 36049f1e76
4 changed files with 85 additions and 22 deletions

View file

@ -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) {

View file

@ -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
}
}

View file

@ -1 +1 @@
version = 2.2.0
version = 2.2.1

View file

@ -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<String> 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<String> to, List<String> cc, List<String> 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;
}