软件编程
位置:首页>> 软件编程>> java编程>> 使用SpringBoot发送邮件的方法详解

使用SpringBoot发送邮件的方法详解

作者:掉头发的王富贵  发布时间:2023-01-30 02:21:37 

标签:SpringBoot,发送邮件

第一步,导jar包

<!--Email-->
       <dependency>
           <groupId>com.sun.mail</groupId>
           <artifactId>javax.mail</artifactId>
           <version>1.6.2</version>
       </dependency>
       <!--Email-->

第二步,申请email授权密码

这里以163邮箱举例:

使用SpringBoot发送邮件的方法详解

使用SpringBoot发送邮件的方法详解

第三步,编写yml文件

spring:
 mail:
   host: smtp.163.com  #固定写死的 163邮箱
   username: masiyi163163@163.com #刚刚生成授权码的邮箱
   password: NOTZSJISFIOOWDLNY #刚刚生成的授权码
   default-encoding: UTF-8

第四步,编写工具类

一个Java邮件工具类,使用Spring Boot和JavaMailSender实现了发送邮件的功能。该类使用了@Component注解,标识为一个Spring Bean,可以被其他类依赖注入使用。通过@Autowired注解注入了JavaMailSender类,以便使用其提供的邮件发送功能。同时,通过@Value注解注入了配置文件中的发件人邮件地址,方便发送邮件时设置发件人。

该工具类提供了两种发送邮件的方法,分别为简单文本邮件和带有附件的邮件。其中,简单文本邮件使用了SimpleMailMessage类实现,而带有附件的邮件使用了MimeMessage和MimeMessageHelper类实现。同时,通过传递不同的参数,实现了发送不同的邮件内容和附件。


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.io.File;

/**
* @author MaSiyi
* @version 1.0.0 2021/11/20
* @since JDK 1.8.0
*/
@Component
public class EmailUtil {

@Autowired
   private JavaMailSender javaMailSender;

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

/**
    * 简单文本邮件
    * @param to 接收者邮件
    * @param subject 邮件主题
    * @param content 邮件内容
    */
   public void sendSimpleMail(String to, String subject, String content){

SimpleMailMessage message = new SimpleMailMessage();
       message.setTo(to);
       message.setSubject(subject);
       message.setText(content);
       message.setFrom(from);

javaMailSender.send(message);
   }

/**
    * 附件邮件
    * @param to 接收者邮件
    * @param subject 邮件主题
    * @param content HTML内容
    * @param filePath 附件路径
    * @throws MessagingException
    */
   public void sendAttachmentsMail(String to, String subject, String content,
                                   String filePath) throws MessagingException {

try {
           MimeMessage mimeMessage = javaMailSender.createMimeMessage();
           MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
           helper.setTo(to);
           helper.setSubject(subject);
           helper.setText(content, true);
           helper.setFrom(from);

FileSystemResource file = new FileSystemResource(new File(filePath));
           String fileName = file.getFilename();
           helper.addAttachment(fileName, file);

javaMailSender.send(mimeMessage);
       } catch (javax.mail.MessagingException e) {
           e.printStackTrace();
       }
   }

}

第五步,编写controller类

一个邮件服务相关的控制器类EmailController,使用了SpringBoot框架中的@RestController注解来标识它是一个Web API控制器类。同时,它还使用了@Api注解来标识这是一个邮件服务的API,方便其他开发者对这个API进行理解和使用。

这个控制器类中有两个API,分别是sendSimpleMail和sendAttachmentsMail。这两个API都是GET请求,并使用了@ApiOperation注解来描述它们的功能和参数。其中,sendSimpleMail方法调用了EmailUtil类中的sendSimpleMail方法,用于发送简单邮件;sendAttachmentsMail方法调用了EmailUtil类中的sendAttachmentsMail方法,用于发送带附件的邮件。


import com.wangfugui.apprentice.common.util.EmailUtil;
import com.wangfugui.apprentice.common.util.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author MaSiyi
* @version 1.0.0 2021/11/20
* @since JDK 1.8.0
*/
@Api(tags = "邮件服务")
@RestController
@RequestMapping("/email")
public class EmailController {
   @Autowired
   private EmailUtil emailUtil;

@GetMapping("/sendSimpleMail")
   @ApiOperation("发送简单邮件")
   public ResponseUtils sendSimpleMail(String email,String subject,String content) {
       emailUtil.sendSimpleMail(email,subject,content);
       return ResponseUtils.success();
   }
   @GetMapping("/sendAttachmentsMail")
   @ApiOperation("发送附件邮件")
   public ResponseUtils sendAttachmentsMail(String email,String subject,String content,String filePath) {
       emailUtil.sendAttachmentsMail(email, subject, content, filePath);
       return ResponseUtils.success();
   }
}

第六步,测试

使用SpringBoot发送邮件的方法详解

查看邮箱

使用SpringBoot发送邮件的方法详解

成功!!

这篇文章主要介绍了如何在Spring Boot项目中使用JavaMailSender发送邮件,包括导入所需的jar包、申请邮箱授权密码、编写配置文件、编写JavaMailSender工具类和Controller类,最后演示了如何调用工具类发送简单文本邮件和带有附件的邮件。

好了,就是这么的简单,完整代码请移至SpringBoot+Email查看

来源:https://juejin.cn/post/7229858467804823589

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com