Exceptions cause error-level logging in addition to rethrowing the exception, but should just include the message in a custom exception
SantoshDeepak1 opened this issue · 7 comments
When the SMTP credentials are wrong and at the time of sending email library logs the error itself instead propagating to caller.
org.simplejavamail.mailer.internal.AbstractProxyServerSyncingClosure
Failed to send email:
736007253.1.1625220323405@LP-IND-PUN-59.SSGCORP.local
Closing until new information comes up.
@bbottema I'm having the same issue using version 7.0.1
This issue creates unnecessary error reports that is created for error level logs in our application.
Here is an example to reproduce this. You just have to set an unknown host:
package mail;
import org.simplejavamail.MailException;
import org.simplejavamail.api.email.Email;
import org.simplejavamail.api.mailer.Mailer;
import org.simplejavamail.api.mailer.config.TransportStrategy;
import org.simplejavamail.email.EmailBuilder;
import org.simplejavamail.mailer.MailerBuilder;
public class SimpleMailTest {
public static void main(String[] args) {
try {
String host = "foo.fooexample.com";
int port = 587;
String username = "username";
String password = "password";
Mailer mailer = MailerBuilder.withSMTPServer(host, port, username, password)
.withTransportStrategy(TransportStrategy.SMTP_TLS)
.withDebugLogging(false)
.buildMailer();
// mailer.testConnection();
Email email = EmailBuilder.startingBlank()
.from("Michel Baker", "m.baker@mbakery.com")
.to("mom", "jean.baker@hotmail.com")
.to("dad", "StevenOakly1963@hotmail.com")
.withSubject("My Bakery is finally open!")
.withPlainText("Mom, Dad. We did the opening ceremony of our bakery!!!")
.buildEmail();
mailer.sendMail(email, false);
} catch (MailException e) {
System.err.println("########################## Caught MailException");
e.printStackTrace();
} catch (Throwable e) {
System.err.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ Caught Throwable");
e.printStackTrace();
}
}
}Here is the output. Notice that AbstractProxyServerSyncingClosure logs an error before the Catch stage
2022-01-25 11:06:12.212 ERROR o.s.m.i.AbstractProxyServerSyncingClosure [main] Failed to send email:
<1373810119.0.1643105172187@WIN10PC>
########################## Caught MailException
org.simplejavamail.mailer.internal.MailerException: Third party error
at org.simplejavamail.mailer.internal.SendMailClosure.executeClosure(SendMailClosure.java:91)
at org.simplejavamail.mailer.internal.AbstractProxyServerSyncingClosure.run(AbstractProxyServerSyncingClosure.java:56)
at org.simplejavamail.mailer.internal.MailerImpl.sendMail(MailerImpl.java:345)
at mail.SimpleMailTest.main(SimpleMailTest.java:34)
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: foo.fooexample.com, 587; timeout 60000;
nested exception is:
java.net.UnknownHostException: foo.fooexample.com
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2210)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:722)
at jakarta.mail.Service.connect(Service.java:364)
at jakarta.mail.Service.connect(Service.java:222)
at jakarta.mail.Service.connect(Service.java:171)
at org.simplejavamail.mailer.internal.util.TransportRunner.runOnSessionTransport(TransportRunner.java:67)
at org.simplejavamail.mailer.internal.util.TransportRunner.sendMessage(TransportRunner.java:47)
at org.simplejavamail.mailer.internal.SendMailClosure.executeClosure(SendMailClosure.java:82)
... 3 more
Caused by: java.net.UnknownHostException: foo.fooexample.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:607)
at com.sun.mail.util.WriteTimeoutSocket.connect(WriteTimeoutSocket.java:91)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:333)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:214)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2160)
... 10 more
I still don't see the problem. The library does not log the actual error and does propagate it to the caller. The only error-level detail the library logs in addition to that is the following:
12:09:39 [main] ERROR AbstractProxyServerSyncingClosure - Failed to send email:
1579280783.0.1643108979695@Cypher.mshome.net
Is this what you are referring to?
yes. It is that extra error log line I am referring to.
In my example it is:
2022-01-25 11:06:12.212 ERROR o.s.m.i.AbstractProxyServerSyncingClosure [main] Failed to send email:
<1373810119.0.1643105172187@WIN10PC>
I agree, it is not needed if we're rethrowing the exception anyway. Might as well wrap both the cause and the logged message in new exception.
Fix released in 7.0.2
