Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions cause error-level logging in addition to rethrowing the exception, but should just include the message in a custom exception #329

Closed
SantoshDeepak1 opened this issue Jul 2, 2021 · 7 comments

Comments

@SantoshDeepak1
Copy link

SantoshDeepak1 commented Jul 2, 2021

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

@bbottema
Copy link
Owner

bbottema commented Jul 2, 2021

I'm not sure what you mean, but the exception is thrown again:

image

@bbottema
Copy link
Owner

Closing until new information comes up.

@martonsz
Copy link

martonsz commented Jan 25, 2022

@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

@bbottema
Copy link
Owner

bbottema commented Jan 25, 2022

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?

@martonsz
Copy link

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>

@bbottema bbottema changed the title Log Exception issue Exceptions cause error-level logging in addition to rethrowing the exception, but should just include the message in a custom exception Jan 25, 2022
@bbottema
Copy link
Owner

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.

@bbottema bbottema added this to the 7.0.2 milestone Jan 25, 2022
bbottema added a commit that referenced this issue Jan 25, 2022
@bbottema
Copy link
Owner

Fix released in 7.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants