码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot配置发送邮件

时间:2019-01-09 15:04:04      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:lin   dep   from   cti   username   auto   邮箱   frame   bean   

一、导入jar包

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

二、在zpplication.properties种添加邮箱配置

spring.mail.host=smtp.qq.com
spring.mail.username=54281****@qq.com
spring.mail.password=tneawbzduhvf****
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

三、发送邮件类

package com.example.demo.timedtask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class Scheduler2Task {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    private JavaMailSender mailSender;

    /*
     * @Scheduled(fixedRate = 6000) 上一次开始执行时间点之后6秒再执行
     * @Scheduled(fixedDelay = 6000) 上一次执行完毕时间之后6秒再执行
     * @Scheduled(initialDelay=1000, fixedRate=6000) 第一次延迟1秒后执行,之后按fixedRate的规则执行
     * */
    @Scheduled(fixedRate = 6000)/*每隔六秒钟执行一次*/
    public void reportCurrentTime() {
        System.out.println("现在时间:" + dateFormat.format(new Date()));
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("542813934@qq.com");
        message.setTo("542813934@qq.com");
        message.setSubject("主题:简单邮件1");
        message.setText("测试邮件内容1");
        mailSender.send(message);
    }
}

 

SpringBoot配置发送邮件

标签:lin   dep   from   cti   username   auto   邮箱   frame   bean   

原文地址:https://www.cnblogs.com/zhanzhuang/p/10244203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!