SpringBoot带附件邮件发送

接着http://www.zeromemos.com/index/article/read.html?id=321

创建带附件邮件服务接口方法sendAttachFileMail并实现该方法

package com.zeromemos.service;

import java.io.File;

public interface MailService {
    void sendSimpleMail(String to, String subject, String content);
    void sendAttachFileMail(String to, String subject, String content, File file);
}


package com.zeromemos.service.impl;

import com.zeromemos.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

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

@Service
public class MailServiceImpl implements MailService {

    @Autowired
    JavaMailSender javaMailSender;

    @Value("${spring.mail.username}") //直接从配置文件获取username的值
    private String from;

    @Override
    public void sendSimpleMail(String to, String subject, String content) {

        // 简单邮件可以直接构建一个SimpleMailMessage对象进行配置,配置完成后,通过JavaMailSender将邮件发送出去。
        SimpleMailMessage simpMsg = new SimpleMailMessage();
        simpMsg.setFrom(from);//发送者
        simpMsg.setTo(to);//收件者
        //simpMsg.setCc(cc);//抄送人
        simpMsg.setSubject(subject);//主题
        simpMsg.setText(content);//内容
        javaMailSender.send(simpMsg);
    }

    @Override
    public void sendAttachFileMail(String to, String subject, String content, File file) {
        MimeMessage message = javaMailSender.createMimeMessage();

        try {
            /*
             * 这里使用MimeMessageHelper简化了邮件配置,
             * 它的构造方法的第二个参数true表示构造一个multipart message类型的邮件,
             * multipart message类型的邮件包含多个正文、附件以及内嵌资源,邮件的表现形式更加丰富
             */
            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            helper.setFrom(from);//发送者
            helper.setTo(to);//收件者
            helper.setSubject(subject);//主题
            helper.setText(content);//内容
            helper.addAttachment(file.getName(), file);//设置附件
            javaMailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

到控制器里加入测试代码,这边只用了sendAttachFileMail测试附件发送

    @Autowired
    private MailService mailService;

    @PostMapping("/sendFile/{to}")
    public R sendFile(@PathVariable("to") String to,
                      MultipartFile file){
        String subject = "邮件主题:XXX";
        String content = "邮件内容:XXX";
        if (!file.isEmpty()){
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            try {
                //保存到C:\Users\admin\Desktop\文件名
                File file1 = new File("C:\\Users\\admin\\Desktop\\" + originalFilename);
                file.transferTo(file1);
                mailService.sendAttachFileMail(to,subject,content,file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.ok();
    }

用swagger测试成功,上传的文件也会被保存到指定目录



如果本地的文件不需要也可以进行删除

    @PostMapping("/sendFile/{to}")
    public R sendFile(@PathVariable("to") String to,
                      MultipartFile file){
        String subject = "邮件主题:XXX";
        String content = "邮件内容:XXX";
        if (!file.isEmpty()){
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            try {
                //保存到C:\Users\admin\Desktop\文件名
                File file1 = new File("C:\\Users\\admin\\Desktop\\" + originalFilename);
                file.transferTo(file1);
                mailService.sendAttachFileMail(to,subject,content,file1);
                if (file1.delete()){
                    System.out.println("文件已删除");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.ok();
    }