send email automatically from Java using Command line mail

Java is a popular programming language which has grown significantly over the years in use. As such this example will demonstrate how to complement the usefulness of Java with Febooti Command line mail client for Windows. As an example Java can be used to build an application or applet that processes some data on a remote server. The results can then be analyzed and sent to a specific user by command line Windows mail client. The obvious advantage of this method is the utilization of a simple to implement, yet powerful, reliable and flexible way to automatically send email.

SOLUTION:

The following solution displays a method to execute Febooti Command line mail tool to auto-send email. The method makes a basic call to the email tool with only four required parameters. This is to demonstrate the ease of implementation.

import java.lang.*;
import java.io.*;
...

public void sendMail( String smtpServer, String sender,
                      String reciever, String msg,
                      boolean showOutput ) {
  try {
    String cmd;
    cmd = "C:\\febootimail.exe";
    cmd += " -SERVER " + smtpServer;
    cmd += " -FROM " + sender;
    cmd += " -TO " + reciever;
    cmd += " -MSG " + msg;
    Process p = Runtime.getRuntime().exec(cmd);

    // Capture the output - note: inputstream contains the output
    if (showOutput) {
      DataStream in = new DataInputStream(p.getInputStream());
      try {
        String oneLine;
        while ((oneLine = in.readLine()) != null)
          System.out.println(oneLine);
      } catch (IOException e) {}
    }

  } catch (IOException e1) {
    System.out.println(e1);
  }
}

1: The method itself takes five parameters. The first four are defined as strings which will be used as standard arguments when calling Febooti Command line mail tool. The last argument, showOutput is used to determine whether the output should be displayed. This is useful in scenarios where the output does not need to be displayed.

2: The first few lines of code put together a string named cmd. Basically cmd represents the actual command line call to send email from DOS. It is important to note the use of \\ to separate filenames and directories for Windows systems. The use of white space must also be maintained so that the command executes correctly.

3: The command is then executed using the Runtime.getRuntime().exec(cmd) method in a new process. If showOutput has been set to true then the output is displayed automatically provided the method has been called from a command line interface. Otherwise it is piped to a DataStream class and must be manually output.

4: Unless any exceptions are caught the method should return successfully after starting the execution of Febooti Command line mail tool.

5: Further extensions to the method can include the ability to accept more parameters and attachments, and display specific error messages. An even better approach would be to define an entire class with various fields, each representing an actual Febooti Command line mail parameter. It is then easy to validate fields before executing the command and have a consistent and powerful way to automatically send email.

public class Febootimail {

  String smtpServer;
  String sender;
  String reciever;
  String plainTextMsg;
  String[] attachmentList;
  ...

  public Febootimail(/*basic arguments*/) {
    /* Any preliminary validation and assignments */
    ...
  }

  public void addAttachment(String fileName) {
    ...
  }

  public void setSender() { ... }
  public void getSender() { ... }
  ...

  // This method sends the email
  public sendMail() { ... }
}

6: The constructor may take on any number of basic arguments for convenience. Other methods can be added to change the value of parameters and perform data validation or to get the value of a parameter. The final method sendMail will gather all the data to construct and execute the command to send email. This implementation provides an easy to use wrapper class for the use of a powerful command line email tool.

Please do not hesitate to contact our support department with any possible further questions or to solve practical issues connected with Command line email integration.

Copyright © 2001-2011 Febooti Software. All rights reserved. Last updated: November 03, 2008