Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open the default mail program with a Subject and Body in a cross-platform way?

How do I open the default mail program with a Subject and Body in a cross-platform way?

Unfortunately, this is for a a client app written in Java, not a website.

I would like this to work in a cross-platform way (which means Windows and Mac, sorry Linux). I am happy to execute a VBScript in Windows, or AppleScript in OS X. But I have no idea what those scripts should contain. I would love to execute the user's default program vs. just searching for Outlook or whatever.

In OS X, I have tried executing the command:

open mailto:?subject=MySubject&body=TheBody

URL escaping is needed to replace spaces with %20.

Updated On Windows, you have to play all sorts of games to get start to run correctly. Here is the proper Java incantation:

class Win32 extends OS {
    public void email(String subject, String body) throws Exception {
        String cmd = "cmd.exe /c start \"\" \"" + formatMailto(subject, body) + "\"";
        Runtime.getRuntime().exec(cmd);
    }
}
like image 953
Frank Krueger Avatar asked Aug 20 '08 03:08

Frank Krueger


People also ask

How do I set my default email program?

Or open the Start menu and click the gear icon on the left. In Settings, click “Apps.” In Apps, click “Default Apps” in the sidebar. In the Default Apps section, click the icon located just below “Email.” This will allow you to select a new email client that you'd like to use as default.

How do I change my default email program on Mac?

Open the Mail app. Choose Mail > Preferences, then click General. Choose an email app from the ”Default email reader” menu.

How do I change my default email program in Windows 11?

To change your default mail app on Windows PC, open the Windows Settings menu by pressing the Win + I. When the Settings tab opens, click on the Apps option and select Default Apps. Now head to the search menu bar and type in the name of the email you'd like to set up as the default email client.


1 Answers

Never use Runtime.exec(String) on Mac OS X or any other operating system. If you do that, you'll have to figure out how to properly quote all argument strings and so on; it's a pain and very error-prone.

Instead, use Runtime.exec(String[]) which takes an array of already-separated arguments. This is much more appropriate for virtually all uses.

like image 184
Chris Hanson Avatar answered Oct 23 '22 03:10

Chris Hanson