我想要做的是在按钮中创建一个电子邮件链接,以便当用户点击该按钮时,它会启动默认电子邮件客户端,并将我的电子邮件地址作为目的地
我尝试过这个但没有运气(我不知道):
private void jButton50ActionPerformed(java.awt.event.ActionEvent evt) {
URL url = new URL('tcod@live.ca');
}
但当然那不是网址!
更新:我试过这个,每当我尝试使用mail()时我都会收到错误。我是否必须导入其他内容
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String mailTo = jButton2.getText();
URI uriMailTo = null;
try
{
if(mailTo.length() > 0)
{
uriMailTo = new URI("mailto", mailTo, null);
desktop.mail(uriMailTo);
}
else
{
desktop.mail();
}
}
catch(IOException e)
{
e.printStackTrace();
}
catch(URISyntaxException use)
{
use.printStackTrace();
}
}
答案 0 :(得分:1)
查看How to Integrate with the Desktop Class
这是直接从上面的链接教程中获取的一个例子......
private void onLaunchMail(ActionEvent evt) {
String mailTo = txtMailTo.getText();
URI uriMailTo = null;
try {
if (mailTo.length() > 0) {
uriMailTo = new URI("mailto", mailTo, null);
desktop.mail(uriMailTo);
} else {
desktop.mail();
}
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(URISyntaxException use) {
use.printStackTrace();
}
}
您还应该查看Desktop#mail(URI)
,而不是URI
所需的格式
mailto:URI可以指定消息字段,包括"到"," cc"," subject"," body"等。有关mailto:URI规范的详细信息,请参阅The mailto URL scheme (RFC 2368)。
更新了工作示例...
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class TestEmail {
public static void main(String[] args) {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.MAIL)) {
String mailTo = "sendto@somwhere.com";
URI uriMailTo = null;
try {
if (mailTo.length() > 0) {
System.out.println("Mail to " + mailTo);
uriMailTo = new URI("mailto", mailTo, "This is a message");
desktop.mail(uriMailTo);
} else {
System.out.println("Mail");
desktop.mail();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (URISyntaxException use) {
use.printStackTrace();
}
}
}
}
}