国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

JavaMail 發(fā)送郵件失敗

瀏覽:115日期:2023-12-14 16:29:59

問題描述

用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)

標(biāo)簽: java
相關(guān)文章:
主站蜘蛛池模板: 昌江| 甘孜县| 潞西市| 泸州市| 赤壁市| 辽源市| 荥阳市| 富源县| 清新县| 土默特右旗| 尚义县| 将乐县| 双牌县| 仪征市| 丹阳市| 兰州市| 屏东市| 安岳县| 修水县| 灵武市| 清原| 丹棱县| 始兴县| 潞城市| 平罗县| 榆社县| 潜山县| 交城县| 河南省| 尼木县| 葵青区| 桦南县| 哈尔滨市| 汝南县| 福海县| 德昌县| 丹江口市| 河池市| 乌兰浩特市| 天等县| 无极县|