前言
依赖pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
yml配置application.yml
spring:
# application:
# name: register-center
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xx.xxx.xxx.xx:3306/wx
hikari:
username: root
password: xxxxxxxx
jpa:
hibernate:
ddl-auto: update
show-sql: true
mail:
host: smtp.qq.com
username: 你的邮箱账号
password: 在邮箱中smtp中生成的密码
代码部分
package com.clunt.wechat.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmailController {
@Autowired
private JavaMailSender javaMailSender;
@RequestMapping("sendEmail")
@ResponseBody
public Boolean sendMail(){
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom("你的邮箱");
msg.setBcc();
msg.setTo("对方的邮箱");
msg.setSubject("from java mail tools");//文本主题
msg.setText("java mail");//文本内容
javaMailSender.send(msg);
return true;
}
}