SpringBoot发送邮箱

创建一个邮箱

在设置里开启IMAP/SMTP服务(这里是126网易邮箱)



创建一个application.properties用来做配置文件

#编码
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.126.com
#密码
spring.mail.password=123456
#端口
spring.mail.port=25
#协议
spring.mail.protocol=smtp
#邮箱号
spring.mail.username=tao_hg@126.com
创建一个java类用来向邮箱发送邮件

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MailController {
	@Autowired
	private JavaMailSender jms;//借助这个类,我们便可以发邮件了
	@GetMapping("/send")
	public String send(String message){
		SimpleMailMessage ss=new SimpleMailMessage();
		//发送邮箱
		ss.setFrom("tao_hg@126.com");
		//接收邮箱
		ss.setTo("201883607@qq.com");
		//标题
		ss.setSubject("测试");
		//内容
		ss.setText("测试一下耍耍");
		jms.send(ss);
		return "1";
	}
}
启动

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Test {
	public static void main(String[] args) {
		SpringApplication.run(Test.class, args);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值