Java:打开默认邮件应用程序并创建新邮件并填充收件人和主题字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2357895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:45:55  来源:igfitidea点击:

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

javaemailuridefaultmailto

提问by Mike

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

只是想知道是否有人可以帮助我解决我在 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...)

Java 中是否有功能可以生成一段代码来打开用户 PC 上的默认电子邮件应用程序?(我想几乎就像一个花哨的mailto链接......)

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

如果有 - 是否可以填充诸如“收件人”和“主题”字段之类的字段?

Thanks, Mike.

谢谢,迈克。

回答by sfussenegger

Desktop.mail(URI mailtoURI) is your friend!

Desktop.mail(URI mailtoURI) 是你的朋友!

Javadoc:

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.

启动用户默认邮件客户端的邮件撰写窗口,填充由 mailto: URI 指定的消息字段。

mailto: URI 可以指定消息字段,包括“to”、“cc”、“subject”、“body”等。有关mailto: URI 规范的详细信息,请参阅mailto URL 方案 (RFC 2368)

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 ;)");
}