You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Exim错误:SMTP初始连接后超时(Operation timed out)

问题:Exim Docker镜像发送邮件至Gmail时SMTP连接超时

现象描述

使用基于Alpine的Exim Docker镜像发送通知邮件,发件地址mailid@domain.com,收件地址username@gmail.com,出现SMTP连接超时错误。

错误日志

2023-07-27 05:36:21 1qOtfh-00000C-0E <= mailid@domain.com H=(b0264265d64d) [192.168.0.3] P=esmtp S=718 id=348112319.0.1690436181096.JavaMail.mailid@domain.com
2023-07-27 05:38:31 1qOtfh-00000C-0E H=gmail-smtp-in.l.google.com [74.125.200.27]: SMTP timeout after initial connection: Operation timed out
2023-07-27 05:42:53 1qOtfh-00000C-0E H=alt2.gmail-smtp-in.l.google.com [142.250.141.27]: SMTP timeout after initial connection: Operation timed out

Docker镜像配置(Dockerfile)

FROM alpine:latest
RUN echo -en 'http://dl-cdn.alpinelinux.org/alpine/edge/main\n\
http://dl-cdn.alpinelinux.org/alpine/edge/testing\n\
http://dl-cdn.alpinelinux.org/alpine/edge/community\n' >> /etc/apk/repositories

RUN apk update && apk --update add exim tini && rm -rf /var/cache/apk/*
RUN mkdir -p /var/log/exim && \
touch /var/log/exim/mainlog /var/log/exim/paniclog /var/log/exim/rejectlog && \
chown exim:exim /var/log/exim/*log

COPY exim.conf /etc/exim/

COPY main.cf /etc/postfix/

COPY resolv.conf /etc/


ENTRYPOINT ["/sbin/tini"]

CMD ["/usr/sbin/exim", "-bd", "-q10m"]

EXPOSE 25

Exim核心配置片段

dnslookup:
      driver = dnslookup
      domains = ! +local_domains
      transport = remote_smtp
      ignore_target_hosts = 0.0.0.0 : 127.0.0.0/8
      # if ipv6-enabled then instead use:
      # ignore_target_hosts = <;; 0.0.0.0 ; 127.0.0.0/8 ; ::1
      self = pass
      no_more

排查与解决步骤

1. 验证容器网络连通性

进入容器内部,测试能否访问Gmail邮件服务器:

docker exec -it <容器ID/名称> ping gmail-smtp-in.l.google.com
docker exec -it <容器ID/名称> telnet gmail-smtp-in.l.google.com 25
  • 若ping不通:检查容器内resolv.conf的DNS配置是否正确,或切换Docker网络模式(如host模式)测试路由。
  • 若telnet超时:多数情况是宿主机网络的25端口被ISP/云服务商封禁,建议改用Gmail的587端口(STARTTLS)。

2. 调整Exim配置使用587端口

修改Exim的remote_smtp传输规则,指定端口并启用STARTTLS:

remote_smtp:
      driver = smtp
      port = 587
      hosts_require_tls = *

同时在Dockerfile中补充openssl依赖,确保Exim支持TLS:

RUN apk update && apk --update add exim tini openssl && rm -rf /var/cache/apk/*

3. 清理冗余配置

Dockerfile中复制了Postfix的main.cf但未使用,属于冗余操作,删除该行:

# COPY main.cf /etc/postfix/

4. 完善Exim域名配置

在exim.conf中明确local_domains,避免路由逻辑错误:

local_domains = localhost : domain.com

5. 直接测试邮件发送

在容器内用Exim命令直接测试,查看实时输出定位问题:

echo "Test email content" | exim -v username@gmail.com -f mailid@domain.com

内容的提问来源于stack exchange,提问作者SUBIN K SOMAN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 19:42:11