bbottema / simple-java-mail

Simple API, Complex Emails (Jakarta Mail smtp wrapper)

Home Page:http://www.simplejavamail.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

bbottema opened this issue · comments

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

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.

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.

Good point! What about Jasypt?

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

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:}

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.