Micronaut and Java Mail API Micronaut and Java Mail API docker docker

Micronaut and Java Mail API


Not sure it might be enough, but you could try to initialize com.sun.mail.util.MailLogger at build-time passing the additional--initialize-at-build-time=com.sun.mail.util.MailLogger as argument

This will preload the class before runtime, which may solve or not the compilation issue

On a side-note, I guess you can chain your runtime initialization arguments in a single comma-separated list, i.e.

--initialize-at-runtime=io.micronaut.views.thymeleaf.ThymeleafFactory,io.micronaut.views.thymeleaf.ThumeleafViewsRenderer, ...


After trying a few different things I'm posting what finally worked for us so that others might be helped.

Firstly we upgraded to the latest version of micronaut (2.3.4 at the moment) and also we used the latest version of com.sun.mail:jakarta.mail (2.0.0 at the moment).Secondly our nativeImage args were the following (omitting some for other dependencies):

--report-unsupported-elements-at-runtime-H:+ReportExceptionStackTraces-H:-DeleteLocalSymbols-H:+PreserveFramePointer-H:IncludeResources=META-INF/mailcap-H:IncludeResources=META-INF/mailcap.default-H:IncludeResources=META-INF/javamail.default.address.map-H:IncludeResources=META-INF/javamail.charset.map-H:IncludeResources=META-INF/javamail.default.providers-H:IncludeResources=META-INF/services/javax.mail.Provider

Then we had to mark a few classes for introspection using @TypeHint:

@TypeHit({  SMTPTransport.class,  MimeMultipart.class,  MailcapCommandMap.class,  text_html.class,  multipart_mixed.class,  handler_base.class,  image_gif.class,  image_jpeg.class,  message_rfc822.class,  text_xml.class,  text_plain.class})

Lastly, we had an SMTP Sender bean on which we had to configure mailcap (so perhaps the resource inclusion doesn't work that well):

@PostConstructpublic void initialize() {  MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();  mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");  mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");  mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");  mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");  mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");  CommandMap.setDefaultCommandMap(mc);}

With all those in place, we were able to create a native image that could send emails without any issues.