3

I would like to send a webpage to Outlook from Java but without initially specifying the person to whom I am sending it too. In short, I would like to implement Internet Explorer's functionality of sending a web page by email. I currently can't figure this out.

This is what I have tried but it's not working, it gives the error :

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: URI scheme is not "mailto"

This is my code :

File htmlFile = new File("http://stackoverflow.com/questions/ask");

try {
    Desktop.getDesktop().mail( htmlFile.toURI() );
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
P. Frank
  • 5,691
  • 6
  • 22
  • 50
alvy
  • 117
  • 1
  • 10

1 Answers1

0

You are getting this error because you are specifying web page URI which is "http" and not "mailto" which is expected. Check the documentation, you could use utility method.

So, to send email use something like this:

    String subject = URLEncoder.encode("Test subject").replace("+", "%20");
    String body = URLEncoder.encode("Test body").replace("+", "%20");
    Desktop.getDesktop().browse(new URI("mailto:?subject="+subject+"&body="+body));

Or this (if mail() is preffered):

Desktop.getDesktop().mail(new URI("mailto:?subject="+subject+"&body="+body));

This will set subject and body without recipient. Also mail client might be Outlook, but depends of system settings (what is set for default mail client).

If you would like to send webpage in content of mail, you should first read it, if you would like to send just URL address, then encode it and send in body.

Community
  • 1
  • 1
Dario
  • 2,053
  • 21
  • 31
  • Thank you for your reply,however the response you have given me is for sending textual email.i want to plant a web page in the body of the email as you would do with internet explorer – alvy Apr 21 '16 at 14:13