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);
}
}
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.
Open the Mail app. Choose Mail > Preferences, then click General. Choose an email app from the ”Default email reader” menu.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With