bbottema/simple-java-mail

Debug mode that logs but doesn't send emails?

Addvilz opened this issue · 10 comments

Is it possible to configure mailer to only "pretend" that the mail is sent? TransportStrategy.VOID for example?

Could be rather useful for testing, to not need a smtp server running somewhere.

That doesn't make sense to me. You should probably be use a MailSender mock using something like EasyMock or Mockito. You don't want to test whether Simple Java Mail is working properly, right.

Sorry, it came out a little wrong. With testing I did not mean testing per-se, like JUnit tests, I meant, for example, in QA environment, it would be preferable to log messages instead of actually sending them. This is not limited to this example either, there are example use cases where "disabling" sending of the email is needed.

Our example use case is staging environment. We would like to log the emails instead of actually sending them, but there are two options then - every time sendMail is invoked, in every place check the current environment, or replace mailer with our own class, or use mock SMTP server. Checking for env in every usage of mailer is tedious task at best. Having a mock SMTP isn't a simple task either. Unfortunately sendMail is final and we can't override it. We could in theory create our own interface and two delegates to the mailer, but it introduces an abstraction I would rather avoid having.

@bbottema do you perhaps have something in mind on how you would prefer to see this in code? I can implement it and send a PR.

I'm still not sure about the use case validity, because if you want to check environment configuration or integration I would rather configure an actual SMTP server that doesn't relay. However, due to popular demand I will add this feature anyway.

I would probably create a Session subclass that overrides .getTransport() to return a dummy that only logs when transport strategy is TransportStrategy.LOGGING_ONLY. This way Simple Java Mail remains unchanged, while the actual transport becomes optional. However, this won't work for session objects passed in from outside. I'll have to think about it some more...

Implemented, but not ready for release yet.

Solution is simple: I've added it as a config property. Either configure through properties file, or set directly on the Mailer. I'll update the documentation somewhere next week.

mailer.setTransportModeLoggingOnly(true);

Or set property

simplejavamail.transport.mode.logging.only=true

Released in v4.2.0.

Absolutely amazing! Thanks!

@bbottema Is this feature possible in v5.0+?? Thanks!

@mordmord It's been available since 4.2.0, so yes it's there :)