gpt4 book ai didi

email - 从 localhost 使用 Spring Boot 发送电子邮件

转载 作者:行者123 更新时间:2023-12-04 02:59:39 26 4
gpt4 key购买 nike

我想从本地主机(mac 或 linux)发送电子邮件到某个地方(假设是几个 gmail 地址)。只发送电子邮件,从不接收电子邮件,就像通知推送系统一样。

我知道 mac/linux 有一个“sendmail”程序,但现在我需要使用 spring boot 2.0 版发送电子邮件。

在我的 Mac 上:
$ which sendmail
/usr/sbin/sendmail

我在这里阅读了 Spring Boot 电子邮件教程 http://www.baeldung.com/spring-email ,并在我的代码中进行了类似的配置,但它不起作用。在本教程中,mailsendr 的“主机”是“smtp.gmail.com”,在这种情况下,我需要“主机”是 localhost(我的 Spring Boot 应用程序将在其上运行的机器)。

这是我在 application.properties 中的电子邮件配置:

spring.mail.host=localhost
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.auth=false
spring.mail.properties.mail.smtp.starttls.enable=false
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000

当通过 JavaMailSender(autowired) 发送电子邮件时,spring boot 会抛出以下错误:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout 5000;
nested exception is:
java.net.ConnectException: Connection refused (Connection refused). Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout 5000;
nested exception is:
java.net.ConnectException: Connection refused (Connection refused)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:446) ~[spring-context-support-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:321) ~[spring-context-support-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:310) ~[spring-context-support-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at com.ibm.gem.service.EmailServiceImpl.sendSimpleMessage(EmailServiceImpl.java:33) ~[classes/:na]
at com.ibm.gem.component.ApplicationStartup.onApplicationEvent(ApplicationStartup.java:39) [classes/:na]
at com.ibm.gem.component.ApplicationStartup.onApplicationEvent(ApplicationStartup.java:1) [classes/:na]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:399) [spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:353) [spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.boot.context.event.EventPublishingRunListener.running(EventPublishingRunListener.java:103) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplicationRunListeners.running(SpringApplicationRunListeners.java:78) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:341) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at com.ibm.gem.PerfInsightApplication.main(PerfInsightApplication.java:11) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.0.RELEASE.jar:2.0.0.RELEASE]
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout 5000
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209) ~[javax.mail-1.6.1.jar:1.6.1]
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740) ~[javax.mail-1.6.1.jar:1.6.1]
at javax.mail.Service.connect(Service.java:366) ~[javax.mail-1.6.1.jar:1.6.1]
at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:515) ~[spring-context-support-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:435) ~[spring-context-support-5.0.4.RELEASE.jar:5.0.4.RELEASE]
... 21 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_151]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_151]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_151]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_151]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_151]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_151]
at com.sun.mail.util.WriteTimeoutSocket.connect(WriteTimeoutSocket.java:115) ~[javax.mail-1.6.1.jar:1.6.1]
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:351) ~[javax.mail-1.6.1.jar:1.6.1]
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:239) ~[javax.mail-1.6.1.jar:1.6.1]
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175) ~[javax.mail-1.6.1.jar:1.6.1]
... 25 common frames omitted

我认为我在 application.properties 中的配置是错误的,但我不知道正确的配置是什么。 mac/linux 没有内置的 smtp 服务器,是吗?我不知道,我不想安装一个。我只想要类似“sendmail”程序的功能。使用“sendmail”程序时,不需要指定协议(protocol)、端口、ssl等参数,不知道为什么在使用spring boot的时候要提供这些信息,也不知道怎么办在我的电脑上找到这些信息。

有谁能够帮助我?

提前致谢。

最佳答案

问题解决了。

原来 mac(以及我相信的大多数 linux 系统)都带有预装的邮件系统 postfix 。在使用 spring boot 发送电子邮件之前,您需要启动邮件系统(其中包括一个 smtp 服务器)。

使用以下命令启动和检查后缀状态:
$ sudo postfix status
$ sudo postfix start

在 mac 上,我使用以下命令检查 smtp 是否正在运行:
$ sudo lsof -i :smtp
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
master 84359 root 13u IPv4 0xacc57a372acc10b 0t0 TCP localhost:smtp (LISTEN)
master 84359 root 14u IPv6 0xacc57a379b3eb63 0t0 TCP localhost:smtp (LISTEN)
postscree 84562 _postfix 6u IPv4 0xacc57a372acc10b 0t0 TCP localhost:smtp (LISTEN)
postscree 84562 _postfix 7u IPv6 0xacc57a379b3eb63 0t0 TCP localhost:smtp (LISTEN)

而在spring boot application.properties中,我只需要一行配置:
spring.mail.host=localhost

有效!

关于email - 从 localhost 使用 Spring Boot 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50317404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com