当前位置: 移动技术网 > IT编程>开发语言>Java > 基于SpringBoot实现定时发送邮件过程解析

基于SpringBoot实现定时发送邮件过程解析

2020年06月23日  | 移动技术网IT编程  | 我要评论

前提:

1.springboot项目

2.引入maven 依赖

 <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-mail</artifactid>
    </dependency>

以下代码中涉及到的maven依赖有日志依赖,但是springboot都有集成,不用重新引入依赖

application(程序入口)

package com.springbootemaildemo;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.scheduling.annotation.enablescheduling;
import springfox.documentation.swagger2.annotations.enableswagger2;
/**
 * 引入了一个注解@enableswagger2来启动swagger注解。(启动该注解使得用在controller中的swagger注解生效, 覆盖的范围由@componentscan的配置来指定,
 * 这里默认指定为根路径”com.springboot”下的所有controller)
 * 也可以单独写衣swaggerconfigura
 */
@enablescheduling //启动定时任务
@enableswagger2 //启动swagger注解
@springbootapplication
public class mailapplication {
  public static void main(string[] args) {
    springapplication.run(mailapplication.class, args);
  }
}

mailjob(定时任务类)

package com.springbootemaildemo.job;

import com.springbootemaildemo.send.sendmail;
import com.springbootemaildemo.send.tensenvenmail;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.scheduling.annotation.enablescheduling;
import org.springframework.scheduling.annotation.scheduled;
import org.springframework.stereotype.component;

import javax.annotation.resource;

@component
@enablescheduling
public class mailjob {
  private static final logger logger = loggerfactory.getlogger(mailjob.class);

  @resource
  sendmail sendmail;

  @resource
  tensenvenmail tensenvenmail;

  //@scheduled(cron = "0/5 * * * * ?")
  //或直接指定时间间隔,例如:100秒
  // @scheduled(fixedrate=100000)
  //早晨7点
  @scheduled(cron = "0 0 7 * * ?")
  public void sendjob() {
    string bodyten = "早安哇,太阳出来啦,记得开心哟";
    string bodywen = "记得开心哟";
    logger.info("定时任务开始..........................");
    sendmail.sendwen(bodywen);
    tensenvenmail.sendten(bodyten);
    logger.info("定时任务结束..........................");
  }
}

@enablescheduling 这个注解是 开启定时任务。

发送邮件代码:

发送普通的邮件(发送邮件类):

package com.springbootemaildemo.send;

import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.stereotype.component;

import javax.mail.message;
import javax.mail.messagingexception;
import javax.mail.session;
import javax.mail.transport;
import javax.mail.internet.internetaddress;
import javax.mail.internet.mimemessage;
import java.util.date;
import java.util.properties;


@component
public class sendmail {
  private static final logger logger = loggerfactory.getlogger(sendmail.class);

  public void sendwen(string body) {
    logger.info("开始发送..................");
    string from = "212212@qq.com";
    string to = "5456456@qq.com";
    string subject = "happy";
    string smtphost = "smtp.qq.com";
    properties props = new properties();
    props.setproperty("mail.transport.protocol", "smtp"); // 使用的协议(javamail规范要求)
    props.setproperty("mail.smtp.host", smtphost); // 发件人的邮箱的 smtp服务器地址
    props.setproperty("mail.smtp.auth", "true"); // 请求认证,参数名称与具体实现有关

    // 创建session实例对象
    session session = session.getdefaultinstance(props);
    // 创建mimemessage实例对象
    mimemessage message = new mimemessage(session);
    // 设置发件人
    try {
      message.setfrom(new internetaddress(from));
      // 设置收件人
      message.setrecipients(message.recipienttype.to, internetaddress.parse(to));
      // 设置发送日期
      message.setsentdate(new date());
      // 设置邮件主题
      message.setsubject(subject);
      // 设置纯文本内容的邮件正文
      message.settext(body);
      // 保存并生成最终的邮件内容
      message.savechanges();
      // 设置为debug模式, 可以查看详细的发送 log
      session.setdebug(true);
      // 获取transport对象
      transport transport = session.gettransport("smtp");
      // 第2个参数需要填写的是qq邮箱的smtp的授权码,什么是授权码,它又是如何设置?
      transport.connect(from, "ipeiquufachheefg");
      // 发送,message.getallrecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
      transport.sendmessage(message, message.getallrecipients());
      logger.info("发送完成");
      transport.close();
    } catch (messagingexception e) {
      e.printstacktrace();
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网