gpt4 book ai didi

java - 关于如何在 java 按钮中创建电子邮件链接的任何建议

转载 作者:太空宇宙 更新时间:2023-11-04 06:58:09 26 4
gpt4 key购买 nike

我想要做的是在按钮中创建一个电子邮件链接,以便当用户单击该按钮时,它会启动默认电子邮件客户端,并将我的电子邮件地址作为目的地

我尝试过这个实验,但没有运气(我不知道):

private void jButton50ActionPerformed(java.awt.event.ActionEvent evt) {                                          

URL url = new URL('tcod@live.ca');
}

但这当然不是 URL!

更新:我尝试过这个,但每当我尝试使用 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();
}
}

最佳答案

看看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

所需的格式

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.

已更新工作示例...

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

}

关于java - 关于如何在 java 按钮中创建电子邮件链接的任何建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22448340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com