Skip to content

Commit 6acfc71

Browse files
committed
#148: Added support for handling async results including exception
1 parent baf46b4 commit 6acfc71

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

‎src/main/java/org/simplejavamail/mailer/Mailer.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.EnumSet;
2525
import java.util.Map;
2626
import java.util.Properties;
27+
import java.util.concurrent.Future;
2728

2829
import static java.lang.String.format;
2930
import static org.simplejavamail.internal.util.MiscUtil.valueNullOrEmpty;
@@ -236,10 +237,11 @@ public final void sendMail(final Email email) {
236237
* @see MailSender#send(Email, boolean)
237238
* @see #validate(Email)
238239
*/
239-
public final synchronized void sendMail(final Email email, @SuppressWarnings("SameParameterValue") final boolean async) {
240+
public final synchronized Future<?> sendMail(final Email email, @SuppressWarnings("SameParameterValue") final boolean async) {
240241
if (validate(email)) {
241-
mailSender.send(email, async);
242+
return mailSender.send(email, async);
242243
}
244+
throw new AssertionError("Email not valid, but no MailException was thrown for it");
243245
}
244246

245247
/**

‎src/main/java/org/simplejavamail/mailer/internal/mailsender/MailSender.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Properties;
2525
import java.util.concurrent.ExecutorService;
2626
import java.util.concurrent.Executors;
27+
import java.util.concurrent.Future;
2728
import java.util.concurrent.Phaser;
2829

2930
import static java.lang.String.format;
@@ -177,10 +178,11 @@ private static AnonymousSocks5Server configureSessionWithProxy(@Nonnull final Pr
177178
* @param email The information for the email to be sent.
178179
* @param async If false, this method blocks until the mail has been processed completely by the SMTP server. If true, a new thread is started to
179180
* send the email and this method returns immediately.
181+
* @return A {@link Future} or null if not <em>async</em>.
180182
* @throws MailException Can be thrown if an email isn't validating correctly, or some other problem occurs during connection, sending etc.
181183
* @see Executors#newFixedThreadPool(int)
182184
*/
183-
public final synchronized void send(final Email email, final boolean async) {
185+
public final synchronized Future<?> send(final Email email, final boolean async) {
184186
/*
185187
we need to track even non-async emails to prevent async emails from shutting down
186188
the proxy bridge server (or connection pool in async mode) while a non-async email is still being processed
@@ -196,7 +198,7 @@ the proxy bridge server (or connection pool in async mode) while a non-async ema
196198
executor = Executors.newFixedThreadPool(operationalConfig.getThreadPoolSize());
197199
}
198200
configureSessionWithTimeout(session, operationalConfig.getSessionTimeout());
199-
executor.execute(new Runnable() {
201+
return executor.submit(new Runnable() {
200202
@Override
201203
public void run() {
202204
sendMailClosure(session, email);
@@ -209,6 +211,7 @@ public String toString() {
209211
});
210212
} else {
211213
sendMailClosure(session, email);
214+
return null;
212215
}
213216
}
214217

0 commit comments

Comments
 (0)