Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用servlet发送电子邮件_Java_Email_Servlets - Fatal编程技术网

Java 使用servlet发送电子邮件

Java 使用servlet发送电子邮件,java,email,servlets,Java,Email,Servlets,我必须通过servlet发送电子邮件,我已经尝试了很多代码,但是没有一个可以与gmail帐户一起工作,有什么想法吗 java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); pro

我必须通过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.MessaginException:无法将套接字转换为TLS 异常

使用

使用


仅仅切换到commons email不会消除此异常。此外,切换到commons电子邮件将减少异常中的有用信息量-异常日志中的以下行将不再出现:

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


在我添加证书后,身份验证成功。

仅切换到公用电子邮件不会消除此异常。此外,切换到commons电子邮件将减少异常中的有用信息量-异常日志中的以下行将不再出现:

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


添加证书后,身份验证成功。

如何将其添加到..netbeans项目?下载jar并将其添加到生成路径。如何将其添加到..netbeans项目?下载jar并将其添加到生成路径。