gpt4 book ai didi

javamail 不工作。出现消息异常

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

我正在执行以下代码,使用 javamail 在 gmail 上发送电子邮件。

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaApplication1 {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("goods.ramesh","mypassword");
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("goods.ramesh@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("goods.ramesh@yahoo.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");

Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}

但是我收到以下错误堆栈跟踪:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getMethod0(Class.java:2685)
at java.lang.Class.getMethod(Class.java:1620)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:484)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476)
Exception in thread "main" Java Result: 1

我不明白 MessagingException 正在做什么以及它发生的原因。任何人都可以帮助我摆脱这个异常吗?

最佳答案

我也遇到了同样的 ClassNotFound 问题。我已经通过以下 POM 设置解决了

    <dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2</version>
</dependency>

我在 EJB 单元测试中使用了这个版本的 glassfish-embedded-all,所以我的理解是这个 jar 包含 Java EE API 所需的所有类

关于javamail 不工作。出现消息异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11569235/

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