使用 springboot 发送邮件出现的问题
今天在使用springboot 发送邮件的功能,进行单元测试时一直报错
错误信息
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 -1;
nested exception is:
java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
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, 25; timeout -1;
大致的意思是连接超时,
配置
# 邮件配置
spring.mail.host=smtp.163.com
spring.mail.username=开启授权码的账号
spring.mail.password=授权码
spring.mail.default-encoding=UTF-8
依赖
<!-- springboot 发送邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
向springboot 容器注入JavaMailSender
package top.codedesigner.www.visit.config;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
@Configuration
public class WebConfig {
@Bean
public JavaMailSenderImpl JavaMailSender(){
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
return mailSender;
}
}
发送简单邮件方法
package top.codedesigner.www.visit.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import top.codedesigner.www.util.VerificationUtil;
import top.codedesigner.www.visit.service.emailService;
import java.util.Date;
@Service
public class emailServiceImpl implements emailService {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String sender;
@Override
public boolean sendEmailCode() {
//生成email code;
Integer i=VerificationUtil.getVerificationCode();
SimpleMailMessage email=new SimpleMailMessage();
System.out.println(sender);
email.setSubject("测试");
email.setFrom(sender);
email.setTo("3106889243@qq.com");
email.setSentDate(new Date());
email.setText("邮件验证码"+i);
javaMailSender.send(email);
return false;
}
}
进行单元测试
package top.codedesigner.www;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import top.codedesigner.www.visit.service.emailService;
@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailTest {
@Autowired
emailService emailservice;
@Test
public void TestSendEmail(){
boolean flag=emailservice.sendEmailCode();
System.out.println(flag);
}
}
请问这是什么原因造成的又该怎么解决呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没用过spring的mail,具体怎么用不敢瞎说。但你上面的代码的逻辑不对。
前面的代码中增加了配置和依赖,按SpringBoot的惯例,肯定能够自动生成一个可用的发送Email的Bean,然后我们再向发送邮件的代码中如下注入Bean就行:
但是在后面的代码中,你却手动又写了一个Bean,这将导致:即使SpringBoot会默认根据配置提供一个发送邮件的Bean,也会被你的如下定义Bean的代码覆盖:
综上:
① 如果要使用Sping Boot根据config自动创建MailSender的功能,则不要手动的声明Bean。把
WebConfig
中的JavaMailSenderImpl
删除掉就好。② 如果想自己手动的设置一个
JavaMailSender
,则可以尝试如下改造:最后,你可以在JavaMailSenderImpl文档中找到更多的信息。
希望能够帮到你。