Java: Open default mail application and create new mail and populate To and Subject fields

StackOverflow https://stackoverflow.com/questions/2357895

문제

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.

도움이 되었습니까?

해결책

Desktop.mail(URI mailtoURI) 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:john@example.com?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 ;)");
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top