我试着在Spring中测试我的Gmail,并遵循了这个教程。我已经按照本教程中的指定实现了它,但是我的EmailService抛出一个MailSendException:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 2525; timeout 5000;
nested exception is:
java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 2525; timeout 5000;
nested exception is:
java.net.ConnectException: Connection refused: connect
; message exception details (1) are:
Failed message 1:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 2525; timeout 5000;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2210)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:722)
at javax.mail.Service.connect(Service.java:342)有人知道怎么解决这个问题吗?(从未测试过诸如SMTP/Email之类的内容,因此只需遵循上面的教程。编辑:我可以发送电子邮件没有任何问题手动,但我需要测试。我的application.yml:
spring.mail.host: smtp.gmail.com
spring.mail.port: 587
spring.mail.properties.mail.smtp.starttls.enable: true
spring.mail.username: ************@gmail.com
spring.mail.password: ***************
spring.mail.properties.mail.smtp.starttls.required: true
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.connectiontimeout: 5000
spring.mail.properties.mail.smtp.timeout: 5000
spring.mail.properties.mail.smtp.writetimeout: 5000发布于 2020-09-01 13:15:41
从日志中可以看到,Spring试图连接到端口2525,但无法连接。这意味着在测试执行期间,这个端口上没有正在运行的邮件服务器--这应该由JUnit规则实现提供(如果您正在使用JUnit5使用扩展)
确保您的测试配置已正确设置。这意味着,检查您的测试代码。
@Rule
public SmtpServerRule smtpServerRule = new SmtpServerRule(2525);火柴
src/test/resources/application.yml
spring:
mail:
default-encoding: UTF-8
host: localhost
jndi-name:
username: username
password: secret
port: 2525
properties:
mail:
debug: false
smtp:
debug: false
auth: true
starttls: true
protocol: smtp
test-connection: false我还建议更新来自教程的代码并添加测试用户--因为它可以与安全协议一起使用。
public class SmtpServerRule extends ExternalResource {
// omitted for brevity
@Override
protected void before() throws Throwable {
super.before();
smtpServer = new GreenMail(new ServerSetup(port, null, "smtp"));
smtpServer.addUser("username", "secret");
smtpServer.start();
}
// omitted for brevity
}另外,更具体的设置请参考官方的GreenMail文档。
https://stackoverflow.com/questions/61820439
复制相似问题