【问题标题】:JavaMail, using Gmail SMTP - can't send messagesJavaMail,使用 Gmail SMTP - 无法发送邮件
【发布时间】:2014-09-13 17:03:26
【问题描述】:

我想用 JavaMail 发送一封电子邮件,但每次我都收到异常。我创建了一个 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);
  }

}                                        

}

例外是:

【问题讨论】:

  • 如果我理解的话。您是否已经在服务器中安装了 .exe?问候
  • 什么意思?什么.exe?不,我想...我有一台华硕笔记本电脑,并安装了 NetBeans。我为项目 CLASSPATH 添加了 JavaMail,但没有工作。
  • DavidPostill - 尝试过,但不起作用。 + 我正在尝试使用 SSL 创建它。

标签: java smtp gmail jakarta-mail


【解决方案1】:

您的屏幕截图显示 SSL 握手错误。

您可能必须将他们的 SSL 证书或根证书导入您自己的 java 信任/密钥库。

我为这部分找到了一个tutorial,它描述了单个步骤。

【讨论】:

  • 见JavaMail FAQ entry。通常,对于 Gmail,您不应该遇到此问题。您的 JDK 安装可能有问题,或者您遇到了阻止您连接的防火墙。您也可以尝试在 NetBeans 之外运行您的应用程序,看看是否有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-14
  • 2015-07-25
  • 2021-07-06
  • 2015-08-09
  • 1970-01-01
  • 2018-09-13
相关资源
最近更新 更多