views:

277

answers:

1

Hi everyone.

Just wondering if anyone can help me with a problem I've come across in Java.

Is there functionality within Java to produce a section of code that will open the default email application on a user's PC? (I guess almost like a fancy mailto link...)

If there is - is it possible to populate fields such as the To and Subject fields?

Thanks, Mike.

+4  A: 

Desktop.mail(URI) is your friend!

Javadoc:

Launches the mail composing window of the user default mail client, filling the message fields specified by a mailto: URI.

A mailto: URI can specify message fields including "to", "cc", "subject", "body", etc. See The mailto URL scheme (RFC 2368) for the mailto: URI specification details.

Example Code:

Desktop desktop;
if (Desktop.isDesktopSupported() 
    && (desktop = Desktop.getDesktop()).isSupported(Desktop.Action.MAIL) {
  URI mailto = new URI("mailto:[email protected]?subject=Hello%20World");
  desktop.mail(mailto);
} else {
  // TODO fallback to some Runtime.exec(..) voodoo?
  throw new RuntimeException("desktop doesn't support mailto; mail is dead anyway ;)");
}
sfussenegger
Does this work all the time? I've tried to use awt.Desktop.getDesktop() before and soemtimes it just doesn't work - see also http://stackoverflow.com/questions/102325/not-supported-platforms-for-java-awt-desktop-getdesktop
Bedwyr Humphreys
Thanks for your help sfussenegger! You made what sounded a difficult problem (for me) sound trivial. Cheers.:)
Mike
+1 for the voodoo
BalusC
@Bedwyr I think the question you've linked says it all. I've updated the code to reflect this. It should work reliably for Windows though; All users of different OSs are certainly able to write a mail themselves ;)
sfussenegger
@BalusC it's always good to have some other talents if coding doesn't suffice ;)
sfussenegger
+1 for "mail is dead anyway" :D
Bedwyr Humphreys
Luckily, there are voodoo practicers here where I live ;)
BalusC
note to myself: "no downvotes for @BalusC - might know voodoo practicers"
sfussenegger