无法从 docker 容器内发送邮件

Sub*_*nta 6 email smtp docker

我有一个 spring mvc 应用程序,我已经对其进行了 dockerized。请按照我的方式查看此链接dockerized 。在容器化之前,部署在 tomcat 中的普通 war 能够使用主机 smtp.gmail.com 和端口 587 发送电子邮件。

我的 bean 定义是这样的:

<beans:bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <beans:property name="host" value="smtp.gmail.com" />
    <beans:property name="port" value="587" />
    <beans:property name="username" value="xxxxxx" />
    <beans:property name="password" value="xxxxx" />
    <beans:property name="javaMailProperties">
         <beans:props>
              <beans:prop key="mail.smtp.auth">true</beans:prop>
              <beans:prop key="mail.smtp.starttls.enable">true</beans:prop>
         </beans:props>
    </beans:property>
    </beans:bean>
Run Code Online (Sandbox Code Playgroud)

现在我明白了:

    org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
  nested exception is:
        java.net.ConnectException: Connection refused (Connection refused). Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
  nested exception is:
        java.net.ConnectException: Connection refused (Connection refused); message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
  nested exception is:
        java.net.ConnectException: Connection refused (Connection refused)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
Run Code Online (Sandbox Code Playgroud)

Jos*_*hua 1

查看您提供的链接并假设这正是您为 Dockerfile 复制的内容,您需要为邮件客户端公开一个额外的端口。

如果您还不知道,默认情况下,当您创建容器时,它不会向外界发布任何端口。

在 Dockerfile 中,您可以使用EXPOSE. 在您的情况下,您需要 EXPOSE 587启用 SMTP 通信

此外,将来如果您需要公开具有不同协议的其他端口,您可以使用

EXPOSE 587/tcp(默认为TCP)

如果您想了解更多信息,EXPOSE我可以从 Docker 文档获取信息: https: //docs.docker.com/engine/reference/builder/

  • Expose 用于侦听端口,而不是传出端口,它比任何内容都更多...来自 docker: ` EXPOSE 指令实际上并不发布端口。它充当构建映像的人员和运行容器的人员之间的一种文档,关于哪些端口旨在“ (5认同)