首页 新闻 会员 周边 捐助

javamail发送邮件失败。

0
悬赏园豆:200 [已关闭问题] 关闭于 2019-01-17 15:13

代码如下:我准备先自己给自己发送,我登录邮箱自己给自己发送是可以的。
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class test_mail {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String from = "自己邮箱";
    String to = "自己邮箱";
    String subject = "test";
    String body = "this is a test email";
    String smtpHost = "服务器host";

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
    props.setProperty("mail.smtp.host", smtpHost); // 发件人的邮箱的 SMTP服务器地址
    props.setProperty("mail.smtp.auth", "true"); // 请求认证,参数名称与具体实现有关
    props.setProperty("mail.smtp.port", "587");
    props.setProperty("mail.smtp.starttls.enable", "true");

    // 创建Session实例对象
    Session session = Session.getDefaultInstance(props);
    // 创建MimeMessage实例对象
    MimeMessage message = new MimeMessage(session);
    try 
    {
        // 设置发件人
        message.setFrom(new InternetAddress(from));
        // 设置收件人
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        // 设置发送日期
        message.setSentDate(new Date());
        // 设置邮件主题
        message.setSubject(subject);
        // 设置纯文本内容的邮件正文
        message.setText(body);
        // 保存并生成最终的邮件内容
        message.saveChanges();
        // 设置为debug模式, 可以查看详细的发送 log
        session.setDebug(true);
        // 获取Transport对象
        //Transport transport = session.getTransport("smtp");
        Transport transport = session.getTransport();
        System.out.println("serversocket buffer is " + transport.getURLName().getPassword());
    
        
        transport.connect(from, "PHP的ehlo头,我以为它是授权码");
        // 发送,message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    catch(AddressException e)
    {
        e.printStackTrace();
    }
    catch(MessagingException e)
    {
        e.printStackTrace();
    }

}

}
错误如下:
javax.mail.MessagingException: Can't send command to SMTP host;
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
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1420)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1408)
at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:847)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:384)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:176)
at test_mail.main(test_mail.java:59)
Caused by: 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
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
at sun.security.ssl.Handshaker.processLoop(Unknown Source)
at sun.security.ssl.Handshaker.process_record(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at sun.security.ssl.AppOutputStream.write(Unknown Source)
at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:101)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1418)
... 7 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 19 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 25 more

公司内部的邮箱,PHP代码是可以的,我使用javamail就一直出这个错误。两三天了,一个文本信息都发不出去,求各位大佬帮忙看看,万分感谢!穷人没多少园豆全上了,求帮忙!

AHRI-9245的主页 AHRI-9245 | 菜鸟二级 | 园豆:206
提问于:2019-01-17 13:02
< >
分享
所有回答(1)
0

解决了吗,大哥,我也刚遇到这个错,找了好久没找到解决办法。

卡卡罗特琪琪 | 园豆:202 (菜鸟二级) | 2021-02-24 23:35

时间过的有点久了,我没记错我当时是换了个电脑运行就可以了。

支持(0) 反对(0) AHRI-9245 | 园豆:206 (菜鸟二级) | 2021-02-25 08:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册