0

I want to send an email with JavaMail, but every time I get an exception. I've create a JFrame project, and I created an actionevent for a button:

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  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() {
              @Override
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication("xyz@gmail.com", "###");
              }
          }
  );
  //I've tried also this version of the Session:
  //Session session = Session.getInstance(props,new NewEmpty("xyz@gmail.com","###"));
  try {
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("xyz@gmail.com"));
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("zyx@gmail.com"));
      message.setSubject("Hi");
      message.setText("Helló!");

      Transport.send(message);
      JOptionPane.showMessageDialog(null, "Sent!");
  }catch(Exception e) {
      JOptionPane.showMessageDialog(null, e);
  }

}                                        

}

The Exception is:

Screenshot

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • If I understood. Have you already installed an .exe in your server? Regards – ivanfromer Jul 22 '14 at 18:23
  • possible duplicate of [Sending Email via gmail smtp server in JAVA](http://stackoverflow.com/questions/15597616/sending-email-via-gmail-smtp-server-in-java) – DavidPostill Jul 22 '14 at 18:43
  • What do you mean? What .exe? No, I think... I have an Asus laptop, and installed NetBeans. I added JavaMail for the project CLASSPATH, but not working. – Fgd Fdg Jul 22 '14 at 18:44
  • DavidPostill - Tried, but not working. + I'm trying to create it with SSL. – Fgd Fdg Jul 22 '14 at 19:12
  • Check this one as well http://stackoverflow.com/questions/1990454/using-javamail-to-connect-to-gmail-smtp-server-ignores-specified-port-and-tries – DavidPostill Jul 22 '14 at 19:20
  • the problem was the Avast firewall. I turned off avast and everything is fine now. Thanks to everyone! – Fgd Fdg Jul 23 '14 at 13:14

1 Answers1

0

Your screenshot shows an SSL handshake error.

You probably have to import their SSL certificate or the root certificate into your own java trust/keystore.

I found a tutorial for this part which describes the single steps.

Udo Held
  • 12,314
  • 11
  • 67
  • 93
  • See this [JavaMail FAQ entry](http://www.oracle.com/technetwork/java/javamail/faq/index.html#installcert). Normally, for Gmail, you shouldn't run into this problem. Possibly there's something wrong with your JDK installation, or possibly you're running into a firewall that's preventing you from connecting. You might also try running your application outside of NetBeans to see if that helps. – Bill Shannon Jul 22 '14 at 19:32