JavaMail 發(fā)送郵件失敗
問題描述
用java 實(shí)現(xiàn)一個(gè) 發(fā)送郵件的Demo(用的是QQ郵箱) 但發(fā)送總是失敗,QQ郵箱中的 Smtp 服務(wù)也都開啟了,密碼用的是獲取的授權(quán)碼
public static void main(String[] args) { // 收件人電子郵箱 String to = '123@qq.com'; // 發(fā)件人電子郵箱 String from = '456@qq.com'; // 指定發(fā)送郵件的主機(jī)為 smtp.qq.com String host = 'smtp.qq.com'; //QQ 郵件服務(wù)器 // 獲取系統(tǒng)屬性 Properties properties = System.getProperties(); // 設(shè)置郵件服務(wù)器 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'); // 獲取默認(rèn)session對(duì)象 Session session = Session.getDefaultInstance(properties,new Authenticator(){ public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication('456@qq.com', '123123'); //發(fā)件人郵件用戶名、密碼 } }); session.setDebug(true); try{ // 創(chuàng)建默認(rèn)的 MimeMessage 對(duì)象 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!'); // 設(shè)置消息體 message.setText('This is actual message'); // 發(fā)送消息 Transport.send(message); System.out.println('Sent message successfully....from w3cschool.cc'); }catch (MessagingException mex) { mex.printStackTrace(); } }
錯(cuò)誤提示DEBUG SMTP: trying to connect to host 'smtp.qq.com', port 465, isSSL falsejavax.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 targetat 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)
有沒有做過類似的大神指點(diǎn)一下 …^.^…
問題解答
回答1:SSL數(shù)字證書問題,簡(jiǎn)單點(diǎn)去掉數(shù)字證書驗(yàn)證試試 properties.put('mail.smtp.ssl.trust', 'smtp.qq.com'); // 這行先注釋調(diào)
相關(guān)文章:
1. 致命錯(cuò)誤: Class ’appfacadeTest’ not found2. html5 - 如何實(shí)現(xiàn)帶陰影的不規(guī)則容器?3. objective-c - iOS開發(fā)支付寶和微信支付完成為什么跳轉(zhuǎn)到了之前開發(fā)的一個(gè)app?4. css - 移動(dòng)端字體設(shè)置問題5. python - 管道符和ssh傳文件6. javascript - 循環(huán)嵌套多個(gè)promise應(yīng)該如何實(shí)現(xiàn)?7. mysql優(yōu)化 - 關(guān)于mysql分區(qū)8. 請(qǐng)教各位大佬,瀏覽器點(diǎn) 提交實(shí)例為什么沒有反應(yīng)9. css3 - rem布局下,用戶瀏覽器的最小字號(hào)是12px怎么辦?10. javascript - ionic2 input autofocus 電腦成功,iOS手機(jī)鍵盤不彈出
