首頁  >  問答  >  主體

JavaMail 傳送郵件失敗

用java 實作一個 發送郵件的Demo(用的是QQ郵箱) 但發送總是失敗,QQ郵箱中的 Smtp 服務也都開啟了,密碼用的是取得的授權碼

public static void main(String[] args) {
         // 收件人电子邮箱
          String to = "123@qq.com";
     
          // 发件人电子邮箱
          String from = "456@qq.com";
     
          // 指定发送邮件的主机为 smtp.qq.com
          String host = "smtp.qq.com";  //QQ 邮件服务器
     
          // 获取系统属性
          Properties properties = System.getProperties();
     
          // 设置邮件服务器
          properties.setProperty("mail.smtp.host", host);
     
          properties.put("mail.smtp.auth", "true");
          properties.put("mail.smtp.port", "465"); 
          properties.put("mail.smtp.socketFactory.port", "465");
          properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
          properties.put("mail.smtp.starttls.enable", "true");
          properties.put("mail.smtp.socketFactory.fallback", "false");
          properties.put("mail.smtp.starttls.enable", "true");  
          properties.put("mail.smtp.ssl.trust", "smtp.qq.com");
          // 获取默认session对象
          Session session = Session.getDefaultInstance(properties,new Authenticator(){
            public PasswordAuthentication getPasswordAuthentication()
            {
             return new PasswordAuthentication("456@qq.com", "123123"); //发件人邮件用户名、密码
            }
           });
          
          session.setDebug(true);
     
          try{
             // 创建默认的 MimeMessage 对象
             MimeMessage message = new MimeMessage(session);
     
             // Set From: 头部头字段
             message.setFrom(new InternetAddress(from));
     
             // Set To: 头部头字段
             message.addRecipient(Message.RecipientType.TO,
                                      new InternetAddress(to));
     
             // Set Subject: 头部头字段
             message.setSubject("This is the Subject Line!");
     
             // 设置消息体
             message.setText("This is actual message");
     
             // 发送消息
             Transport.send(message);
             System.out.println("Sent message successfully....from w3cschool.cc");
          }catch (MessagingException mex) {
             mex.printStackTrace();
          }
    }
    

錯誤提示
DEBUG SMTP: trying to connect to host "smtp.qq.com", port 465, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com , port: 465;
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.openServer(SMTPTransport.java:1972)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.syx.email.MailTest3.main(MailTest3.java:68)

有沒有做過類似的大神指點一下 …^.^…

世界只因有你世界只因有你2684 天前717

全部回覆(1)我來回復

  • 仅有的幸福

    仅有的幸福2017-05-17 10:02:14

    SSL數位憑證問題,簡單點去掉數位憑證驗證試試
    properties.put("mail.smtp.ssl.trust", "smtp.qq.com"); // 這行先註解調

    回覆
    0
  • 取消回覆