configurable debug for sending mail
This commit is contained in:
parent
1c9ce2a094
commit
b87ccbe123
1 changed files with 80 additions and 13 deletions
|
@ -1,5 +1,6 @@
|
||||||
package org.xbib.groovy.smtp;
|
package org.xbib.groovy.smtp;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import javax.mail.Address;
|
import javax.mail.Address;
|
||||||
import javax.mail.Authenticator;
|
import javax.mail.Authenticator;
|
||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
|
@ -27,22 +28,25 @@ public class SMTP {
|
||||||
|
|
||||||
private final String password;
|
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.url = url;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
|
this.debug = debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SMTP newInstance() {
|
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) {
|
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) {
|
public static SMTP newInstance(String url, String username, String password, boolean debug) {
|
||||||
return new SMTP(url, username, password);
|
return new SMTP(url, username, password, debug);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getURL() {
|
public String getURL() {
|
||||||
|
@ -50,17 +54,77 @@ public class SMTP {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(String subject, String from, String to, String text) throws Exception {
|
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);
|
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 {
|
public void send(String subject, Address from, Address[] to, String text) throws Exception {
|
||||||
send(subject, from, null, to, null, null, text);
|
send(subject, from, null, to, null, null, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(String subject,
|
public void send(String subject,
|
||||||
Address from, Address[] replyTo,
|
Address from,
|
||||||
Address[] to, Address[] cc, Address[] bcc,
|
Address[] replyTo,
|
||||||
|
Address[] to,
|
||||||
|
Address[] cc,
|
||||||
|
Address[] bcc,
|
||||||
String text) throws Exception {
|
String text) throws Exception {
|
||||||
Multipart multipart = new MimeMultipart("mixed");
|
Multipart multipart = new MimeMultipart("mixed");
|
||||||
MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
||||||
|
@ -71,8 +135,11 @@ public class SMTP {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(String subject,
|
public void send(String subject,
|
||||||
Address from, Address[] replyTo,
|
Address from,
|
||||||
Address[] to, Address[] cc, Address[] bcc,
|
Address[] replyTo,
|
||||||
|
Address[] to,
|
||||||
|
Address[] cc,
|
||||||
|
Address[] bcc,
|
||||||
Multipart multipart) throws Exception {
|
Multipart multipart) throws Exception {
|
||||||
WithContext<Object> action = ctx -> {
|
WithContext<Object> action = ctx -> {
|
||||||
Message message = new MimeMessage(ctx.session);
|
Message message = new MimeMessage(ctx.session);
|
||||||
|
@ -101,7 +168,7 @@ public class SMTP {
|
||||||
try {
|
try {
|
||||||
if (url != null) {
|
if (url != null) {
|
||||||
ctx = new SmtpContext();
|
ctx = new SmtpContext();
|
||||||
ctx.properties = createEnvironment(url);
|
ctx.properties = createEnvironment(url, debug);
|
||||||
ctx.session = username != null ?
|
ctx.session = username != null ?
|
||||||
Session.getDefaultInstance(ctx.properties, new SMTPAuthenticator()) :
|
Session.getDefaultInstance(ctx.properties, new SMTPAuthenticator()) :
|
||||||
Session.getDefaultInstance(ctx.properties);
|
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);
|
URI uri = URI.create(urlSpec);
|
||||||
Properties env = new Properties();
|
Properties env = new Properties();
|
||||||
env.setProperty("mail.smtp.auth", "false");
|
env.setProperty("mail.smtp.auth", "false");
|
||||||
|
@ -124,7 +191,7 @@ public class SMTP {
|
||||||
env.setProperty("mail.smtp.port", Integer.toString(uri.getPort()));
|
env.setProperty("mail.smtp.port", Integer.toString(uri.getPort()));
|
||||||
boolean secure = uri.getScheme().equals("smtps") || 995 == uri.getPort();
|
boolean secure = uri.getScheme().equals("smtps") || 995 == uri.getPort();
|
||||||
env.setProperty("mail.smtp.ssl.enable", secure ? "true" : "false");
|
env.setProperty("mail.smtp.ssl.enable", secure ? "true" : "false");
|
||||||
env.setProperty("mail.debug", "true");
|
env.setProperty("mail.debug", Boolean.toString(debug));
|
||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue