how-to

Network

How to launch default mail client for sending message

For this example you need:

import java.awt.Desktop;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;

When your platform supports desktop operation, you can launch default mail client for sending mail message. Here is the code:

/* -- is desktop supported on current platform ? */

if ( Desktop.isDesktopSupported() ) {

  /* -- get desktop instance */

  Desktop desktop = Desktop.getDesktop();

  /* -- is supported browse operation ? */

  if ( desktop.isSupported( Desktop.Action.MAIL ) ) {

    /* -- try to show mail client */

    try {
      desktop.mail( new URI( "mailto:javaexamplecenter@gmail.com?subject=Test%20message" ) );
    } catch ( IOException ex ) {
    } catch ( URISyntaxException ex ) {
      Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}

Char %20 in URI specification is representation for space char. When is all OK, default mail client is launched.

Is possible to call only java.awt.Desktop.mail() without parameters for displaying empty mail client.