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

Simple Java Mail should optionally be able to read properties from a Spring context #63

Closed
bbottema opened this issue Jan 27, 2017 · 6 comments

Comments

@bbottema
Copy link
Owner

It should be possible to optionally include a Spring bean from Simple Java Mail that can read properties from a Spring context and use these to configure Simple Java Mail (using the ConfigLoader).

@bbottema
Copy link
Owner Author

bbottema commented Jan 28, 2017

Released in 4.1.3.

To enable Spring properties with Simple Java Mail (example using Java config):

@Component // (or @Configuration)
@Import(SimpleJavaMailSpringSupport.class)
public class YourBean {
	
	@Autowired // (or create your own instance, as long as Spring finished processing SimpleJavaMailSpringSupport as a bean)
	private Mailer mailer;
}

Then if you are using for example spring profiles, you might have something like the following:

application.properties

simplejavamail.javaxmail.debug=true
simplejavamail.smtp.host=test.your-smtp.com
simplejavamail.smtp.port=25
simplejavamail.transportstrategy=
simplejavamail.smtp.username=
simplejavamail.smtp.password=

application-production.properties

simplejavamail.javaxmail.debug=false
simplejavamail.transportstrategy=SMTP_SSL
simplejavamail.smtp.host=production.your-smtp.com
simplejavamail.smtp.port=443
simplejavamail.smtp.username=<username>
simplejavamail.smtp.password=<password>
simplejavamail.proxy.host=production.your-proxy.com
simplejavamail.proxy.port=12345
simplejavamail.proxy.username=<proxy-username>
simplejavamail.proxy.password=<proxy-password>

The regular simplejavamail.properties is also still process if provided and properties are overwritten as provided.

@Flashky
Copy link

Flashky commented Dec 6, 2018

What about encrypting the password?
I really love the feature of reading the mailer configuration from the application.properties / yaml, but I find annoying that I cannot make my password encrypted in any way.

@bbottema
Copy link
Owner Author

bbottema commented Dec 6, 2018

Good point! What about Jasypt?

@Flashky
Copy link

Flashky commented Dec 6, 2018

I think I have read about that API before. I will try to use it and I'll let you know if it does work properly with SimpleJavaMail. Thank you. :)

@Flashky
Copy link

Flashky commented Dec 7, 2018

Hi bbottema!

I just wanted to share with you my discoveries, just in case you want to share the knowledge in the FAQ so other people know that it can be done easily. :)

I have a Spring Boot application, in order to make Jasypt work with the SimpleJavaMail password I just needed to do the following.

Encrypting the password with Jasypt

The first thing you would want to do is to encrypt the password.

It seems Jasypt provides some easy tools for encrypting a password using command line:
http://www.jasypt.org/cli.html
http://www.jasypt.org/download.html

After downloading and unziping the tool, just go to your terminal and execute either the bat or the sh just like this:

encrypt.bat input=<password_to_encrypt> password=<master_password_for_encryption>

The encrypted password will be shown at the output of the command.

Project configuration

  1. pom.xml - Add the following dependency:
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>
  1. Properties file - Configure the master password for Jasypt:
jasypt:
  encryptor:
    password: <master_password_for_encryption>
  1. Properties file - Configure the encrypted password for SimpleJavaMail:
simplejavamail:
  smtp:
    password: ENC(<encrypted_password>)
  1. Spring Boot Application class - Add the following annotation:
@EnableEncryptableProperties

And that's it! I didn't have to modify a single line of code, besides the annotation itself. Everything is pretty well configurable using just YAML properties.

Additional comments

I was a bit bugged that I stopped showing the SimpleJavaMail password and then started displaying the Jasypt password, so I researched a little bit further.

If you don't want to expose the jasypt encrypt password in the properties file, you can add the password as a VM argument:

java -Djasypt.encryptor.password=password -jar target/jasypt-spring-boot-demo-0.0.1-SNAPSHOT.jar

Or just as a Environment System variable in the YAML:

jasypt:
    encryptor:
        password: ${JASYPT_ENCRYPTOR_PASSWORD:}

@bbottema
Copy link
Owner Author

bbottema commented Dec 7, 2018

Hah, thanks for the write up! Actually I have done this exact same thing for a project recently, but this really saves me writing it all down.
However, I would like to support it regardless of Spring. So I'll have to do a little more home work for that still.

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

2 participants