bbottema / simple-java-mail

Simple API, Complex Emails (Jakarta Mail smtp wrapper)

Home Page:http://www.simplejavamail.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: `withEmailDefaults` and `withEmailOverrides` does not work with `CustomMailer`

morki opened this issue · comments

It is simply ignored.

Most defaults are set when creating the Email, but overrides happen during sending as well as the defaults for smime/dkim. Those will fail currently.

However, I'm currently working on an overhaul regarding defaults and overrides. And this will be addressed.

Thank you for your response. Related issue I am facing is that even with CustomMailer, email is enforced to have From, but it is irrelevant for custom mailer. I thought it can be solved with withEmailOverrides and setting "fake" from address in one place, but it does not work.

I will be happy if in future, it can be addressed somehow. Thank you very much for this awesome library :)

Btw, as a workaround you can disable the completeness validation using mailerBuilder.disablingAllClientValidation(true).

I gotta ask though, why is From not relevant in your use case? For the purpose of sending emails, in whatever form, according to RFC 5322 the From header is mandatory.

I have a logging CustomMailer implementation when there is no SMTP configuration in application.
In this case, FROM is irrelevant and also there is no source for it.

An example in Kotlin:

val mailer = when (smtpUrl) {
    null -> MailerBuilder
        .withCustomMailer(LoggingMailer)
        .disablingAllClientValidation(true)
        .clearEmailValidator()
        .buildMailer()

    else -> MailerBuilder
        .withSMTPServer(smtpUrl.host, smtpUrl.port, smtpUrl.user, smtpUrl.password)
        .withTransportStrategy(TransportStrategy.valueOf(smtpUrl.protocol.name.uppercase()))
        .withSessionTimeout(5000)
        .withEmailOverrides(
            EmailBuilder.startingBlank()
                .from(smtpUrl.parameters.getOrFail("from"))
                .buildEmail()
        )
        .buildMailer()
}!!

I see. In that case, does .disablingAllClientValidation(true) cover your use case appropriately?

Yes, thank you, it is working perfectly now :)

The only little improvement for readabolity would be to create an override of .withCustomMailer to accept lambda or maybe make empty default implementation of testConnection in CustomMailer.

But i know it is just cosmetic and readability, so just to note you. Thank you very much for this library :)

Oh sorry, the solution with disableAllClientValidation(true) does not work. I only forget to remove my current workaround (setting manualy .from("this@is.workaround") from all my e-mail builders.

It throws: Email is not valid: missing sender. Provide with emailBuilder.from(...)

Ahh indeed, I just checked the code and the completeness check is always performed. I will disable this along with the other validations in case of .disableAllClientValidation(true). Expect a release soon.

Thank you very much :)

I released 8.0.0, which now should always apply defaults and overrides in all scenarios. Please let me know if you run into any issue. Note, I'm still in the process of updating the documentation on simplejavamail.org.