Simple Java Mail CLI
Simple Java Mail exposes the complete builder API as CLI commands, which is to say everything you can do with Simple Java Mail in Java, you can also do that in a terminal.
From builder API to CLI commands
To support CLI, the commands and options are generated using Picocli's Java API and Therapi's Javadoc bundler. Together and with a lot of custom glue Simple Java Mail's builder API is traversed and converted into CLI commands.
The great benefit from this is that new builder API is supported by CLI automagically and the documentation is still centralized in the builder API, providing a consistent experience.
How to use
To use Simple Java Mail CLI, Download and expand the standalone-cli package and execute the sjm script. Included in the package are all dependencies and two helper scripts for Windows and Unix (generated by standard Maven plugins).
The flags are a bit verbose, but then it supports the complete builder API!
sjm sjm --help sjm send --email:startingBlank --email:from "Mr. Candy" firstname.lastname@example.org --email:withSubject "About those tongue twisters" --email:to "Sweet Cheeks" "email@example.com" --email:cc "Candy Research Group" "<firstname.lastname@example.org>;<email@example.com>" --email:bcc "box1 <firstname.lastname@example.org>,box2 <email@example.com>" --email:withPlainText "I discovered a new flavor! (..)" --mailer:withSMTPServer "my-smtp-server.com" 1234 "myuser" "mypass" --mailer:withTransportStrategy "SMTP_TLS"
You can also check the demo code included in the sources.
You can get usage information displayed on any level:
||informs about available commands|
||informs about options available for the "send" command|
||displays usage help for the "withSubject" option|
If you run into a character limit in your console, you can also use "argument files" or "@-files", which is supported in Picocli, the library used under the hood:
# file /home/foo/args --email:withSubject "My Subject" --mailer:withSMTPServer host=... --other arguments
sjm @/home/foo/args then is expanded to:
sjm --email:withSubject "My Subject" --mailer:withSMTPServer host=... --other arguments
Quoting the Picocli documentation:
This feature is similar to the 'Command Line Argument File' processing supported by gcc, javadoc and javac. The documentation for these tools shows further examples.
From picocli 3.8.1, a simpler argument file format is also supported where every line (except comment lines) is interpreted as a single argument. Arguments containing whitespace do not need to be quoted, but it is not possible to have arguments with embedded newlines.
Set system property
picocli.useSimplifiedAtFileswithout a value or with value
"true"(case-insensitive) to enable this simpler argument file format. This simplified format is similar to the argument file format supported by JCommander.
With the provided scripts, anything added to the \etc folder is visible on the classpath. In the current release, you can add your simplejavamail.properties there and it will be picked up.
In the current release, log4j2 properties as supported and you can simply edit the provided log4j2.xml in the /etc folder (see above).
Invoking CLI manually
If you wish to directly invoke the CLI from the jar or class files, CLI support for Java libraries works just like any other Java static void main invocation:
java -cp all;the;jars.jar org.simplejavamail.cli.SimpleJavaMail --help
Simple Java Mail's own main jar should be on the classpath of course, as well as all non-optional dependencies, the optional cli-module dependency and its dependencies.You can add a simplejavamail.properties file to the classpath this way which will automatically be picked for defaults and configuration.
Or by Maven
Alternatively, you can also let Maven deal with the dependencies, by invoking the maven-exec plugin.
Or by configuring it in your POM:
mvn exec:java -Dexec.mainClass="org.simplejavamail.cli.SimpleJavaMail" -Dexec.args="--help"
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <executions> <execution> <goals><goal>java</goal></goals> </execution> </executions> <configuration> <mainClass>org.simplejavamail.cli.SimpleJavaMail</mainClass> <arguments> <argument>--help</argument> </arguments> </configuration> </plugin> </plugins> </build>