How to open the default mail client in Java

Open standard mail in JavaIn the following post there is once again a small Java code snippet, by means of which the standard mail program can be called/opened.

In order to open the standard mail application in almost any operating system, a URI with the mailto protocol is suitable. Therefore, the following Java code must be able to compose the mailto URI format.

The second difficulty is to resolve this URI (or in simple words – to execute this URI). Unfortunately, this can not be executed directly using the Runtime.getRuntime().exec()-command, but must be started as a parameter via a system-specific program. Our Java-Snippet must therefore be able to distinguish between the operating systems.

Open standard mail in Java

The sample code can be run as it is and opens the default mail program with a few test parameters. In essence, the snippet consists of two functions: On the one hand composeEmail-function, which creates the mailto URI and generates and executes the operating system-specific call command, and on the other hand, the uriEncode-function, which serves as an auxiliary function, and builds the parameters of the mailto URI.

You can read here, how the differentiation of the operating systems works. In the following I let the code and/or its comments speak for itself:

public class MailHelper {

	public static void main(String[] args) {
		
		try {
			//Open mail client with "receiver", "subject", "message"
			composeEmail("webmaster@code-bude.net", 
						 "Thanks for the snippet!", 
						 "Hello Raffael,\r\nthis mail was initiated by Java.");
	    }
	    catch (Exception err) {
	      err.printStackTrace();
	    }
		System.out.println("Done!");
	}
	
	public static void composeEmail(String receiver, String subject, String body) throws Exception {
		//Generating mailto-URI. Subject and body (message) has to encoded.		
		String mailto = "mailto:" + receiver;        
		mailto += "?subject=" + uriEncode(subject);
		mailto += "&body=" + uriEncode(body);
		
		//Create OS-specific run command
		String cmd = "";
		String os = System.getProperty("os.name").toLowerCase();
	    if (os.contains("win")){
	 		cmd = "cmd.exe /c start "" "" + mailto + """;
		}
	    else if (os.contains("osx")){
	    	cmd = "open " + mailto;
		}	    
	    else if (os.contains("nix") || os.contains("aix") || os.contains("nux")){
	    	cmd = "xdg-open " + mailto;
	    }
	    //Call default mail client with paramters
	    Runtime.getRuntime().exec(cmd);
	 
	}
	
	private static String uriEncode(String in) {
		String out = new String();
	    for (char ch : in.toCharArray()) {
	    	out += Character.isLetterOrDigit(ch) ? ch : String.format("%%%02X", (int)ch);
	    }
	    return out;
	}	
}

If something is unclear, do not hesitate to leave a comment. If you still have ideas on how to improve the code, I am also grateful for every comment. Otherwise – have fun programming!

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked