Springboot + Jodd + Smtp方式 发送带附件邮件

一、引入依赖:

<dependency>
   <groupId>org.jodd</groupId>
    <artifactId>jodd-mail</artifactId>
    <version>5.0.13</version>
</dependency>

二、发送邮件:

方案一: 直接将MultipartFile文件以byte数组传入。

  • 优点:不会生成临时文件,造成额外磁盘开销。
  • 缺点:文件直接放在内存中,如果文件多或者请求量大的话,会导致内存占用过高。因此仅推荐请求并发较小的场景。
public String sendEmail(EmailRequest request, MultipartFile file) {
        SendMailSession session = null;
        try {
            Email email = Email.create()
                    .from(request.getFrom())
                    .to(request.getTo())
                    .cc(request.getCc())
                    .bcc(request.getBcc())
                    .subject(request.getSubject())
                    .htmlMessage(request.getContent())
                    // 内嵌资源,name对应内嵌资源名
                    .embeddedAttachment(EmailAttachment.with()
                            .content(file.getBytes(), file.getContentType())
                            .name(file.getOriginalFilename()))
                    // 附件
                    .attachment(EmailAttachment.with()
                            .content(file.getBytes(), file.getContentType())
                            .name(file.getOriginalFilename()))
                    .priority(PRIORITY_HIGHEST);

            SmtpServer smtpServer = MailServer.create()
                    .ssl(true)  // 这里可以按需选择true或false
                    .host(request.getHost())
                    .auth(request.getAccessKey(), request.getAccessSecret())
                    .buildSmtpMailServer();

            session = smtpServer.createSession();
            session.open();
            session.sendMail(email);
        }catch (Exception e){
            e.printStackTrace();
            return "failed";
        } finally {
            if(null != session){
                try{
                    session.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return "success";
    }

方案二: 将传输的文件生成本地临时文件,发送完邮件后,删除临时文件。

1、springboot 获取上传文件的类型是MultipartFile。而邮件附件添加的类型为file。因此首先需要将multipartFile 转换成 file。
2、发送完邮件后,将生成的本地file文件删除。

  • 优点:不会造成内存占用过高的情况。
  • 缺点:生成的临时文件可能会出现无法立刻删除的情况。可以通过定时任务清理解决。
public String sendEmail(EmailRequest request, MultipartFile multipartFile) {
        SendMailSession session = null;
        Fie file = null;
        try {
        	file = FileUtil.multipartFileToFile(multipartFile);
            Email email = Email.create()
                    .from(request.getFrom())
                    .to(request.getTo())
                    .cc(request.getCc())
                    .bcc(request.getBcc())
                    .subject(request.getSubject())
                    .htmlMessage(request.getContent())
                    // 内嵌资源,file的name对应内嵌资源名
                    .embeddedAttachment(EmailAttachment.with()
                            .content(file)
                    // 附件
                    .attachment(EmailAttachment.with()
                            .content(file)
                    .priority(PRIORITY_HIGHEST);

            SmtpServer smtpServer = MailServer.create()
                    .ssl(true)  // 这里可以按需选择true或false
                    .host(request.getHost())
                    .auth(request.getAccessKey(), request.getAccessSecret())
                    .buildSmtpMailServer();

            session = smtpServer.createSession();
            session.open();
            session.sendMail(email);
        }catch (Exception e){
            e.printStackTrace();
            return "failed";
        } finally {
            if(null != session){
                try{
                    session.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            // 删除临时文件
			FileUtil.deleteTempFile(file);
        }
        return "success";
    }

这里遇到过一个问题:执行FileUtil.deleteTempFile(file);后一直无法成功删除文件,说是文件被占用,即使是手动删除也是不行,后来在网上查找了许多资料,发现主要是下面2种情况。

  • 1、有输入输出流没有关闭,即没有调用close()方法。解决方法就是调用close()方法。
  • 2、所删除文件被其他对象引用。这就是我遇到的情况。从上面代码可以看出,file对象实际上被email对象引用,因此无法成功删除。只是即使没有删除成功,控制台也不会提示错误。。。
    后来经过测试,重启系统后,第一次调用接口时,临时文件可以正常被删除,第二次再调用时,生成的文件就不会被删除。这就是最困扰我的问题。于是我在删除文件前加一句System.gc();,这样就能成功删除,但是也不能保证每次都能成功删除,究其原因就是email对象没有被回收释放,使得它所引用的file对象文件也无法被删除。具体有效的解决方案有待研究。
    在这里插入图片描述

解决方案:

创建定时任务,扫描清理临时文件。

三、 注意事项:

  • 1、邮件内容包含html内容时,注意规范写法,比如包含头文件<!DOCTYPE html>等,否则很容易被识别成垃圾邮件,导致无法发送和接收。
  • 2、发送人地址和账号需要一致。
  • 3、textMessage和htmlMessage同时存在时,前者不生效,信息会丢失。
  • 4、使用方案一传入附件时,记得设置name值,否则收到的附件会变成后缀为.bin的文件。

四、部分代码:

EmialRequest类:

public class EmailRequest {
    private String accessKey;
    private String accessSecret;
    private String host;
    private String from;
    private String to;
    private String cc;
    private String bcc;
    private String subject;
    private String content;

    // 省略getter、setter
}

FileUtil工具类:

public class FileUtil {
    /**
     * MultipartFile 转 File
     * @param file
     * @throws Exception
     */
    public static File multipartFileToFile(MultipartFile file) {
        if (null == file || file.getSize() <= 0) {
            return null;
        }
        
        File toFile = new File(file.getOriginalFilename());
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            inputStreamToFile(inputStream, toFile);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return toFile;
    }

    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        OutputStream os = null;
        try {
             os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ins != null){
                try {
                    ins.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除本地临时文件
     * @param file
     */
    public static void deleteTempFile(File file) {
        if (file != null) {
            File del = new File(file.toURI());
            del.delete();
        }
    }
}

五、参考地址:

https://jodd.org/email/#e-mail-definition

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值