Configuration

Simple Java Mail provides full configuration through programmatic API as well as system variables, environment variables and properties files. For each level of configuration detail, a simple constructor is available as well.

The Java API and config files complement each other; the more you configure in a properties file, the less you can provide through the programmatic API. If you provide the overlapping configuration, the programmatic API takes priority, overriding system and environment values, overriding property values.

§

Programmatic API

Everything can be configured through the java API.

// the simplest constructor, this is how the library started!
new Mailer("smtp.host.com", 25, "username", "password");

// the same as the basic constructor, but with a config object
new Mailer(new ServerConfig("smtp.host.com", 25, "username", "password"));

// you can even leave out some details for an anonymous SMTP server
new Mailer(new ServerConfig("smtp.host.com", 25));

// adding the transport strategy...
new Mailer("smtp.host.com", 587, "username", "password", TransportStrategy.SMTP_TLS);
new Mailer(serverConfig, TransportStrategy.SMTP_TLS);

// or instead adding anonymous proxy configuration
new Mailer(serverConfig, new ProxyConfig("proxy.host.com", 1080));

// or authenticated proxy
new Mailer(serverConfig, new ProxyConfig("proxy.host.com", 1080, "proxy username", "proxy password"));

// anonymous smtp + anonymous proxy + default SMTP_PLAIN strategy
new Mailer(new ServerConfig("smtp.host.com", 25), new ProxyConfig("proxy.host.com", 1080));

// configure everything!
new Mailer(
		new ServerConfig("smtp.host.com", 587, "user@host.com", "password"),
		TransportStrategy.SMTP_TLS,
		new ProxyConfig("socksproxy.host.com", 1080, "proxy user", "proxy password")
).sendMail(email);

// preconfigured Session?
new Mailer(session);

// preconfigured but you need anonymous proxy?
new Mailer(session, new ProxyConfig("proxy.host.com", 1080));

// preconfigured but you need authenticated proxy?
new Mailer(session, new ProxyConfig("proxy.host.com", 1080, "proxy user", "proxy password"));
§

Other settings

Aside from the constructors, there are a few other settings.

// make javax.mail produce more logging
mailer.setDebug(true);

// change email validation strategy, or deactivate completely
mailer.setEmailAddressCriteria(EmailAddressCriteria.DEFAULT);
mailer.setEmailAddressCriteria(EmailAddressCriteria.RFC_COMPLIANT);
mailer.setEmailAddressCriteria(EnumSet
       .of(ALLOW_QUOTED_IDENTIFIERS, ALLOW_PARENS_IN_LOCALPART));
mailer.setEmailAddressCriteria(null);

// change SOCKS5 bridge port in case of authenticated proxy
Proxyconfig proxyConfig = new ProxyConfig(...);
proxyConfig.setProxyBridgePort(1081); // is always localhost

// set custom properties
Properties props = new Properties();
props.setProperty("mail.smtp.sendpartial", true);
mailer.applyProperties(props);
  
// or:
mailer.getSession().getProperties().setProperty("mail.smtp.sendpartial", true);

// trust all hosts for SSL connections, don't validate keys
mailer.trustAllSSLHosts(true)

// white list hosts for SSL connections, don't validate keys for these
mailer.trustSSLHosts("a", "b", "c", ...);
§

Properties files

With properties files you can define defaults and overrides. You can also provide overriding value by defining system variables.

Simple Java Mail will automatically load properties from simplejavamail.properties, if available on the classpath. Alternatively, you can manually load additional properties files in a number of ways.

Properties are loaded in order of priority from high to low:

  1. Programmatic values
  2. System variables
  3. Environment variables
  4. Properties from config files
ConfigLoader.loadProperties("overrides-on-classpath.properties", /* addProperties = */ true);
ConfigLoader.loadProperties(new File("d:/replace-from-environment.properties"), /* addProperties = */ false);
ConfigLoader.loadProperties(usingMyOwnInputStream, addProperties);
§

Available properties

The default recipients are like admin features, where you notify a person whenever a mail goes out from a certain mailer.

simplejavamail.javaxmail.debug=true
simplejavamail.transportstrategy=SMTP_SSL
simplejavamail.smtp.host=smtp.default.com
simplejavamail.smtp.port=25
simplejavamail.smtp.username=username
simplejavamail.smtp.password=password
simplejavamail.proxy.host=proxy.default.com
simplejavamail.proxy.port=1080
simplejavamail.proxy.username=username proxy
simplejavamail.proxy.password=password proxy
simplejavamail.proxy.socks5bridge.port=1081
simplejavamail.defaults.subject=Sweet News
simplejavamail.defaults.from.name=From Default
simplejavamail.defaults.from.address=from@default.com
simplejavamail.defaults.replyto.name=Reply-To Default
simplejavamail.defaults.replyto.address=reply-to@default.com
simplejavamail.defaults.to.name=To Default
simplejavamail.defaults.to.address=to@default.com
simplejavamail.defaults.cc.name=CC Default
simplejavamail.defaults.cc.address=cc@default.com
simplejavamail.defaults.bcc.name=BCC Default
simplejavamail.defaults.bcc.address=bcc@default.com
simplejavamail.defaults.poolsize=10
§

Combining both for multiple environments

Let's set up configuration for a test, acceptance and production environment.

Properties for the environments

/* global default properties (simplejavamail.properties on classpath) */

    // anonoymous SMTP inside 'safe' DMZ
    simplejavamail.smtp.host=dmz.smtp.candyshop.com
    simplejavamail.smtp.port=25

    // default sender and reply-to address
    simplejavamail.defaults.from.name=The Candy App
    simplejavamail.defaults.from.address=candyapp@candystore.com
    simplejavamail.defaults.replyto.name=Candystore Helpdesk
    simplejavamail.defaults.replyto.address=helpdesk@candystore.com
/* overrides from TEST and UAT .../config/candystore/simplejavamail.properties */

    // always send a copy to test archive
    simplejavamail.defaults.bcc.name=Archive TST UAT
    simplejavamail.defaults.bcc.address=test-archive@candyshop.com
/* overrides from PRODUCTION .../config/candystore/simplejavamail.properties */

    // always send a copy to production archive
    simplejavamail.defaults.bcc.name=Archive PRODUCTION
    simplejavamail.defaults.bcc.address=prod-archive@candyshop.com

    // smtp server in production is protected
    simplejavamail.smtp.username=creamcake
    simplejavamail.smtp.password=crusty_l0llyp0p

    // sending mails in production must go through proxy
    simplejavamail.proxy.host=proxy.candyshop.com
    simplejavamail.proxy.port=1080
    simplejavamail.proxy.username=candyman
    simplejavamail.proxy.password=I has the sugarcanes!!1!

Now for the programmatic part

// simplejavamail.properties is automatically loaded

// assume that every environment provides its own property file
ConfigLoader.loadProperties(new File(".../config/candystore/simplejavamail.properties"));

// see if we need to do some specific override for some reason
if (someSpecialCondition) {
  ConfigLoader.loadProperties("special-override.properties", true);
}

// or maybe we want to ditch all defaults and trust someone else's config completely
if (ditchOwnAndTrustOtherSource) {
  ConfigLoader.loadProperties(someFileOrInputSource, false);
}

// maybe the config service has something?
ConfigLoader.loadProperties(socket.getInpuStream(), true);

Maybe we want to connect slightly different for some reason:

// override only the port and connection type, leave everything else to config files
Mailer mailer = new Mailer(new ServerConfig(null, 587), TransportStrategy.SMTP_TLS);