Bfcm
Bfcm

Reputation: 2746

500 Internal Server Error while sending an email via Gmail SMTP server using play framework

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:

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

The code for sending an email (class Contact, function test()) is following:

      //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);
        }

Stack trace:

! 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]

Update: Okey, I solved the issue with MessagingException, still getting the error, but now with another trace:

[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]

In all tutorials is nothing about 500 Server Error or external properties to avoid it. Any help?

Upvotes: 0

Views: 2044

Answers (1)

Bfcm
Bfcm

Reputation: 2746

Okey, I solved the first problem by adding "mail.jar" to the CLASSPATH. It's actually a little bit confusing, because in Play Documentation is written, that external jars have to be places in "lib" folder and that should be enough.

The second problem is fixed by changing the method of getting props for the mail-session from

Session.getDefaultInstance(props, ...);

to

Session.getInstance(props, ...);

So now I can send emails. Hopefully it will help another people who will have the same issue.

Upvotes: 2

Related Questions