forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to skip logging invalid recipients when sending emails
This commits detects and prevents logging invalid recipients before delegating to the underlying SMTP client, avoiding the overhead of processing attachments for emails that won’t be sent as well as acquiring an unnecessary connection. When the option is enabled (default), the invalid recipients is not logged and not part of the thrown exception.
- Loading branch information
1 parent
fe164e0
commit fa28a69
Showing
10 changed files
with
236 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
extensions/mailer/deployment/src/test/java/io/quarkus/mailer/InvalidEmailTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package io.quarkus.mailer; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.mailer.reactive.ReactiveMailer; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public class InvalidEmailTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(Sender.class)); | ||
|
||
@Inject | ||
MockMailbox mockMailbox; | ||
|
||
@Inject | ||
Sender sender; | ||
|
||
@Test | ||
public void testInvalidTo() { | ||
List<String> to = List.of("[email protected]", "inv [email protected]", "[email protected]"); | ||
List<String> cc = List.of(); | ||
List<String> bcc = List.of(); | ||
Assertions.assertThatThrownBy(() -> sender.send(to, cc, bcc).await().atMost(Duration.ofSeconds(5))) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("Unable to send an email, an email address is invalid") | ||
.hasMessageNotContaining("@"); | ||
Assertions.assertThat(mockMailbox.getTotalMessagesSent()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void testInvalidCC() { | ||
List<String> cc = List.of("[email protected]", "inv [email protected]", "[email protected]"); | ||
List<String> to = List.of(); | ||
List<String> bcc = List.of(); | ||
Assertions.assertThatThrownBy(() -> sender.send(to, cc, bcc).await().atMost(Duration.ofSeconds(5))) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("Unable to send an email, an email address is invalid") | ||
.hasMessageNotContaining("@"); | ||
Assertions.assertThat(mockMailbox.getTotalMessagesSent()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void testInvalidBCC() { | ||
List<String> bcc = List.of("[email protected]", "inv [email protected]", "[email protected]"); | ||
List<String> to = List.of(); | ||
List<String> cc = List.of(); | ||
Assertions.assertThatThrownBy(() -> sender.send(to, cc, bcc).await().atMost(Duration.ofSeconds(5))) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("Unable to send an email, an email address is invalid") | ||
.hasMessageNotContaining("@"); | ||
Assertions.assertThat(mockMailbox.getTotalMessagesSent()).isEqualTo(0); | ||
} | ||
|
||
@Singleton | ||
static class Sender { | ||
|
||
@Inject | ||
ReactiveMailer mailer; | ||
|
||
Uni<Void> send(List<String> to, List<String> cc, List<String> bcc) { | ||
Mail mail = new Mail() | ||
.setTo(to) | ||
.setCc(cc) | ||
.setBcc(bcc) | ||
.setSubject("Test") | ||
.setText("Hello!"); | ||
return mailer.send(mail); | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
extensions/mailer/deployment/src/test/java/io/quarkus/mailer/LoggedInvalidEmailTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package io.quarkus.mailer; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.mailer.reactive.ReactiveMailer; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public class LoggedInvalidEmailTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(Sender.class)) | ||
.overrideRuntimeConfigKey("quarkus.mailer.log-invalid-recipients", "true"); | ||
|
||
@Inject | ||
MockMailbox mockMailbox; | ||
|
||
@Inject | ||
Sender sender; | ||
|
||
@Test | ||
public void testInvalidTo() { | ||
List<String> to = List.of("[email protected]", "inv [email protected]", "[email protected]"); | ||
List<String> cc = List.of(); | ||
List<String> bcc = List.of(); | ||
Assertions.assertThatThrownBy(() -> sender.send(to, cc, bcc).await().atMost(Duration.ofSeconds(5))) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("Unable to send an email") | ||
.hasStackTraceContaining("inv [email protected]") | ||
.hasMessageNotContaining("@text.io"); | ||
Assertions.assertThat(mockMailbox.getTotalMessagesSent()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void testInvalidCC() { | ||
List<String> cc = List.of("[email protected]", "inv [email protected]", "[email protected]"); | ||
List<String> to = List.of(); | ||
List<String> bcc = List.of(); | ||
Assertions.assertThatThrownBy(() -> sender.send(to, cc, bcc).await().atMost(Duration.ofSeconds(5))) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("Unable to send an email") | ||
.hasStackTraceContaining("inv [email protected]") | ||
.hasMessageNotContaining("@text.io"); | ||
Assertions.assertThat(mockMailbox.getTotalMessagesSent()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void testInvalidBCC() { | ||
List<String> bcc = List.of("[email protected]", "inv [email protected]", "[email protected]"); | ||
List<String> to = List.of(); | ||
List<String> cc = List.of(); | ||
Assertions.assertThatThrownBy(() -> sender.send(to, cc, bcc).await().atMost(Duration.ofSeconds(5))) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("Unable to send an email") | ||
.hasStackTraceContaining("inv [email protected]") | ||
.hasMessageNotContaining("@text.io"); | ||
Assertions.assertThat(mockMailbox.getTotalMessagesSent()).isEqualTo(0); | ||
} | ||
|
||
@Singleton | ||
static class Sender { | ||
|
||
@Inject | ||
ReactiveMailer mailer; | ||
|
||
Uni<Void> send(List<String> to, List<String> cc, List<String> bcc) { | ||
Mail mail = new Mail() | ||
.setTo(to) | ||
.setCc(cc) | ||
.setBcc(bcc) | ||
.setSubject("Test") | ||
.setText("Hello!"); | ||
return mailer.send(mail); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters