之前用163邮箱发邮件时明明是成功的,但是使用中国移动自己的邮箱时,无论如何在linux服务器中都发送不成功,一开始报如下错误:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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成功连接了,启动项目后发送邮件依然报错,只不过换了一个错误而已:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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的,为什么到了程序中就连接不上。如此折腾了好久,最终由组长出马解决了问题,我也才终于知道了原因。竟然是项目中如下代码导致:
1
2
3
4
5
6
7
8
9
10
11
|
// 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实现发送邮件功能时碰到的坑的全部叙述,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!