-
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#427: Added support for maximum email size validation with a possible…
… EmailTooBig exception as result (as cause)
- Loading branch information
Showing
11 changed files
with
180 additions
and
13 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
modules/core-module/src/main/java/org/simplejavamail/api/mailer/EmailTooBigException.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,14 @@ | ||
package org.simplejavamail.api.mailer; | ||
|
||
import static java.lang.String.format; | ||
|
||
/** | ||
* Thrown when an email (as MimeMessage) is bigger than the maximum allowed size. | ||
* | ||
* @see MailerGenericBuilder#withMaximumEmailSize(int) | ||
*/ | ||
public class EmailTooBigException extends RuntimeException { | ||
public EmailTooBigException(final long emailSize, final long maximumEmailSize) { | ||
super(format("Email size of %s bytes exceeds maximum allowed size of %s bytes", emailSize, maximumEmailSize)); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package org.simplejavamail.mailer; | ||
|
||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.Session; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.val; | ||
import org.jetbrains.annotations.NotNull; | ||
|
@@ -14,7 +15,10 @@ | |
import org.simplejavamail.api.email.OriginalSmimeDetails.SmimeMode; | ||
import org.simplejavamail.api.email.Recipient; | ||
import org.simplejavamail.api.internal.smimesupport.model.PlainSmimeDetails; | ||
import org.simplejavamail.api.mailer.CustomMailer; | ||
import org.simplejavamail.api.mailer.EmailTooBigException; | ||
import org.simplejavamail.api.mailer.Mailer; | ||
import org.simplejavamail.api.mailer.config.OperationalConfig; | ||
import org.simplejavamail.converter.EmailConverter; | ||
import org.simplejavamail.email.EmailBuilder; | ||
import org.simplejavamail.email.internal.InternalEmailPopulatingBuilder; | ||
|
@@ -41,6 +45,7 @@ | |
import static java.util.Optional.ofNullable; | ||
import static java.util.stream.Collectors.toList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.data.MapEntry.entry; | ||
import static org.simplejavamail.api.email.ContentTransferEncoding.BIT7; | ||
import static org.simplejavamail.converter.EmailConverter.mimeMessageToEmail; | ||
|
@@ -541,4 +546,59 @@ private void assertAttachmentMetadata(AttachmentResource embeddedImg, String mim | |
assertThat(embeddedImg.getDataSource().getContentType()).isEqualTo(mimeType); | ||
assertThat(embeddedImg.getName()).isEqualTo(filename); | ||
} | ||
|
||
@Test | ||
public void testMaximumEmailSize() { | ||
val mailer = MailerBuilder | ||
.withSMTPServer("localhost", SERVER_PORT, USERNAME, PASSWORD) | ||
.withMaximumEmailSize(4) | ||
.buildMailer(); | ||
|
||
sendAndVerifyEmailTooBigException(mailer); | ||
} | ||
|
||
@Test | ||
public void testMaximumEmailSize_CustomMailer() { | ||
val mailer = MailerBuilder | ||
.withCustomMailer(new CustomMailer() { | ||
@Override | ||
public void testConnection(@NotNull OperationalConfig operationalConfig, @NotNull Session session) { | ||
throw new RuntimeException("should reach here"); | ||
} | ||
|
||
@Override | ||
public void sendMessage(@NotNull OperationalConfig operationalConfig, @NotNull Session session, @NotNull Email email, @NotNull MimeMessage message) { | ||
throw new RuntimeException("should reach here"); | ||
} | ||
}) | ||
.withMaximumEmailSize(4) | ||
.buildMailer(); | ||
|
||
sendAndVerifyEmailTooBigException(mailer); | ||
} | ||
|
||
@Test | ||
public void testMaximumEmailSize_DontSendOnlyLog() { | ||
val mailer = MailerBuilder | ||
.withTransportModeLoggingOnly() | ||
.withMaximumEmailSize(4) | ||
.buildMailer(); | ||
|
||
sendAndVerifyEmailTooBigException(mailer); | ||
} | ||
|
||
private static void sendAndVerifyEmailTooBigException(Mailer mailer) { | ||
val email = EmailBuilder.startingBlank() | ||
.withPlainText("non empty text") | ||
.withSubject("email size test") | ||
.from("[email protected]") | ||
.to("[email protected]") | ||
.buildEmail(); | ||
|
||
assertThatThrownBy(() -> mailer.sendMail(email)) | ||
.hasMessageStartingWith("Failed to send email [ID:") | ||
.getCause() | ||
.isInstanceOf(EmailTooBigException.class) | ||
.hasMessageContaining("bytes exceeds maximum allowed size of 4 bytes"); | ||
} | ||
} |
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