Note that there are some explanatory texts on larger screens.

plurals
  1. PO500 Internal Server Error while sending an email via Gmail SMTP server using play framework
    text
    copied!<p>I'm implementing a contact form using Play Framework in Java. When I'm trying to send an email via Gmail SMTP server I've got following message in browser console:</p> <pre><code>POST http://localhost:9000/app_contact_test.html 500 (Internal Server Error) jquery-1.10.1.min.js:6 </code></pre> <p>The code for sending an email (<code>class Contact, function test()</code>) is following:</p> <pre><code> //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); } </code></pre> <p>Stack trace:</p> <pre><code>! Internal server error, for (POST) [/app_contact_test.html] -&gt; 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] </code></pre> <p><strong>Update</strong>: Okey, I solved the issue with MessagingException, still getting the error, but now with another trace:</p> <pre><code>[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] -&gt; 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] </code></pre> <p>In all tutorials is nothing about 500 Server Error or external properties to avoid it. Any help?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload