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

<p>Unfortunately, this is for a a client app written in Java, not a website.</p>

<p>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.</p>

<p>In OS X, I have tried executing the command:</p>

<pre><code>open mailto:?subject=MySubject&amp;body=TheBody
</code></pre>

<p>URL escaping is needed to replace spaces with <code>%20</code>.</p>

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

<pre><code>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);
    }
}
</code></pre>

[{'answer_id': 17379, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 2, 'selected': False, 'text': '<p>Mailto isn\'t a bad route to go. But as you mentioned, you\'ll need to make sure it is encoded correctly. </p>\n\n<p>The main problem with using mailto is with breaking lines. Use %0A for carriage returns, %20 for spaces.</p>\n\n<p>Also, keep in mind that the mailto is considered the same as a URL of sorts and therefore will have the same limitations for length. See \n<a href="http://support.microsoft.com/kb/208427" rel="nofollow noreferrer">http://support.microsoft.com/kb/208427</a>, note the maximum URL length of 2083 characters. This is confirmed for mailto as well \nin this article: <a href="http://support.microsoft.com/kb/279460/en-us" rel="nofollow noreferrer">http://support.microsoft.com/kb/279460/en-us</a>. Also, some mail clients can also have a limit (I believe older versions of Outlook Express had a limit of something much smaller like 483 characters or something. If you expect to have a longer string than that then you\'ll need to look at alternatives.</p>\n\n<p>BTW, you shouldn\'t have to resort to kicking out a script to do that as long as you can shell out a command from Java (I don\'t know if you can since I don\'t do Java).</p>\n'}, {'answer_id': 17389, 'author': 'Dhaust', 'author_id': 242, 'author_profile': 'https://Stackoverflow.com/users/242', 'pm_score': 2, 'selected': False, 'text': '<p><strong>1. Add a Subject Line</strong> </p>\n\n<p>You can prefill the subject line in the email by adding the subject preceded by \'<strong>?subject=</strong>\' after the email address. </p>\n\n<p>So the link now becomes: </p>\n\n<pre><code>&lt;a href="mailto:someone@yoursite.com?subject=Mail from Our Site"&gt;Email Us&lt;/a&gt; \n</code></pre>\n\n<p><strong>2. Send to Multiple Recipients</strong> </p>\n\n<p>Mail can be sent to additional recipients either as carbon copies (cc) or blind carbon copies (bcc). </p>\n\n<p>This is done in a similar way, by placing \'<strong>?cc=someoneelse@theirsite.com</strong>\' after the initial address. </p>\n\n<p>So the link looks like this:</p>\n\n<pre><code>&lt;a href="mailto:someone@yoursite.com?cc=someoneelse@theirsite.com"&gt;Email Us&lt;/a&gt;\n</code></pre>\n\n<p>cc can simply be replaced by bcc if you wish to send blind carbon copies. </p>\n\n<p>This can be very useful if you have links on pages with different subjects. You might have the email on each page go to the appropriate person in a company but with a copy of all mails sent to a central address also. </p>\n\n<p>You can of course specify more than one additional recipient, just separate your list of recipients with a comma. </p>\n\n<pre><code>&lt;a href="mailto:someone@yoursite.com?cc=someoneelse@theirsite.com, another@thatsite.com, me@mysite.com"&gt;Email Us&lt;/a&gt; \n</code></pre>\n\n<p>Sourced from <a href="http://www.outfront.net/tutorials_02/adv_tech/mailto.htm" rel="nofollow noreferrer">Getting More From \'mailto\'</a> which now 404s.  I retrieved the content from waybackmachine.    </p>\n\n<p><strong>3. Combining Code</strong> </p>\n\n<p>You can combine the various bits of code above by the addition of an \'&amp;\' between each.</p>\n\n<p>Thus adding </p>\n\n<pre><code>me@mysite.com?subject=Hello&amp;cc=you@yoursite.com&amp;bcc=her@hersite.com\n</code></pre>\n\n<p>would send an email with the subject \'Hello\' to me, you and her. </p>\n\n<p><strong>4. Write the Email</strong> </p>\n\n<p>You can also prefill the body of the email with the start of a message, or write the whole message if you like! To add some thing to the body of the email it is again as simple as above - \'<strong>?body=</strong>\' after the email address. However formatting that email can be a little tricky. To create spaces between words you will have to use hex code - for example \'<strong>%20</strong>\' between each word, and to create new lines will mean adding \'<strong>%0D</strong>\'. Similarly symbols such as <strong>$</strong> signs will need to be written in hex code.</p>\n\n<p>If you also wish to add a subject line and send copies to multiple recipients, this can make for a very long and difficult to write bit of code. </p>\n\n<p>It will send a message to three people, with the subject and the message filled in, all you need to do is add your name. </p>\n\n<p>Just look at the code! </p>\n\n<pre><code>&lt;a href="mailto:abbeyvet@outfront.net?CC=spooky@outfront.net\n&amp;BCC=thomasbrunt@outfront.net&amp;Subject=Please%2C%20I%20insist\n%21&amp;Body=Hi%0DI%20would%20like%20to%20send%20you%20\n%241000000%20to%20divide%20as%20you%20see%20fit%20among\n%20yourselves%20and%20all%20the%20moderators.%0DPlease%\n20let%20me%20know%20to%20whom%20I%20should%20send\n%20the%20check."&gt;this link&lt;/a&gt; \n</code></pre>\n\n<p><em>Note: Original source URL where I found this is now 404ing so I <a href="http://web.archive.org/web/20121123210358/http://www.outfront.net/tutorials_02/adv_tech/mailto.htm" rel="nofollow noreferrer">grabbed to content from waybackmachine</a> and posted it here so it doesn\'t get lost.  Also, the OP stated it was not for a website, which is what these examples are, but some of these techniques may still be useful.</em></p>\n'}, {'answer_id': 17394, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 0, 'selected': False, 'text': '<p>I have implemented this, and it works well on OS X. (Ryan\'s mention of the max URL length has not been codified.)</p>\n\n<pre><code>public void email(String subject, String body) throws Exception {\n    String cmd = "open mailto:";        \n    cmd += "?subject=" + urlEncode(subject);\n    cmd += "&amp;body=" + urlEncode(body);\n    Runtime.getRuntime().exec(cmd);\n}\n\nprivate static String urlEncode(String s) {\n    StringBuilder sb = new StringBuilder();\n    for (int i = 0; i &lt; s.length(); i++) {\n        char ch = s.charAt(i);\n        if (Character.isLetterOrDigit(ch)) {\n            sb.append(ch);\n        }\n        else {\n            sb.append(String.format("%%%02X", (int)ch));\n        }\n    }\n    return sb.toString();\n}\n</code></pre>\n\n<p>I had to re-implement URLencode because Java\'s would use <code>+</code> for space and Mail took those literally. Haven\'t tested on Windows yet.</p>\n'}, {'answer_id': 17398, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>I had to re-implement URLencode\n  because Java\'s would use + for space\n  and Mail took those literally.</p>\n</blockquote>\n\n<p>I don\'t know if Java has some built-in method for urlencoding the string, but this link <a href="http://www.permadi.com/tutorial/urlEncoding/" rel="nofollow noreferrer">http://www.permadi.com/tutorial/urlEncoding/</a> shows some of the most common chars to encode:</p>\n\n<pre><code>;       %3B\n?       %3F\n/       %2F\n:       %3A\n#       %23\n&amp;       %24\n=       %3D\n+       %2B\n$       %26\n,       %2C\nspace   %20 or +\n%       %25\n&lt;       %3C\n&gt;       %3E\n~       %7E\n%       %25\n</code></pre>\n'}, {'answer_id': 17426, 'author': 'travis', 'author_id': 1414, 'author_profile': 'https://Stackoverflow.com/users/1414', 'pm_score': 2, 'selected': False, 'text': '<p><code>start</code> works fine in Windows (see below). I would use Java\'s built in UrlEscape then just run a second replacement for \'+\' characters.</p>\n\n<pre><code>start mailto:"?subject=My%20Subject&amp;body=The%20Body"\n</code></pre>\n'}, {'answer_id': 17535, 'author': 'Shadow2531', 'author_id': 1697, 'author_profile': 'https://Stackoverflow.com/users/1697', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>I don\'t know if Java has some built-in method for urlencoding the string, but this link <a href="http://www.permadi.com/tutorial/urlEncoding/" rel="nofollow noreferrer">http://www.permadi.com/tutorial/urlEncoding/</a> shows some of the most common chars to encode:</p>\n</blockquote>\n\n<p>For percent-encoding mailto URI hnames and hvalues, I use the rules at <a href="http://shadow2531.com/opera/testcases/mailto/modern_mailto_uri_scheme.html#encoding" rel="nofollow noreferrer">http://shadow2531.com/opera/testcases/mailto/modern_mailto_uri_scheme.html#encoding</a>. Under <a href="http://shadow2531.com/opera/testcases/mailto/modern_mailto_uri_scheme.html#implementations" rel="nofollow noreferrer">http://shadow2531.com/opera/testcases/mailto/modern_mailto_uri_scheme.html#implementations</a>, there\'s a Java example that may help.</p>\n\n<p>Basically, I use:</p>\n\n<pre><code>private String encodex(final String s) {\n    try {\n        return java.net.URLEncoder.encode(s, "utf-8").replaceAll("\\\\+", "%20").replaceAll("\\\\%0A", "%0D%0A");\n    } catch (Throwable x) {\n        return s;\n    }\n}\n</code></pre>\n\n<p>The string that\'s passed in should be a string with \\r\\n, and stray \\r already normalized to \\n.</p>\n\n<p>Also note that just returning the original string on an exception like above is only safe if the mailto URI argument you\'re passing on the command-line is properly escaped and quoted.</p>\n\n<p>On windows that means:</p>\n\n<ol>\n<li>Quote the argument.</li>\n<li>Escape any " inside the quotes with \\.</li>\n<li>Escape any \\ that precede a " or the end of the string with \\.</li>\n</ol>\n\n<p>Also, on windows, if you\'re dealing with UTF-16 strings like in Java, you might want to use ShellExecuteW to "open" the mailto URI. If you don\'t and return s on an exception (where some hvalue isn\'t completely percent-encoded, you could end up narrowing some wide characters and losing information. But, not all mail clients accept unicode arguments, so ideally, you want to pass a properly percent-encoded-utf8 ascii argument with ShellExecute.</p>\n\n<p>Like \'start\', ShellExecute with "open" should open the mailto URI in the default client.</p>\n\n<p>Not sure about other OS\'s.</p>\n'}, {'answer_id': 27353, 'author': 'Chris Hanson', 'author_id': 714, 'author_profile': 'https://Stackoverflow.com/users/714', 'pm_score': 2, 'selected': False, 'text': "<p>Never use <code>Runtime.exec(String)</code> 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.</p>\n\n<p>Instead, use <code>Runtime.exec(String[])</code> which takes an array of already-separated arguments.  This is much more appropriate for virtually all uses.</p>\n"}, {'answer_id': 27791, 'author': 'rotabla', 'author_id': 901, 'author_profile': 'https://Stackoverflow.com/users/901', 'pm_score': 4, 'selected': True, 'text': '<p>In Java 1.6 you have a stardard way to open the default mailer of the platform:\n<a href="http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html" rel="noreferrer">the Desktop.mail(URI) method</a>.The URI can be used to set all the fields of the mail (sender, recipients, body, subject).\nYou can check a full example of desktop integration in Java 1.6 on <a href="http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/" rel="noreferrer">Using the Desktop API in Java SE 6</a></p>\n'}, {'answer_id': 12107210, 'author': 'NoNaMe', 'author_id': 1410342, 'author_profile': 'https://Stackoverflow.com/users/1410342', 'pm_score': 1, 'selected': False, 'text': '<p>You may use this...</p>\n\n<pre><code>main(string[] args){    \nString forUri = String.format("mailto:?subject=%s&amp;body=%s",  urlEncode(sub), urlEncode(mailBody));\n                        Desktop.getDesktop().mail(new URI(forUri));\n}\n\nprivate static final String urlEncode(String str) {\n        try {\n            return URLEncoder.encode(str, "UTF-8").replace("+", "%20");\n        } catch (UnsupportedEncodingException e) {\n            throw new RuntimeException(e);\n        }\n    }\n</code></pre>\n\n<p>Also for formatting read <a href="http://www.2ality.com/2010/12/simple-way-of-sending-emails-in-java.html" rel="nofollow">A simple way of sending emails in Java: mail-to links</a></p>\n'}]
['https://Stackoverflow.com/questions/17373', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/338/']