一:使用发送邮件
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
这样在别的项目中只需调用就可以使用了