Springboot发送邮件
添加依赖
在pom.xml中添加依赖
<!-- 添加发送邮件依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置邮件参数
#配置发送邮件
spring:
mail:
default-encoding: UTF-8
#发送邮件主机
host: smtp.qq.com
#用户名,密码(密码是授权码)
username: xx@qq.com
password: 授权码
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
发送邮件的Controller
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class mailController {
@Autowired
private JavaMailSender mailSender;
@RequestMapping(value="/sendEmail")
public void sendMail(){
try{
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setFrom("xxx@qq.com");
message.setTo("xxx@qq.com");
message.setSubject("验证码");
message.setText("您的验证码是:123");
this.mailSender.send(mimeMessage);
System.out.println("邮件发送成功");
}catch(Exception e){
e.printStackTrace();
}
}
}
参考博客:
http://blog.csdn.net/clementad/article/details/51833416