简体   繁体   English

使用Gmail SMTP的JavaMail-无法发送邮件

[英]JavaMail, using Gmail SMTP - can't send messages

I want to send an email with JavaMail, but every time I get an exception. 我想使用JavaMail发送电子邮件,但是每次遇到异常时,我都会发送电子邮件。 I've create a JFrame project, and I created an actionevent for a button: 我创建了一个JFrame项目,并为一个按钮创建了一个actionEvent:

  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: 例外是:

屏幕截图

Your screenshot shows an SSL handshake error. 您的屏幕截图显示了SSL握手错误。

You probably have to import their SSL certificate or the root certificate into your own java trust/keystore. 您可能必须将其SSL证书或根证书导入到您自己的java trust / keystore中。

I found a tutorial for this part which describes the single steps. 我找到了这部分的教程 ,其中描述了单个步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM