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:dummy@domain.com?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:dummy@domain.com?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:name@hotmail.com"));

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
  • Could I write: Desktop desktop = null; desktop = getDesktop(); desktop.browse(new URI("name@hotmail.com")); ? –  Feb 15 '12 at 09:07
  • @PitaSivam: It will not send email to the user.You have to use `Desktop.mail` – RanRag Feb 15 '12 at 09:09
  • no, better use `dektop.mail()` as that's what it is for. `browse()` will open a browser (which in turn can open the email client). two steps instead of one. – soulcheck Feb 15 '12 at 09:10
  • Desktop desktop = null; desktop = getDesktop(); desktop.mail(name@hotmail.com); Does it sound right? –  Feb 15 '12 at 09:12