Skip to content

Commit

Permalink
Release 2.0.0:
Browse files Browse the repository at this point in the history
Upgrade to jakarta mail
  • Loading branch information
labrotni committed Aug 22, 2021
1 parent 26eca09 commit 10f32d8
Show file tree
Hide file tree
Showing 21 changed files with 992 additions and 1,090 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ SmtpConnectionPool smtpConnectionPool = new SmtpConnectionPool(SmtpConnectionFac
//borrow an object in a try-with-resource statement or call `close` by yourself
try (ClosableSmtpConnection transport = smtpConnectionPool.borrowObject()) {
MimeMessage mimeMessage = new MimeMessage(transport.getSession());
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false);
mimeMessageHelper.addTo("[email protected]");
mimeMessageHelper.setFrom("[email protected]");
mimeMessageHelper.setSubject("Hi!");
mimeMessageHelper.setText("Hello World!", false);
mimeMessage.addRecipients(Message.RecipientType.TO, to);
mimeMessage.setFrom("[email protected]");
mimeMessage.setSubject("Hi!");
mimeMessage.setText("Hello World!");
transport.sendMessage(mimeMessage);
}

Expand Down
21 changes: 4 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.nithril</groupId>
<artifactId>smtp-connection-pool</artifactId>
<version>1.5.0</version>
<version>2.0.0</version>

<name>SMTP Connection Pool</name>
<description>SMTP Connection Pool which uses JavaMail and Apache Common Pool</description>
Expand Down Expand Up @@ -37,8 +37,8 @@

<properties>
<jakarta.mail.version>2.0.1</jakarta.mail.version>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>


Expand All @@ -53,7 +53,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.7.0</version>
<version>2.11.1</version>
</dependency>

<dependency>
Expand All @@ -76,24 +76,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>2.0.0-alpha-1</version>
<scope>test</scope>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>com.sun.mail</groupId>-->
<!-- <artifactId>javax.mail</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>

<dependency>
Expand Down
308 changes: 154 additions & 154 deletions src/main/java/org/nlab/smtp/exception/MailSendException.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,165 +32,165 @@
@SuppressWarnings("serial")
public class MailSendException extends RuntimeException {

private transient final Map<Object, Exception> failedMessages;

private Exception[] messageExceptions;


/**
* Constructor for MailSendException.
*
* @param msg the detail message
*/
public MailSendException(String msg) {
this(msg, null);
}

/**
* Constructor for MailSendException.
*
* @param msg the detail message
* @param cause the root cause from the mail API in use
*/
public MailSendException(String msg, Throwable cause) {
super(msg, cause);
this.failedMessages = new LinkedHashMap<Object, Exception>();
}

/**
* Constructor for registration of failed messages, with the
* messages that failed as keys, and the thrown exceptions as values.
* <p>The messages should be the same that were originally passed
* to the invoked send method.
*
* @param msg the detail message
* @param cause the root cause from the mail API in use
* @param failedMessages Map of failed messages as keys and thrown
* exceptions as values
*/
public MailSendException(String msg, Throwable cause, Map<Object, Exception> failedMessages) {
super(msg, cause);
this.failedMessages = new LinkedHashMap<Object, Exception>(failedMessages);
this.messageExceptions = failedMessages.values().toArray(new Exception[failedMessages.size()]);
}

/**
* Constructor for registration of failed messages, with the
* messages that failed as keys, and the thrown exceptions as values.
* <p>The messages should be the same that were originally passed
* to the invoked send method.
*
* @param failedMessages Map of failed messages as keys and thrown
* exceptions as values
*/
public MailSendException(Map<Object, Exception> failedMessages) {
this(null, null, failedMessages);
}


/**
* Return a Map with the failed messages as keys, and the thrown exceptions
* as values.
* <p>Note that a general mail server connection failure will not result
* in failed messages being returned here: A message will only be
* contained here if actually sending it was attempted but failed.
* <p>The messages will be the same that were originally passed to the
* invoked send method, that is, SimpleMailMessages in case of using
* the generic MailSender interface.
* <p>In case of sending MimeMessage instances via JavaMailSender,
* the messages will be of type MimeMessage.
* <p><b>NOTE:</b> This Map will not be available after serialization.
* Use {@link #getMessageExceptions()} in such a scenario, which will
* be available after serialization as well.
*
* @return the Map of failed messages as keys and thrown exceptions as values
* @see jakarta.mail.internet.MimeMessage
*/
public final Map<Object, Exception> getFailedMessages() {
return this.failedMessages;
}

/**
* Return an array with thrown message exceptions.
* <p>Note that a general mail server connection failure will not result
* in failed messages being returned here: A message will only be
* contained here if actually sending it was attempted but failed.
*
* @return the array of thrown message exceptions,
* or an empty array if no failed messages
*/
public final Exception[] getMessageExceptions() {
return (this.messageExceptions != null ? this.messageExceptions : new Exception[0]);
}


@Override
public String getMessage() {
if (messageExceptions == null || messageExceptions.length == 0) {
return super.getMessage();
} else {
StringBuilder sb = new StringBuilder();
String baseMessage = super.getMessage();
if (baseMessage != null) {
sb.append(baseMessage).append(". ");
}
sb.append("Failed messages: ");
for (int i = 0; i < this.messageExceptions.length; i++) {
Exception subEx = this.messageExceptions[i];
sb.append(subEx.toString());
if (i < this.messageExceptions.length - 1) {
sb.append("; ");
private transient final Map<Object, Exception> failedMessages;

private Exception[] messageExceptions;


/**
* Constructor for MailSendException.
*
* @param msg the detail message
*/
public MailSendException(String msg) {
this(msg, null);
}

/**
* Constructor for MailSendException.
*
* @param msg the detail message
* @param cause the root cause from the mail API in use
*/
public MailSendException(String msg, Throwable cause) {
super(msg, cause);
this.failedMessages = new LinkedHashMap<Object, Exception>();
}

/**
* Constructor for registration of failed messages, with the
* messages that failed as keys, and the thrown exceptions as values.
* <p>The messages should be the same that were originally passed
* to the invoked send method.
*
* @param msg the detail message
* @param cause the root cause from the mail API in use
* @param failedMessages Map of failed messages as keys and thrown
* exceptions as values
*/
public MailSendException(String msg, Throwable cause, Map<Object, Exception> failedMessages) {
super(msg, cause);
this.failedMessages = new LinkedHashMap<Object, Exception>(failedMessages);
this.messageExceptions = failedMessages.values().toArray(new Exception[failedMessages.size()]);
}

/**
* Constructor for registration of failed messages, with the
* messages that failed as keys, and the thrown exceptions as values.
* <p>The messages should be the same that were originally passed
* to the invoked send method.
*
* @param failedMessages Map of failed messages as keys and thrown
* exceptions as values
*/
public MailSendException(Map<Object, Exception> failedMessages) {
this(null, null, failedMessages);
}


/**
* Return a Map with the failed messages as keys, and the thrown exceptions
* as values.
* <p>Note that a general mail server connection failure will not result
* in failed messages being returned here: A message will only be
* contained here if actually sending it was attempted but failed.
* <p>The messages will be the same that were originally passed to the
* invoked send method, that is, SimpleMailMessages in case of using
* the generic MailSender interface.
* <p>In case of sending MimeMessage instances via JavaMailSender,
* the messages will be of type MimeMessage.
* <p><b>NOTE:</b> This Map will not be available after serialization.
* Use {@link #getMessageExceptions()} in such a scenario, which will
* be available after serialization as well.
*
* @return the Map of failed messages as keys and thrown exceptions as values
* @see jakarta.mail.internet.MimeMessage
*/
public final Map<Object, Exception> getFailedMessages() {
return this.failedMessages;
}

/**
* Return an array with thrown message exceptions.
* <p>Note that a general mail server connection failure will not result
* in failed messages being returned here: A message will only be
* contained here if actually sending it was attempted but failed.
*
* @return the array of thrown message exceptions,
* or an empty array if no failed messages
*/
public final Exception[] getMessageExceptions() {
return (this.messageExceptions != null ? this.messageExceptions : new Exception[0]);
}


@Override
public String getMessage() {
if (messageExceptions == null || messageExceptions.length == 0) {
return super.getMessage();
} else {
StringBuilder sb = new StringBuilder();
String baseMessage = super.getMessage();
if (baseMessage != null) {
sb.append(baseMessage).append(". ");
}
sb.append("Failed messages: ");
for (int i = 0; i < this.messageExceptions.length; i++) {
Exception subEx = this.messageExceptions[i];
sb.append(subEx.toString());
if (i < this.messageExceptions.length - 1) {
sb.append("; ");
}
}
return sb.toString();
}
}
return sb.toString();
}
}

@Override
public String toString() {
if (messageExceptions == null || messageExceptions.length == 0) {
return super.toString();
} else {
StringBuilder sb = new StringBuilder(super.toString());
sb.append("; message exceptions (").append(this.messageExceptions.length).append(") are:");
for (int i = 0; i < this.messageExceptions.length; i++) {
Exception subEx = this.messageExceptions[i];
sb.append('\n').append("Failed message ").append(i + 1).append(": ");
sb.append(subEx);
}
return sb.toString();

@Override
public String toString() {
if (messageExceptions == null || messageExceptions.length == 0) {
return super.toString();
} else {
StringBuilder sb = new StringBuilder(super.toString());
sb.append("; message exceptions (").append(this.messageExceptions.length).append(") are:");
for (int i = 0; i < this.messageExceptions.length; i++) {
Exception subEx = this.messageExceptions[i];
sb.append('\n').append("Failed message ").append(i + 1).append(": ");
sb.append(subEx);
}
return sb.toString();
}
}
}

@Override
public void printStackTrace(PrintStream ps) {
if (messageExceptions == null || messageExceptions.length == 0) {
super.printStackTrace(ps);
} else {
ps.println(super.toString() + "; message exception details (" +
this.messageExceptions.length + ") are:");
for (int i = 0; i < this.messageExceptions.length; i++) {
Exception subEx = this.messageExceptions[i];
ps.println("Failed message " + (i + 1) + ":");
subEx.printStackTrace(ps);
}

@Override
public void printStackTrace(PrintStream ps) {
if (messageExceptions == null || messageExceptions.length == 0) {
super.printStackTrace(ps);
} else {
ps.println(super.toString() + "; message exception details (" +
this.messageExceptions.length + ") are:");
for (int i = 0; i < this.messageExceptions.length; i++) {
Exception subEx = this.messageExceptions[i];
ps.println("Failed message " + (i + 1) + ":");
subEx.printStackTrace(ps);
}
}
}
}

@Override
public void printStackTrace(PrintWriter pw) {
if (messageExceptions == null || messageExceptions.length == 0) {
super.printStackTrace(pw);
} else {
pw.println(super.toString() + "; message exception details (" +
this.messageExceptions.length + ") are:");
for (int i = 0; i < this.messageExceptions.length; i++) {
Exception subEx = this.messageExceptions[i];
pw.println("Failed message " + (i + 1) + ":");
subEx.printStackTrace(pw);
}

@Override
public void printStackTrace(PrintWriter pw) {
if (messageExceptions == null || messageExceptions.length == 0) {
super.printStackTrace(pw);
} else {
pw.println(super.toString() + "; message exception details (" +
this.messageExceptions.length + ") are:");
for (int i = 0; i < this.messageExceptions.length; i++) {
Exception subEx = this.messageExceptions[i];
pw.println("Failed message " + (i + 1) + ":");
subEx.printStackTrace(pw);
}
}
}
}


}
Loading

0 comments on commit 10f32d8

Please sign in to comment.