gpt4 book ai didi

java.lang.NoSuchMethodError : main. 线程中的异常 "main"

转载 作者:行者123 更新时间:2023-12-01 05:50:54 25 4
gpt4 key购买 nike

我正在尝试从 Java 发送邮件。

我收到此运行时异常:

java.lang.NoSuchMethodError: main. Exception in thread "main"

不知道问题出在哪里。请帮忙。

我使用以下代码:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail{
public static void Mail(String from, String to, String subject, String text)
{

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");

Session mailSession = Session.getDefaultInstance(props, null);
Message simpleMessage = new MimeMessage(mailSession);

InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try
{
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
} catch (AddressException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

try
{
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO, toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);

Transport.send(simpleMessage);
} catch (MessagingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args)
{

String from = "prav.br@gmail.com";
String to = "prav.br@gmail.com";
String subject = "Test";
String message = "A test message";

Mail.Mail(from, to, subject, message);

}
}

最佳答案

没有看到代码中有任何问题[至少它应该运行]。确保邮件 api 位于要编译的类路径中。

建议: 您的代码不遵循 Java 标准命名约定,您应该遵循该约定。

另请参阅:

关于java.lang.NoSuchMethodError : main. 线程中的异常 "main",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4723714/

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