'Can't send mail using apache commons mail when smtp appender for log4j is configured
I have a working code that creates and sends an org.apache.commons.mail.HtmlEmail
(version 1.5), but that same code doesn't word when I have SMTPAppender configured for log4j2 (version 2.1), if I remove the SMTPAppender, my code runs again. This is the log from when SMTPAppender is configured and my code fails:
Loading javamail.default.providers from jar:file:/home/stirr/.m2/repository/com/sun/mail/javax.mail/1.5.6/javax.mail-1.5.6.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Oracle, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Oracle, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Oracle, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Oracle, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Oracle, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Oracle, version=null
DEBUG: getProvider() returning provider protocol=smtps; type=javax.mail.Provider$Type@240a1e4d; class=com.sun.mail.smtp.SMTPSSLTransport; vendor=Oracle
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 465, isSSL true
mai. 09, 2022 10:36:01 AM nfce.receita.nfe.envio.SendEmail sendXMLsAndReports
SEVERE: null
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
Pay attention where it says:
"trying to connect to host "localhost", port 465, isSSL true"
When I remove SMTPAppender, this message change and try to connecto to the correct host:
"trying to connect to host "smtp.gmail.com", port 465, isSSL false"
EDIT 1:
This is the relevant part of my log4j2.xml and the class that sends my email:
<Configuration status="INFO">
<Appenders>
<SMTP name="Mail" subject="My Subject"
to="[email protected]"
from="[email protected]"
smtpUsername="[email protected]" smtpPassword="mypasswd"
smtpHost="smtp.gmail.com" smtpPort="465" bufferSize="50"
smtpProtocol="smtps" smtpDebug="true">
</SMTP>
<Async name="Async">
<AppenderRef ref="Mail"/>
</Async>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Async"/>
</Root>
</Loggers>
</Configuration>
ps: I omitted console appender and file appender to focus on smtp appender configuration
org.apache.commons.mail.HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.gmail.com");
email.setAuthentication("[email protected]", "mypasswd");
email.setSmtpPort(465);
email.setSSLOnConnect(true);
email.setStartTLSEnabled(true);
email.setFrom("[email protected]", "ERROR SISEMAIL");
email.addTo("[email protected]");
email.setSubject("My Subject");
email.setHtmlMsg("My message");
email.setCharset("UTF-8");
email.setDebug(true);
if (attachs != null) {
for (File attach : attachs) {
if (attach != null) {
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(attach.getAbsolutePath());
attachment.setName(attach.getName());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
email.attach(attachment);
}
}
}
email.send();
EDIT 2:
I have made a project that illustrate the error: https://github.com/lleandroo93/sof-q72173964
The problem seems to be related to one local lib that I have no control and is essential to the project (cannot be replaced or removed), please follow the steps described in the README of the project
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|