0

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

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

发送电子邮件 ( class Contact, function 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 服务器错误或避免它的外部属性。有什么帮助吗?

4

1 回答 1

2

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

第二个问题是通过更改获取邮件会话道具的方法来解决的

Session.getDefaultInstance(props, ...);

Session.getInstance(props, ...);

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

于 2013-11-10T20:42:38.297 回答