Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: set a maximum email size on Mailer level which throws EmailToBig exception when exceeded #427

Closed
jpnffigueiredo opened this issue Dec 13, 2022 · 5 comments

Comments

@jpnffigueiredo
Copy link

jpnffigueiredo commented Dec 13, 2022

Hi,

Thanks for this amazing java mailing library.

I would like to ask you if is possible to have any (efficient) way to calculate final message size?

In some scenarios we are checking message size before send it.

Currently, we are doing this by converting Email to MimeMessage and then write it an output stream.

Some like this:

final EmailPopulatingBuilder builder = ....;
final Email message = builder.buildEmail();
try (final CountingOutputStream out = new CountingOutputStream(NullOutputStream.NULL_OUTPUT_STREAM)) {
    EmailConverter.emailToMimeMessage(message).writeTo(out);
    if (out.getByteCount() > limit) {
        //Message bigger than limit
    }
}

// Send email
mailer.sendMail(message);

With this solution we are creating MimeMessage twice: one just to check size and other inside mailer.sendEmail.

Do you have any suggestion to calculate this or is possible to (somehow) cache MimeMessage or allow mailer.send(mimeMessage)? Just saying some options.

Thanks in advance.

Best regards,
João Figueiredo.

@bbottema
Copy link
Owner

Is it important to get the size before sending the email or is it fine to get it after sending?

@bbottema
Copy link
Owner

Closing until I get more details.

@jpnffigueiredo
Copy link
Author

jpnffigueiredo commented Jan 24, 2023

Hi,

Sorry for the delay.

For current use case we need it before sending email. We want to check message size because we already know the smtp server message limit.

After sending the message, maybe we could catch exception and check message. We are trying to prevent send message that will fail due to message size.

Thanks.

@bbottema
Copy link
Owner

bbottema commented Jan 24, 2023

Ahh... that's a good point, actually. I have to think about this though, it's not trivial as the conversion to MimeMessage happens at the end of the chain, in case of batch-module even after a Transport was claimed from the SMTP connection pool.

I think a specific exception might do the trick. I'll have a look.

@bbottema bbottema reopened this Jan 24, 2023
@bbottema bbottema changed the title Email message size Feature: set a maximum email size on Mailer level which throws EmailToBig exception Jan 24, 2023
@bbottema bbottema added this to the 7.8.0 milestone Jan 24, 2023
@bbottema bbottema changed the title Feature: set a maximum email size on Mailer level which throws EmailToBig exception Feature: set a maximum email size on Mailer level which throws EmailToBig exception when exceeded Jan 24, 2023
bbottema added a commit that referenced this issue Jan 24, 2023
@bbottema
Copy link
Owner

Feature released in 7.8.0. You can now set the maximum Email size like this:

Mailer mailer = MailerBuilder
      .(..)
      .withMaximumEmailSize(4) // 4 bytes, that's not much
      .buildMailer();

try {
	mailer.sendMail(emailBiggerThan4Bytes)
} catch(Exception e) {
	// cause: EmailTooBigException
	// msg: "Email size of 277 bytes exceeds maximum allowed size of 4 bytes"
	e.getCause().printStrackTrace();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants