Spring boot 集成邮件服务

  1. 添加依赖
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>
  1. 定义邮件服务接口
import com.baomidou.mybatisplus.extension.service.IService;

public interface IMailService {

    /**
     * 异步发送纯文本的简单邮件
     * @param subject
     * @param content
     */
    public void sendSimpleMail(String subject, String content);

    /**
     * 异步发送纯文本的简单邮件
     * @param to
     * @param subject
     * @param content
     */
    public void sendSimpleMail(String to, String subject, String content);

}
  1. 定义邮件服务接口实现类

在方法上添加@Async 实现方法的异步处理


import com.goldnurse.commons.mail.IMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class MailServiceImpl implements IMailService {

    @Resource
    private JavaMailSender sender;

    @Value("${spring.mail.username}")
    private String from;

    @Value("${goldnurse.commons.config.mail.enable}")
    private boolean send_mail_enable;

    @Value("${goldnurse.commons.config.mail.address}")
    private String send_mail_address;

    /**
     * 发送纯文本的简单邮件
     * @param subject
     * @param content
     */
    @Async
    @Override
    public void sendSimpleMail(String subject, String content){
        this.sendSimpleMail(send_mail_address,subject,content);
    }

    /**
     * 发送纯文本的简单邮件
     * @param to
     * @param subject
     * @param content
     */
    @Async
    @Override
    public void sendSimpleMail(String to, String subject, String content){
        if (send_mail_enable) {
            SimpleMailMessage message = new SimpleMailMessage();
            String[] toRecive = to.split(",");
            message.setFrom(from);
            message.setTo(toRecive);
            message.setSubject(subject);
            message.setText(content);

            try {
                sender.send(message);
                log.info("简单邮件已经发送。");
            } catch (Exception e) {
                log.error("发送简单邮件时发生异常!", e);
            }
        }
    }

}

  1. application.yml 邮件参数
  • 企业邮件地址如下
  • host、username、password 注意需修改
  • enable true时发送邮件,false 时不发送邮件
  • address 接收邮件的地址,可配置多个,使用逗号隔开
spring:
  mail:
    host: smtp.goldnurse.com
    username: service@goldnurse.com
    password: 99999999
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
goldnurse:
  commons:
    config:
      mail:
        enable: true
        address: fengrz@goldnurse.com,fengrz@goldnurse.com
  1. 开启多线程

在主函数中添加@EnableAsync 开启多线程

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableAsync
public class SurveyWebApplication {

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

}
  1. 调用并测试
  • 注入
	@Autowired
    IMailService iMailService;
  • 发送邮件
	iMailService.sendSimpleMail("访问异常", "消息内容");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值