Send mail to with Java is not hard to find a very simple approach chooses the Apache Commons project , with its sub-project email . The only thing to pay attention to the dependencies to Sun's JavaMail and Activation Framework , Java archives which you have to contact in the CLASSPATH.
A class that uses Commons email and sending emails with attachments permits could look like this (mail server and port must also be adjusted):
Continue reading »
Who had ever programmed using Java safe for a Java application to read the command line parameters. If you want to reinvent the wheel now is not always new, it should be only a look at the Apache commons library throw.
That done, one will find there also a problem for the command line. The solution is called - Apache commons - CLI . The library not only helps when reading the command line parameters, but also offers the possibility to immediately assist in the use of parameters to generate.
A small example says more than 1000 words
1) Installation
- Download and unpack
- commons-cli-1.0.jar in the record class_path
2) Definition of Options
Options options = new Options(); Option help = new Option( "help", "print this message" ); Option optDescription = OptionBuilder.withArgName( "string" ) .hasArg() .withDescription( "use given description for incident" ) .create( "description" ); Option optLongDescription = OptionBuilder.withArgName( "string" ) .hasArg() .withDescription( "use given description for incident" ) .create( "longDescription" ); options.addOption(help); options.addOption(optDescription); options.addOption(optLongDescription); 3) Parse the command line arguments
CommandLineParser parser = new GnuParser(); CommandLine line = parser.parse( options, args );CommandLineParser parser = new GnuParser(); CommandLine line = parser.parse( options, args );
4) Reading of options without arguments
if(line.hasOption("h")) { // Ausgabe der Hilfe }if(line.hasOption("h")) { // Ausgabe der Hilfe }
5) Reading of options with arguments
String description=""; String longDescription=""; if( line.hasOption( "description" ) ) { description = line.getOptionValue( "description" ); log.debug("Description: "+description); } if( line.hasOption( "longDescription" ) ) { longDescription = line.getOptionValue( "longDescription" ); log.debug("Long Description: "+longDescription); }String description=""; String longDescription=""; if( line.hasOption( "description" ) ) { description = line.getOptionValue( "description" ); log.debug("Description: "+description); } if( line.hasOption( "longDescription" ) ) { longDescription = line.getOptionValue( "longDescription" ); log.debug("Long Description: "+longDescription); }
6) Generate output for the help
HelpFormatter formatter = new HelpFormatter(); formatter.printHelp( "ant", options );HelpFormatter formatter = new HelpFormatter(); formatter.printHelp( "ant", options );
7) complete -> simply beautiful or beautifully simple ![]()



