Java完成發送郵件功效時碰著的坑。本站提示廣大學習愛好者:(Java完成發送郵件功效時碰著的坑)文章只能為提供參考,不一定能成為您想要的結果。以下是Java完成發送郵件功效時碰著的坑正文
之前用163郵箱發郵件時明明是勝利的,然則應用中國挪動本身的郵箱時,不管若何在linux辦事器中都發送不勝利,一開端報以下毛病:
javax.mail.MessagingException: Unknown SMTP host: mail.chinamobilesz.com at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1959) ~ [mail-1.4.7.jar:1.4.7] at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:65 4) ~[mail-1.4.7.jar:1.4.7] at javax.mail.Service.connect(Service.java:317) ~[mail-1.4.7.jar:1.4.7] at javax.mail.Service.connect(Service.java:176) ~[mail-1.4.7.jar:1.4.7] at javax.mail.Service.connect(Service.java:125) ~[mail-1.4.7.jar:1.4.7] at javax.mail.Transport.send0(Transport.java:194) ~[mail-1.4.7.jar:1.4.7 ] at javax.mail.Transport.send(Transport.java:124) ~[mail-1.4.7.jar:1.4.7] at com.cmsz.processOm.service.impl.EmailSendServiceImpl.sendEmail(EmailS endServiceImpl.java:107) ~[EmailSendServiceImpl.class:na] at com.cmsz.processOm.controller.WorkOrderController$SendEmail.isSendEma il(WorkOrderController.java:555) [WorkOrderController$SendEmail.class:na] at com.cmsz.processOm.controller.WorkOrderController$SendEmail.run(WorkO rderController.java:524) [WorkOrderController$SendEmail.class:na] at java.lang.Thread.run(Thread.java:745) [na:1.7.0.11] ......
在linux中telnet mail.chinamobilesz.com 25也欠亨,然則直接把mail.chinamobilesz.com換成ip便可以了,在網上查找材料並參考其他文章後,設置了主機名和ip的映照後終究telnet勝利銜接:
然則雖然telnet勝利銜接了,啟動項目後發送郵件仍然報錯,只不外換了一個毛病罷了:
javax.mail.MessagingException: Could not connect to SMTP host: mail.chinamobilesz.com, port: 25 at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961) ~[mail-1.4.7.jar:1.4.7] at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) ~[mail-1.4.7.jar:1.4.7] at javax.mail.Service.connect(Service.java:317) ~[mail-1.4.7.jar:1.4.7] at javax.mail.Service.connect(Service.java:176) ~[mail-1.4.7.jar:1.4.7] at javax.mail.Service.connect(Service.java:125) ~[mail-1.4.7.jar:1.4.7] at javax.mail.Transport.send0(Transport.java:194) ~[mail-1.4.7.jar:1.4.7] at javax.mail.Transport.send(Transport.java:124) ~[mail-1.4.7.jar:1.4.7] at com.cmsz.processOm.service.impl.EmailSendServiceImpl.sendEmail(EmailSendServiceImpl.java:107) ~[EmailSendServiceImpl.class:na] at com.cmsz.processOm.controller.WorkOrderController$SendEmail.isSendEmail(WorkOrderController.java:555) [WorkOrderController$SendEmail.class:na] at com.cmsz.processOm.controller.WorkOrderController$SendEmail.run(WorkOrderController.java:524) [WorkOrderController$SendEmail.class:na] at java.lang.Thread.run(Thread.java:745) [na:1.7.0.11] ......
查找許多材料照樣沒能處理成績,不明確明明可以telnet的,為何到了法式中就銜接不上。如斯折騰了很久,終究由組長出馬處理了成績,我也才終究曉得了緣由。居然是項目中以下代碼招致:
// SSL加密 MailSSLSocketFactory sf = null; try { sf = new MailSSLSocketFactory(); // 設相信任一切的主機 sf.setTrustAllHosts(true); proerties.put("mail.smtp.ssl.enable", "true"); proerties.put("mail.smtp.ssl.socketFactory", sf); } catch (GeneralSecurityException e) { e.printStackTrace(); }
應用了這一段代碼後,銜接的是465端口,不是25,然則現實上挪動供給給我的郵箱沒有開465這個端口,是以便形成豈論是465照樣25端口都銜接不上,因而去失落這段代碼後,啟動項目再次發送郵件便勝利了。
以上所述是小編給年夜家引見的Java完成發送郵件功效時碰著的坑的全體論述,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!