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成功连接:
参考:http://blog.csdn.net/mchdba/article/details/49339715

但是尽管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发送邮件)