SpringBoot实现邮件发送

首先,我只是一个刚进入社会的编码小白,文章中有不足之处还望大家海涵,并请多多指教。写博客只是整理自己的学习笔记,并且一个小白走过的弯路,可能会给大家更好的解答效果,不排除有些罗嗦。哈哈!

一、导入配置模块

<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    
<!-- 邮箱发送依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

二、配置文件

我的配置文件,包含了使用公司邮箱 和 QQ邮箱两种方式实现。你可以选择注解其中一种(具体配置要换成自己的信息):

#application.properties
server.port=8091

#spring.mail.host=smtp.qq.com
#spring.mail.username=1888888@qq.com
#spring.mail.password=oswdgwqirdxufihj
#spring.mail.default-encoding=UTF-8

spring.mail.host=mail.myCompany.com
spring.mail.username=myMailName@myCompany.com
spring.mail.password=myCompanyMailPassword
spring.mail.default-encoding=UTF-8

spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=true

1. 使用 QQ 邮箱(要把userName,password 改成自己的)

#spring.mail.password=oswdgwqirdxufihj

(这个不是邮箱帐号的密码,是邮箱授权码,这一点很重要)
授权码的获取方式如下 :
登录QQ 邮箱网页版,按图片中标记操作
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
不要忽视配置文件中,最后一部分的四项配置内容,它能帮你减少一些不必要的报错

2 使用公司邮箱实现

我们公司使用的是foxmail,没有找到授权码信息,所以password 直接添的就是自己公司邮箱帐号的密码
在这里插入图片描述
刚才只是配置了邮件系统,接下来设置邮件发送方和接收方信息。当然,如果不需要,也可以后面直接在项目中写死

#application.yaml
mailEntity:
  from: mail.myCompany.com
  to: [mail.myCompany.com, 18888@qq.com]
  subject: New Project Update
  text_01: New Project Web
  text_02: New Project UpdateTime

如果你最后整合项目,发现有这样的报错:(这是yaml等配置文件编码集的原因 )
Java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application-dev.yml’
可以参考这篇文章:https://blog.csdn.net/weixin_44701264/article/details/105860707?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162649212916780262519595%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162649212916780262519595&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-1-105860707.first_rank_v2_pc_rank_v29&utm_term=idea%E6%9B%B4%E6%94%B9%E9%A1%B9%E7%9B%AE%E7%9A%84%E7%BC%96%E7%A0%81%E9%9B%86&spm=1018.2226.3001.4187

如果不能解决: 删除你的配置文件,重新写一遍,可能是因为你格式不正确,引入了不必要的多余 空格 (这个很难检查出来,我当初可能就是因为这个才编译不通过)

三、功能实现

1 文件结构
在这里插入图片描述
2 UrlBean

package dateChangeReporter.beans;

package dateChangeReporter.beans;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "urlBean")
public class UrlBean {
    // 目标网页的URL地址
    private String URL;

    //  要匹配的子串内容
    private String[] matchSubStr;

    //  网页内容的最近更新时间
    private String updateTime;

    public String getURL() {
        return URL;
    }

    public void setURL(String URL) {
        this.URL = URL;
    }

    public String[] getMatchSubStr() {
        return matchSubStr;
    }

    public void setMatchSubStr(String[] matchSubStr) {
        this.matchSubStr = matchSubStr;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }
}

3 MailEntity

package dateChangeReporter.mail;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "mailEntity")
public class MailEntity {
    private String[] to;        //  收件人
    private String from;      //  发件人
    private String subject;   //  标题
    private String text_01;   //  正文 (第一段)
    private String text_02;   //  正文 (第二段)

    public String[] getTo() {
        return to;
    }

    public void setTo(String[] to) {
        this.to = to;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getText_01() {
        return text_01;
    }

    public void setText_01(String text_01) {
        this.text_01 = text_01;
    }

    public String getText_02() {
        return text_02;
    }

    public void setText_02(String text_02) {
        this.text_02 = text_02;
    }
}

4 MailSend

package dateChangeReporter.mail;

import dateChangeReporter.beans.UrlBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class MailSend {
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private UrlBean urlBean;

    @Autowired
    private MailEntity mailEntity;

    public void sendMail() {
        //  简单邮件模板
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom(mailEntity.getFrom());
        msg.setTo(mailEntity.getTo());
        msg.setSubject(mailEntity.getSubject());
        msg.setText(mailEntity.getText_01() + " :  " + urlBean.getURL() + "\n "  + mailEntity.getText_02() + " :  " + urlBean.getUpdateTime());
        try{
            javaMailSender.send(msg);
            System.out.println("send mail successfully !");
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("error in mail sendding !");
        }
    }

}

5 ApplicationRunnerImpl

package dateChangeReporter;

import dateChangeReporter.beans.UrlBean;
import dateChangeReporter.mail.MailSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

@Service
public class ApplicationRunnerImpl implements ApplicationRunner {

    
    @Autowired
    private MailSend mailSend;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        new LocalThread().start();
    }

    private class LocalThread extends Thread {

        @Override
        public void run() {

            while (true) {
                try {
                    mailSend.sendMail();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


}

6 ApplicationEntrance

package dateChangeReporter;

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

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

要干饭了,很多地方就不细讲了。虽然我把很多功能去掉了,应该也是可以运行的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值