views:

531

answers:

2

I want to open the "new mail" view using the default mail client (i.e. open a new mail form in Outlook). But when I go

String cmd = "explorer.exe \"mailto:[email protected]?subject="+
             subject+"&body="+body+"\"";
Runtime.getRuntime().exec(cmd);

the mail shows up, but I have a problem: explorer.exe brings up an Internet Explorer instance with a dummy page. Is there a better application to run, such as rundll.exe with certain arguments?

I know it is possible to do it without bringing up iexplore from C++, but I don't know how in Java.

+2  A: 

Try with java.awt.Desktop (java 6)

Desktop dt = Desktop.getDesktop();
dt.mail();

will open the default mail client (the one associated with mailto: protocol).

RealHowTo
As you see, I want subject and body as well. I would have loved an attachment, but from what I've read, it's not doable.
Jonas Byström
The API supports that.uriMailTo = new URI("mailto", "[email protected]?subject="+ subject+"dt.mail(uriMailTo);
RealHowTo
Excellent, thank you! I've read some on getting attachments in, but found no real substance. JDIC (which we just finally got rid of) and http://stackoverflow.com/questions/81862/how-do-i-send-an-email-attachment-using-the-designated-client-programmatically-f are the best I've seen, and none of them are good enough. You wouldn't happen to know more?
Jonas Byström
AFAIK that's a limitation of the mailto: protocol, you can't specify an attachment.
RealHowTo
+1  A: 

I found the answer when googling for rundll.exe:

String subject = ...;
String body = ...;
String cmd = "rundll32.exe shell32.dll,ShellExec_RunDLL \"mailto:[email protected]?"+
             "subject="+subject+"&body="+body+"\"";
Runtime.getRuntime().exec(cmd);

Sorry for wasting your time!

Jonas Byström
Jonas: Have you tried ShellExecute() instead of relying on undocumented implementation details that are likely to change in future versions of Windows?
Larry Osterman
Larry: I didn't think there was one for Java? As to JNI-ing it, I very much prefer two lines of Java code that might (probably not though) require minor adjustments to 50 lines of code spread over Java, C and build system.
Jonas Byström