gpt4 book ai didi

java - 使用 Play 框架通过 Gmail SMTP 服务器发送电子邮件时出现 500 内部服务器错误

转载 作者:行者123 更新时间:2023-12-02 06:33:12 24 4
gpt4 key购买 nike

我正在使用 Java 中的 Play Framework 实现联系表单。当我尝试通过 Gmail SMTP 服务器发送电子邮件时,我在浏览器控制台中收到以下消息:

POST http://localhost:9000/app_contact_test.html 500 (Internal Server Error)             jquery-1.10.1.min.js:6

发送电子邮件的代码(Contact 类,函数 test())如下:

      //Get the session object  
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(username,password);
}
});

//compose message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(recipient));
message.setSubject("Hello");
message.setText("Testing.......");

//send message
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}

堆栈跟踪:

! Internal server error, for (POST) [/app_contact_test.html] ->
java.lang.NoClassDefFoundError: javax/mail/MessagingException
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.7.0]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442) ~[na:1.7.0]
at java.lang.Class.getMethod0(Class.java:2685) ~[na:1.7.0]
at java.lang.Class.getMethod(Class.java:1620) ~[na:1.7.0]
at org.apache.commons.lang3.reflect.MethodUtils.getMatchingAccessibleMethod(MethodUtils.java:511) ~[commons-lang3-3.1.jar:3.1]
at play.core.Router$HandlerInvoker$$anon$7$$anon$2$$anonfun$3.apply(Router.scala:178) ~[play_2.10-2.2.0.jar:2.2.0]
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366) ~[na:1.7.0]
at java.net.URLClassLoader$1.run(URLClassLoader.java:355) ~[na:1.7.0]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.7.0]
at java.net.URLClassLoader.findClass(URLClassLoader.java:354) ~[na:1.7.0]
at java.lang.ClassLoader.loadClass(ClassLoader.java:423) ~[na:1.7.0]
at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ~[na:1.7.0]

更新:好吧,我用 MessagingException 解决了这个问题,仍然收到错误,但现在有了另一个跟踪:

[error] play - Cannot invoke the action, eventually got an error: java.lang.SecurityException: Access to default session denied
[error] application -
! @6g8ee4hcb - Internal server error, for (POST) [/app_contact_sendEmail.html] ->
play.api.Application$$anon$1: Execution exception[[SecurityException: Access to default session denied]]
at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10-2.2.0.jar:2.2.0]
at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10-2.2.0.jar:2.2.0]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10-2.2.0.jar:2.2.0]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10-2.2.0.jar:2.2.0]
at scala.Option.map(Option.scala:145) [scala-library.jar:na]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2.applyOrElse(PlayDefaultUpstreamHandler.scala:261) [play_2.10-2.2.0.jar:2.2.0]
Caused by: java.lang.SecurityException: Access to default session denied
at javax.mail.Session.getDefaultInstance(Session.java:327) ~[mail.jar:1.4.7]
at controllers.Contact.sendEmail(Contact.java:52) ~[na:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$7$$anonfun$apply$7.apply(routes_routing.scala:129) ~[na:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$7$$anonfun$apply$7.apply(routes_routing.scala:129) ~[na:na]
at play.core.Router$HandlerInvoker$$anon$7$$anon$2.invocation(Router.scala:183) ~[play_2.10-2.2.0.jar:2.2.0]
at play.core.Router$Routes$$anon$1.invocation(Router.scala:377) ~[play_2.10-2.2.0.jar:2.2.0]

在所有教程中都没有提到 500 服务器错误或外部属性来避免它。有什么帮助吗?

最佳答案

好吧,我通过将“mail.jar”添加到 CLASSPATH 解决了第一个问题。这实际上有点令人困惑,因为在 Play 文档中编写的,外部 jar 必须放在“lib”文件夹中,这应该足够了。

第二个问题通过更改从邮件 session 获取 Prop 的方法来解决

Session.getDefaultInstance(props, ...);

Session.getInstance(props, ...);

现在我可以发送电子邮件了。希望它能帮助其他遇到同样问题的人。

关于java - 使用 Play 框架通过 Gmail SMTP 服务器发送电子邮件时出现 500 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19894291/

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