4

I am trying to set a hyperlink in my eclipse java project. when someone clicks on a button, it should open up an email client along with the given email id. is it possible to implement it with java.awt.Desktop?

2 Answers2

8

Yes it is possible using desktop.mail()

Desktop desktop = Desktop.getDesktop();
String message = "mailto:[email protected]?subject=First%20Email";
URI uri = URI.create(message);
desktop.mail(uri);

and regarding the mailto URI you have to create it yourself.

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.

RanRag
  • 48,359
  • 38
  • 114
  • 167
  • Ran Rag, I just got home and tried it out. I wrote Desktop desktop = Desktop.getDesktop(); String message = "mailto:[email protected]?subject=First%20Email"; URI uri = URI.create(message); desktop.mail(uri); and replaced the email id with existing one, and an error message pops up underlining URI.create saying URI.create cannot be resolved to a type? –  Feb 15 '12 at 14:45
2

What's wrong with java.awt.Desktop.mail(URI mailtoURI) ??

edit

as for usage:

   Desktop desktop = getDesktop(); 
   desktop.mail(new URI("mailto:[email protected]"));

You need to construct an URI instance and pass it to Destkop.mail.

Here's a helpful wiki article about constructing mailto URIs.

soulcheck
  • 36,297
  • 6
  • 91
  • 90