SpringBoot.自定义启动器

一:使用发送邮件

1.导入pom依赖

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

2.编写yml文件

spring:
  application:
    name: springboot_06
  mail:
#    指定邮箱
    host: smtp.qq.com
#    使用的邮箱
    username: ******@qq.com
#    邮箱授权码
    password: *******
    properties:
      mail:
        smtp:
          auth: true
          starttls:
          enable: true
          required: true

3.启动测试类

package com.sg.code;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

@SpringBootTest
class SpringBoot06ApplicationTests {

    @Autowired
    JavaMailSender mailSender;

    @Test
    void contextLoads() {
        SimpleMailMessage message = new SimpleMailMessage();
//        发件邮箱
        message.setFrom("*******@qq.com");
//        收件邮箱
        message.setTo("******@foxmail.com");
//        邮件标题
        message.setSubject("主题:简单邮件");
//        邮件内容
        message.setText("测试邮件内容");
        mailSender.send(message);
    }

}

二:自定义一个启动器用来发送邮件

1.新建项目

2.导入pom依赖

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

3.配置yml文件

email:
  #    指定邮箱
  host: smtp.qq.com
  #    使用的邮箱
  username: ******@qq.com
  #    邮箱授权码
  password: ******

4.编写一个java类(相当于yml文件配置的实体类)

package com.yzm.yzm;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@AllArgsConstructor
@NoArgsConstructor
@Data
//@Component
@ConfigurationProperties(prefix = "email")
public class EmailProperties {

    private String host;
    private String username;
    private String password;

}

5.编写接口

package com.yzm.yzm;

import org.springframework.beans.factory.annotation.Autowired;

public interface EmailSender {

    String sendText(String receiver);

}

6.实现类

注:@EnableConfigurationProperties(value = EmailProperties.class)是对第四步那个类进行一个配置

package com.yzm.yzm;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

//把类变成一个组件
@Configuration
@Slf4j
@EnableConfigurationProperties(value = EmailProperties.class)
public class EmailSendImpl implements EmailSender {

    private final EmailProperties emailProperties;
    private final JavaMailSenderImpl mailSender;

    //    1:获得注入的信息
    @Autowired
    public EmailSendImpl(EmailProperties emailProperties) {
//        初始化一个邮件的发送对象
        this.emailProperties = emailProperties;
        mailSender = new JavaMailSenderImpl();
        mailSender.setHost(emailProperties.getHost());
        mailSender.setUsername(emailProperties.getUsername());
        mailSender.setPassword(emailProperties.getPassword());
        mailSender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.starttls.enable", "true");
        p.setProperty("mail.smtp.starttls.required", "true");
        mailSender.setJavaMailProperties(p);
    }

    @Override
    public String sendText(String receiver) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(emailProperties.getUsername());
        message.setTo(receiver);
        message.setSubject("网站验证码");
        message.setText("nanan");
        mailSender.send(message);
        return "nanan";
    }
}

7.测试类

package com.yzm.yzm;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class YzmSpringBootStarterApplicationTests {

    @Autowired
    private EmailSender sender;

    @Test
    void contextLoads() {
        sender.sendText("snanan@foxmail.com");
    }

}

8.添加pom依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>

9.右键该启动器并且install

 这样在别的项目中只需调用就可以使用了

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值