spring boot JavaMailSender发送邮件 实现多附件 多人发送

1.引入相关pom

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

2.配置文件

spring.mail.host=smtp.qq.com
spring.mail.username=邮箱地址
spring.mail.password=授权码
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

3.发送邮件类

@Component
public class SendMailUtils {
    @Value("${spring.mail.username}")
    private String  fromEmail;
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 发送简单纯文字邮件
     * @param to
     * @param subject
     * @param content
     * @return
     */
    public  boolean sendNomalMail(String to, String subject, String content) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setTo(to);
        simpleMailMessage.setFrom(fromEmail);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        try {
            javaMailSender.send(simpleMailMessage);//执行发送
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * 发送纯单个或多个带附件邮件
     * @param to 
     * @param subject
     * @param content
     * @param filepath
     * @return
     */
    public  boolean sendAttachmentMail(List<CityEmail> to, String subject, String content, List filepath) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            /*helper.setTo(to);*/
            helper.setSubject(subject);
            helper.setText(content);
            helper.setFrom(fromEmail);
            //发送给多人 这里是我的实体类 可以改为对应的邮箱集合
            for (CityEmail cityEmail : to){
                mimeMessage.addRecipients(Message.RecipientType.TO, InternetAddress.parse(cityEmail.getEmail(), false));
            }
            //读取附件文件(传入文件路径)遍历文件数组,实现多个附件的添加
            if(filepath.size()>0){
                for (Object string : filepath) {
                    FileSystemResource file = new FileSystemResource(string.toString());
                    String fileName = file.getFilename();
                    helper.addAttachment(fileName, file);
                }
                try {
                    javaMailSender.send(mimeMessage);
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }else {
                return false;
            }
        } catch (MessagingException e) {
            e.printStackTrace();
            //捕获到创建MimeMessageHelper的异常
        }
        return true;
    }
}

具体的功能 直接调用邮件类的方法传入参数即可

你可以使用 Spring BootJavaMailSender发送附件邮件。下面是一个简单的示例代码: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootApplication public class EmailApplication { @Autowired private JavaMailSender javaMailSender; public static void main(String[] args) { SpringApplication.run(EmailApplication.class, args); } public void sendEmailWithAttachment() throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo("recipient@example.com"); helper.setSubject("邮件主题"); helper.setText("邮件内容"); FileSystemResource file = new FileSystemResource(new File("/path/to/attachment.txt")); helper.addAttachment("附件名称.txt", file); javaMailSender.send(message); } } ``` 请注意替换示例代码中的收件人地址和附件路径。这里使用的是 `FileSystemResource` 类来加载文件作为附件,并通过 `addAttachment` 方法将附件添加到邮件中。 你可以将上述代码放在 Spring Boot 项目中的任何地方,并通过调用 `sendEmailWithAttachment` 方法来发送附件邮件
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值