Reputation: 273
I am using commons-email 1.6.0 to send emails and I have been encountering a weird issue where the emails are only sent successfully when the application is launched from netbeans or using java 18.0.2. If I use a different java version I am getting this error. I can't figure out why it only works using java 18.0.2, does anyone have an idea?
public boolean sendMailHTML(String subject, String msgBody, String[] destinataires, String[] copies, String[] blindCopies) {
boolean result = false;
try {
HtmlEmail email = new HtmlEmail();
email.setHostName(Config.getHostName());
email.setSmtpPort(Config.getSmtpPort());
if (Config.isSslEnabled()) {
email.setSSLOnConnect(true);
email.setSslSmtpPort(String.valueOf(Config.getSmtpPort()));
}
if (Config.isTlsEnabled()) {
email.setStartTLSEnabled(true);
email.setStartTLSRequired(true);
}
if (Config.isDebugEnabled()) {
email.setDebug(true);
BaseLoggerNotifier.logInfo(ILogger.levelErreur.debug, "Mail Configuration Loaded: \n" + Config.toString());
}
String login = Config.getLogin();
String password = Config.getPassword();
if (login != null && !login.equals("")) {
email.setAuthentication(login, password);
}
email.setFrom(Config.getFrom());
for (String dest : destinataires) {
if (!dest.equals("")) {
email.addTo(dest);
}
}
if (copies != null) {
for (String copie : copies) {
if (!copie.equals("")) {
email.addCc(copie);
}
}
}
if (blindCopies != null) {
for (String bCopie : blindCopies) {
if (!bCopie.equals("")) {
email.addBcc(bCopie);
}
}
}
email.setSubject(subject);
if (msgBody != null && !msgBody.equals("")) {
email.setHtmlMsg(msgBody);
} else {
email.setHtmlMsg("<HTML></HTML>");
}
email.send();
result = true;
} catch (EmailException ex) {
BaseLoggerNotifier.logInfo(ILogger.levelErreur.erreur, getClass().getName(), ex, "sendMailHTML()");
}
return result;
}
DEBUG: Jakarta Mail version 1.6.7 DEBUG: successfully loaded resource: /META-INF/javamail.default.providers DEBUG: Tables of loaded providers DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle]} DEBUG: Providers Listed By Protocol: {imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle]} DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: need username and password for authentication DEBUG SMTP: protocolConnect returning false, host=####, user=####, password= DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "#####", port 465, isSSL false 2024-06-07 10:43:25,975 INFO [exceptions] mailtransfert.MailSender -> (org.apache.commons.mail.EmailException) Sending the email to the following server failed : ######:465 -> sendMailHTML() 2024-06-07 10:43:25,975 INFO [exceptions] org.apache.commons.mail.Email.sendMimeMessage(Email.java:1298) 2024-06-07 10:43:25,975 INFO [exceptions] org.apache.commons.mail.Email.send(Email.java:1282) 2024-06-07 10:43:25,975 INFO [exceptions] mailtransfert.MailSender.sendMailHTML(MailSender.java:110) 2024-06-07 10:43:25,976 INFO [exceptions] calcul.Calcul_Alarmes.sendMailOuverture(Calcul_Alarmes.java:412) 2024-06-07 10:43:25,976 INFO [exceptions] calcul.Calcul_Alarmes.runAction(Calcul_Alarmes.java:367) 2024-06-07 10:43:25,976 INFO [exceptions] framework.prototypes.ICalcul.run(ICalcul.java:142) 2024-06-07 10:43:25,976 INFO [exceptions] java.lang.Thread.run(Unknown Source)
After launching with -Djavax.net.debug=all
I can see the exception:
***
calcul.Calcul_Alarmes, handling exception: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
%% Invalidated: [Session-12, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256]
calcul.Calcul_Alarmes, SEND TLSv1.2 ALERT: fatal, description = internal_error
calcul.Calcul_Alarmes, WRITE: TLSv1.2 Alert, length = 2
[Raw write]: length = 7
0000: 15 03 03 00 02 02 50 ......P
calcul.Calcul_Alarmes, called closeSocket()
2024-06-07 11:42:22,388 ERROR [general] mailtransfert.MailSender -> (org.apache.commons.mail.EmailException) Sending the email to the following server failed : #### -> sendMailHTML()
Upvotes: 0
Views: 263
Reputation: 273
I was able to fix by adding these two jvm properties, it seems that the jdk 18.0.2 had the necessary truststore which is why it was working. After adding these two properties it worked.
javax.net.ssl.trustStore=NUL
javax.net.ssl.trustStoreType=Windows-ROOT
This answer is really helpful Import Windows certificates to Java
Upvotes: 1