做点基于springboot发送邮件

最近自己在弄个网站,正在研究密码找回该怎么做,短信emmmm,做着玩玩不想花钱,那就用邮箱吧免费的(没错就是这么抠哈哈哈哈)

不废话直接上代码

首先,搭建一个sprintboot的项目这个就不多说了

需要在pom文件中依赖spring-mail

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

springboot启动类没啥不一样的

@SpringBootApplication
public class SpringMailApplication {

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

}

在application.properties文件中配置好

server.port=8080
#根据自己邮箱填写
spring.mail.host=smtp.163.com
#发件人的邮箱
spring.mail.username=czj_folson@163.com
#密码
spring.mail.password=

#照着填写
spring.mail.default-encoding=UTF-8
spring.mail.protocol=smtps
spring.mail.port=465
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

注入JavaMailSender用来发送邮件

 @Autowired
 private JavaMailSender mailSender;

简单写一个测试(注意必须抄收一份否则无法发送,抄收邮箱可以不写)

@Controller
public class SendMailController {

    @Autowired
    private JavaMailSender mailSender;

    @RequestMapping("/sendEmail")
    @ResponseBody
    public boolean sendEmail() {
        // new 一个简单邮件消息对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 和配置文件中的的username相同,相当于发送方
        message.setFrom("czj_folson@163.com");
        // 收件人邮箱
        message.setTo("757892057@qq.com");
        // 抄收一份
        message.setCc();
        // 标题
        message.setSubject("测试");
        for(int i =0;i<=20;i++){
            System.out.println("第"+i+"条;");
            // 正文
//            String text = checkNum(i);
            message.setText("ssss");
            // 发送
            try {
                mailSender.send(message);
            } catch (MailException ex) {
                System.err.println(ex.getMessage());
                return false;
            }
        }
        return true;
    }

    
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值