【问题标题】:Sending email using a servlet使用 servlet 发送电子邮件
【发布时间】:2010-12-19 11:50:11
【问题描述】:

我必须通过 servlet 发送电子邮件,我尝试了很多代码,但它们都不能用于 gmail 帐户,有什么想法吗?

 java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.auth", "true");

        Authenticator auth =  new SMTPAuthenticator();
        //auth = null;
        Session sess = Session.getInstance(props, auth);

        sess.setDebug(false);
           // -- Create a new message --
           Message msg = new MimeMessage(sess);

           // -- Set the FROM and TO fields --
           msg.setFrom(new InternetAddress("myemail@gmail.com"));
           msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
             "sendtoemail", false));
           msg.setSubject("Test");
           msg.setText("Yippe");
           msg.setSentDate(new Date());
           Transport.send(msg);

           public class SMTPAuthenticator extends javax.mail.Authenticator {
           @Override
           public PasswordAuthentication getPasswordAuthentication() {
           String username = "myemail@gmail.com";
           String password = "mypass";
           return new PasswordAuthentication(username, password);
           }

}

此代码抛出 javax.mail.MessagingException: Could not convert socket to TLS 异常

【问题讨论】:

    标签: java email servlets


    【解决方案1】:

    只是切换到 commons-email 不会消除此异常。此外,切换到 commons-email 会导致异常中有用信息的数量减少 - 异常日志中的以下行将不再存在:

    nested exception is:
        javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: 
        sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    

    在这种情况下,真正的线索是嵌套异常。

    要修复它,您需要将电子邮件服务器证书安装到 JVM 密钥库。默认密钥库的位置取决于 java 发行版。就我而言,它是/etc/java-6-sun/security/cacert

    添加证书后认证成功。

    【讨论】:

      【解决方案2】:

      使用commons-email

      public static Email createMail(){
          Email email = SimpleEmail();
      
      
          email.setHostName(Settings.getValue("smtp.host"));
          email.setSmtpPort(Integer.parseInt(Settings.getValue("smtp.port")));
          String username = Settings.getValue("smtp.user");
          if (username.length() > 0) {
              email.setAuthentication(username, Settings.getValue("smtp.password"));
          }
      
          boolean useSSL = false;
          try {
              useSSL = Boolean.parseBoolean(Settings.getValue("smtp.ssl"));
          } catch (Exception ex) {
              // ignore - property not set
          }
      
          email.setSSL(useSSL);
          email.setCharset("utf-8");
      
          return email;
      }
      
      public sendMail(String recipientEmail) {
           Email email = createMail();
           email.addTo(recipientEmail);
           email.setFrom("no-reply@example.org");
           email.setSubject("Your subject herE");
           email.setMsg("Your message here");
           email.send();
      }
      

      【讨论】:

      • 如何将它添加到我的..netbeans 项目中?
      • 下载jar并将其添加到构建路径。
      猜你喜欢
      • 2014-01-28
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 2016-04-17
      • 2017-12-07
      • 1970-01-01
      相关资源
      最近更新 更多