直接上代码了,简单粗暴
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
#邮箱服务器地址
spring.mail.host=smtp.163.com
#用户名
spring.mail.username=XXXXX@163.com
#授权密码
spring.mail.password=XXXX
spring.mail.default-encoding=UTF-8
用户名:填写你自己的邮箱
授权密码:打开你的邮箱

点击进入,授权

@Service("serdbyemail")
public class SendByEmailTools {
@Autowired
JavaMailSender jms;
public String send(String sender,String receiver,String title,String text){
//建立邮件消息
SimpleMailMessage mainMessage = new SimpleMailMessage();
//发送者
System.out.println("发送者 ------------------");
mainMessage.setFrom(sender);
System.out.println("接收者 ------------------");
//接收者
mainMessage.setTo(receiver);
//发送的标题
mainMessage.setSubject(title);
//发送的内容
mainMessage.setText(text);
jms.send(mainMessage);
return "1";
}
}
/**
*
*/
package com.yuyi.mcb.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yuyi.full.handler.exception.ResultBO;
import org.yuyi.full.handler.exception.ResultTool;
import com.yuyi.mcb.tool.SendByEmailTools;
/**
*
*/
@RestController
public class SendByEmailController {
@Autowired
@Qualifier("serdbyemail")
private SendByEmailTools service;
@GetMapping("/send")
public String send(){
String sender=""; //这个是发送人的邮箱
String receiver=""; //这个是接受人的邮箱
String title="约翰福音"; //标题
String text="【约3:16】“ 神爱世人,甚至将他的独生子赐给他们,叫一切信他的,不至灭亡,反得永生。";
String result=service.send(sender, receiver, title, text);
return result;
}
}
