What are the system properties used for sending email?

Here are a list of system properties that can be use to send an e-mail using the JavaMail API.

Property Default Value Description
mail.host Define the host name of the mail server.
mail.smtp.host Define the host name of the SMTP server; this will overrides the mail.host for SMTP connections only.
mail._protocol_.host Define the host name of the specified protocol (POP, IMAP); this will overrides the mail.host.
mail.user Define the default username sent to all mail servers.
mail._protocol_.user Define the default username for the specified protocol; this will overrides the mail.user for the specified protocol.
mail.smtp.port 25 Define the SMTP port on which the SMTP server is listening.
mail._protocol_.port Default port for the corresponding protocol Define the port on which the servers for the specified protocol is listening.
mail.smtp.starttls.enable Upgrade the regular SMTP connection on the usual port to an encrypted (TLS or SSL) connection.
mail.smtp.connectiontimeout Infinite Define the number of milliseconds to wait for a connection timeout.
mail.debug false Define parameter for disabling or enabling information debugging.
mail.from Define the e-mail address to use in the From header.
mail.mime.charset file.encoding Define the default character set used to send messages.
mail.alternates Define other email address for the current that will not to be included when replying to a message.
mail._protocol_.class Define the fully package qualified class name of the provider for the specified protocol.
mail.transport.protocol First transport provider in the configuration file Define default protocol with which to send messages.
mail.transport.protocol.address-type Define the message transport protocol such as SMTP for the specified address type, for example mail.transport.protocol.rfc822.
mail.replayallcc false Put all recipients in the CC list of the reply message instead of the TO field when replying to all.

How do I get path / classpath separator?

OS platform has different symbol used for path separator. Path separator is a symbol that separate one path element from the other. In Windows the path separator is a semicolon symbol (;), you have something like:

.;something.jar;D:/libs/commons.jar

While in Linux based operating systems the path separator is a colon symbol (:), it looks like:

.:something.jar:/libs/commons.jar

To obtain the path separator you can use the following code.

package org.kodejava.example.lang;

import java.util.Properties;

public class PathSeparator {
    public static void main(String[] args) {
        // Get System properties
        Properties properties = System.getProperties();

        // Get the path separator which is unfortunately
        // using a different symbol in different OS platform.
        String pathSeparator = properties.getProperty("path.separator");
        System.out.println("pathSeparator = " + pathSeparator);
    }
}

How do I read a configuration file using java.util.Properties?

When we have an application that used a text file to store a configuration and the configuration is typically in a key=value format then we can use java.util.Properties to read that configuration file.

Here is an example of a configuration file called app.config:

app.name=Properties Sample Code
app.version=1.0

The code below show you how to read the configuration.

package org.kodejava.example.util;

import java.io.*;
import java.net.URL;
import java.util.Objects;
import java.util.Properties;

public class PropertiesExample {
    public static void main(String[] args) {
        Properties prop = new Properties();
        try {
            // the configuration file name
            String fileName = "app.config";
            ClassLoader classLoader = PropertiesExample.class.getClassLoader();

            // Make sure that the configuration file exists
            URL res = Objects.requireNonNull(classLoader.getResource(fileName),
                "Can't find configuration file app.config");

            InputStream is = new FileInputStream(res.getFile());

            // load the properties file
            prop.load(is);

            // get the value for app.name key
            System.out.println(prop.getProperty("app.name"));
            // get the value for app.version key
            System.out.println(prop.getProperty("app.version"));

            // get the value for app.vendor key and if the
            // key is not available return Kode Java as
            // the default value
            System.out.println(prop.getProperty("app.vendor","Kode Java"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The code snippet will print these results:

Properties Sample Code
1.0
Kode Java