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

Debug mode that logs but doesn't send emails? #64

Closed
Addvilz opened this issue Feb 24, 2017 · 10 comments
Closed

Debug mode that logs but doesn't send emails? #64

Addvilz opened this issue Feb 24, 2017 · 10 comments

Comments

@Addvilz
Copy link

Addvilz commented Feb 24, 2017

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.

@bbottema
Copy link
Owner

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.

@Addvilz
Copy link
Author

Addvilz commented Feb 25, 2017

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.

@Addvilz
Copy link
Author

Addvilz commented Feb 25, 2017

@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.

@bbottema
Copy link
Owner

bbottema commented Feb 25, 2017

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...

@bbottema bbottema changed the title Transport strategy void? Debug mode that logs but doesn't send emails? Feb 26, 2017
@bbottema bbottema added this to the 4.2.1 milestone Feb 26, 2017
@bbottema
Copy link
Owner

bbottema commented Feb 26, 2017

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.

@bbottema
Copy link
Owner

bbottema commented Mar 12, 2017

mailer.setTransportModeLoggingOnly(true);

Or set property

simplejavamail.transport.mode.logging.only=true

@bbottema
Copy link
Owner

Released in v4.2.0.

@Addvilz
Copy link
Author

Addvilz commented Mar 13, 2017

Absolutely amazing! Thanks!

@mordmord
Copy link

mordmord commented Oct 15, 2018

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

@bbottema
Copy link
Owner

bbottema commented Oct 16, 2018

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

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