Java:打开默认邮件应用程序并创建新邮件并填充To和Subject字段 [英] Java: Open default mail application and create new mail and populate To and Subject fields

查看:892
本文介绍了Java:打开默认邮件应用程序并创建新邮件并填充To和Subject字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



如果有 - 是否可以填充To和Subject字段等字段?



谢谢,
Mike。

解决方案

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



Javadoc:


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



mailto:URI可以指定包含to,cc,主题,正文等。请参见 mailto URL方案(RFC 2368), mailto:URI规范详细信息。


示例代码:

 桌面桌面; 
if(Desktop.isDesktopSupported()
&&(desktop = Desktop.getDesktop())。isSupported(Desktop.Action.MAIL)){
URI mailto = new URI(邮寄地址:john@example.com主题=你好%20World);
desktop.mail(mailto);
} else {
// TODO fallback to some Runtime.exec(..)voodoo?
抛出新的RuntimeException(桌面不支持mailto;邮件已经死了));
}


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

这篇关于Java:打开默认邮件应用程序并创建新邮件并填充To和Subject字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆