I am trying to send an email from a JSP using gmail, I tested my code in a normal Java Application (not web) and it works well, but I have this problem
java.lang.NoSuchMethodError: sun.security.ssl.Handshaker.setApplicationProtocols([Ljava/lang/String;)V
when I run the JSP, here is my code
<% String result = "failed"; Properties props = new Properties(); String gmailAccount = "mymail@gmail.com"; String gmailPassword = "mypass"; String body = "<h1>Hello!</h1>"; String subject = "Test"; String to = "destinationmail@gmail.com"; Session sessionmail; 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"); sessionmail = Session.getInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(gmailAccount, gmailPassword); } }); try { MimeBodyPart a = new MimeBodyPart(); Multipart multipart = new MimeMultipart(); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(body, "text/html"); multipart.addBodyPart(mimeBodyPart); Message message = new MimeMessage(sessionmail); message.setFrom(new InternetAddress(gmailAccount)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setContent(multipart); Transport transport = sessionmail.getTransport("smtp"); transport.connect("smtp.gmail.com", gmailAccount, gmailPassword); transport.sendMessage(message, message.getAllRecipients()); result = "Ok"; } catch (MessagingException e) { result = "failed"; } %>
By the way, I am using jdk 1.8.0_251
and Payara server 5.194 (I tried by using glassfish but I got the same error)
Advertisement
Answer
If your code works in a console app, but throws that exception in Payara and Glassfish, then it most likely is an issue caused by the server loading a JAR file that contains a different version of that class.
This list of errors for Glassfish mentions grizzly-npn-bootstrap.jar
. See the third error. Looking for the file in combination with your error shows other results that mention the same cause for the problem.
Look for that file in your server installation and rename it to grizzly-npn-bootstrap.old
(or try newer versions of the server), then try again.