#1506  Problem with connecting to email server
Closed
zwsu opened 6 months ago

在配置邮箱服务的时候出现问题,SMTP Port为465,超时时间设置了300s,报错信息如下,这是什么原因导致的呢?

Please wait...
java.lang.RuntimeException: javax.mail.MessagingException: Exception reading response;
      nested exception is:
    	java.net.SocketTimeoutException: Read timed out
    	at io.onedev.server.mail.DefaultMailManager.sendMail(DefaultMailManager.java:321)
    	at io.onedev.server.web.page.admin.mailsetting.MailSettingPage$2$2.call(MailSettingPage.java:139)
    	at io.onedev.server.web.page.admin.mailsetting.MailSettingPage$2$2.call(MailSettingPage.java:87)
    	at io.onedev.server.persistence.DefaultSessionManager.call(DefaultSessionManager.java:90)
    	at io.onedev.server.web.page.admin.mailsetting.MailSettingPage$2.runTask(MailSettingPage.java:87)
    	at io.onedev.server.web.component.taskbutton.TaskButton$2.call(TaskButton.java:174)
    	at io.onedev.server.web.component.taskbutton.TaskButton$2.call(TaskButton.java:145)
    	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    	at io.onedev.server.security.SecurityUtils$1.run(SecurityUtils.java:335)
    	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    	at java.base/java.lang.Thread.run(Thread.java:833)
    Caused by: javax.mail.MessagingException: Exception reading response;
      nested exception is:
    	java.net.SocketTimeoutException: Read timed out
    	at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2460)
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2187)
    	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
    	at javax.mail.Service.connect(Service.java:366)
    	at javax.mail.Service.connect(Service.java:246)
    	at javax.mail.Service.connect(Service.java:195)
    	at javax.mail.Transport.send0(Transport.java:254)
    	at javax.mail.Transport.send(Transport.java:124)
    	at io.onedev.server.mail.DefaultMailManager.sendMail(DefaultMailManager.java:319)
    	... 11 more
    Caused by: java.net.SocketTimeoutException: Read timed out
    	at java.base/sun.nio.ch.NioSocketImpl.timedRead(NioSocketImpl.java:288)
    	at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:314)
    	at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:355)
    	at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:808)
    	at java.base/java.net.Socket$SocketInputStream.read(Socket.java:966)
    	at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:126)
    	at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:244)
    	at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:263)
    	at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:106)
    	at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2440)
    	... 19 more
   
Robin Shen commented 6 months ago

试下通过其他邮件客户端可以连接发送邮件吗

zwsu commented 6 months ago

使用QQ邮箱是可以的,就是内部企业邮箱不行

Robin Shen commented 6 months ago

通过其他邮件客户端比如foxmail连你们的企业邮箱可以吗?

zwsu commented 6 months ago

通过foxmail是可以的,使用相同的邮箱配置,在别的业务系统里面也可以正常发送提醒邮件

Robin Shen commented 6 months ago

Enable StartTLS 选项打开或者关闭都不行吗

Robin Shen commented 6 months ago

确认从安装OneDev的机器能够连接到邮件服务器的465端口吗 (可以通过telnet验证下)

zwsu commented 6 months ago

Enable StartTLS打开和关闭都不行,在安装的机器上telnet网络是通的

Robin Shen commented 6 months ago

这就奇怪了,你们用的什么邮件服务器?

zwsu commented 6 months ago

Coremail的邮件系统 image.png

zwsu commented 6 months ago

从昨天一直尝试Issues里面的各种解决方案,都没能解决,特来问问,看有没有别的排查思路了

Robin Shen commented 6 months ago

不知道你对Java是否熟悉,可以按照这个教程写个简单的程序试下应该用什么选项才能发送成功:

https://www.digitalocean.com/community/tutorials/javamail-example-send-mail-in-java-smtp

这样我在OneDev这里可以做相应的调整。

zwsu commented 6 months ago

OK,我写一下,有新的进展给您同步

zwsu commented 6 months ago
@Test
public void SSLEmailTest() throws GeneralSecurityException {
    final String fromEmail = "XXX@companyname.com.cn"; 
    final String password = "XXXX"; 
    final String toEmail = "XXXX@companyname.com.cn"; 

    System.out.println("SSLEmail Start");
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtpav.companyname.com.cn"); //SMTP Host
    props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication
    props.put("mail.smtp.port", "465"); //SMTP Port
    props.put("mail.smtp.socketFactory.port", "465"); //SSL Port
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); //SSL Factory Class
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.ssl.enable", "true");
    MailSSLSocketFactory sf = new MailSSLSocketFactory();
    sf.setTrustAllHosts(true);
    props.put("mail.smtp.ssl.socketFactory", sf);

    Authenticator auth = new Authenticator() {
        //override the getPasswordAuthentication method
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(fromEmail, password);
        }
    };

    Session session = Session.getDefaultInstance(props, auth);
    System.out.println("Session created");
    EmailUtil.sendEmail(session, toEmail,"SSLEmail Testing Subject", "SSLEmail Testing Body");
}

通过测试,添加了mail.smtp.ssl.socketFactory之后可以正常发送,麻烦您看下如何在页面上配置

Robin Shen commented 6 months ago

关键是需要这个配置吗: sf.setTrustAllHosts(true);

去掉是不是就不可以了?

无条件信任其他主机是个安全隐患。

Robin Shen commented 6 months ago

你们邮件服务器是用自签名的证书?

zwsu commented 6 months ago

不是自签名的,是China Financial Certification Authority签发的证书,我试试把证书手动导入看看

zwsu commented 6 months ago

将来生产环境手动导入证书是不允许的,有考虑放开这个配置吗?让用户自定义配置

Robin Shen commented 6 months ago

我这边再试试,有些配置跟我这边设定有点冲突。

Robin Shen commented 6 months ago

升级到 build #3997,然后 mail setting 的 ssl setting 选择 "Implicit SSL"。 对于证书问题,在信任环境中可以勾选 ”bypass certificate check“。也可以按如下教程导入证书:

https://docs.onedev.io/administration-guide/trust-self-signed-certificates#trust-self-signed-certificates-on-server

这个导入不影响 JVM 的全局证书。

Robin Shen changed state to 'Closed' 6 months ago
Previous Value Current Value
Open
Closed
zwsu commented 6 months ago

测试没什么问题了,感谢大佬支持,另外看到已经有付费的版本,希望onedev越来越好!

Robin Shen commented 6 months ago

不客气,😃

issue 1 of 1
Type
Question
Priority
Normal
Assignee
Labels
No labels
Issue Votes (0)
Watchers (4)
Reference
onedev/server#1506
Please wait...
Page is in error, reload to recover