springboot 发送(支持多人接收邮件、带附件)

本文介绍了如何使用SpringBoot发送电子邮件,包括添加必要的依赖、配置yml文件,以及在Java代码中实现发送支持多个收件人且带有附件的邮件功能。
摘要由CSDN通过智能技术生成

1、pom 加入依赖

<!--定时发送邮件-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>RELEASE</version>
</dependency>

2、yml

spring:
  mail:
      host: smtp.qq.com #如果是客户项目 客户会提供一个邮箱ip 
      username: *******@qq.com #如果是客户项目 客户会提供一个邮箱账号
      password: ***** #密码 如果是qq邮箱在qq邮箱中开通授权码就可以了,如果是客户项目 客户会提供一个邮箱密码
      default-encoding: UTF-8
      protocol: smtp
      port: 25
      properties:
        mail:
          smtp:
            auth: true
          debug: true
      test-connection: false
#邮件发送人、接收人(用;分割)
mail:
  fromMail:  *******@qq.com #和上面邮箱是一个,这里我自己这样抽出来。 没有实际意义, 纯属自己愿意这样玩,也可直接用上面的
  toMail: *******@qq.com #收件人

3、接下来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Spring Boot 中实现发送附件邮件,可以按照以下步骤进行设计: 1. 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 在 application.properties 文件中配置邮件服务器的信息: ```properties # 邮件服务器地址 spring.mail.host=smtp.qq.com # 邮件服务器端口 spring.mail.port=465 # 邮箱账号 spring.mail.username=your_email@qq.com # 邮箱密码 spring.mail.password=your_password # SSL协议 spring.mail.properties.mail.smtp.ssl.enable=true ``` 3. 创建一个邮件服务类 MailService,实现发送邮件的功能: ```java @Service public class MailService { @Autowired private JavaMailSender mailSender; public void sendMailWithAttachment(String to, String subject, String text, File file) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setSubject(subject); helper.setText(text); // 添加附件 FileSystemResource resource = new FileSystemResource(file); helper.addAttachment(file.getName(), resource); mailSender.send(message); } } ``` 4. 在需要发送邮件的地方调用 MailService 的 sendMailWithAttachment 方法,传入收件人地址、邮件主题、邮件正文和附件文件即可: ```java // 发送附件邮件 File file = new File("attachment.txt"); mailService.sendMailWithAttachment("recipient@example.com", "邮件主题", "邮件正文", file); ``` 以上就是在 Spring Boot 中实现发送附件邮件的设计过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值