bbottema / simple-java-mail

Simple API, Complex Emails (Jakarta Mail smtp wrapper)
http://www.simplejavamail.org
Apache License 2.0
1.23k stars 270 forks source link

[bug] Environment variables are not being loaded properly #550

Closed bbottema closed 2 months ago

bbottema commented 2 months ago

When using the ConfigLoader class in the Simple Java Mail library, environment variables are not being loaded unless a properties file (simplejavamail.properties) is present on the classpath. This issue prevents configuration via environment variables in environments where properties files are not used or cannot be placed on the classpath, such as containerized applications or cloud deployments.

Steps to Reproduce:

Ensure there is no simplejavamail.properties file on the classpath. Set environment variables corresponding to configuration properties. For example:

SIMPLEJAVAMAIL_SMTP_HOST=smtp.example.com SIMPLEJAVAMAIL_SMTP_PORT=587

Run the application that utilizes the ConfigLoader to load configuration properties. Expected Behavior:

The ConfigLoader should load configuration properties from environment variables even when the properties file is absent.

Actual Behavior:

The ConfigLoader does not load properties from environment variables unless the properties file is found on the classpath. As a result, the application cannot be configured using environment variables alone.

Cause:

In the loadProperties(String filename, boolean addProperties) method of the ConfigLoader class, when the properties file is not found, the method returns without attempting to load environment variables or system properties. The environment variables are only considered when a properties file is successfully loaded, which is not the intended behavior.

Suggested Fix:

Modify the loadProperties method to ensure that system properties and environment variables are always loaded, regardless of whether the properties file exists. This can be achieved by passing an empty Properties object to the loadProperties(Properties properties, boolean addProperties) method when the properties file is not found.

Additional Information:

bbottema commented 2 months ago

Solution committed under #538.

bbottema commented 2 months ago

Fix released in v8.12.0.