java邮件解析工具类

所需依赖 javax.mail-1.6.2.jar 最底部获取

1、实体类

package copsec.safecheckmonitor.bean;

import java.util.List;

/**
 * @Author wangchang
 * @Description 邮件对象
 * @Date 2024/6/21 11:16
 */
public class EmlInfo {
    private String sender; // 发件人
    private String recver; // 收件人
    private List<String> subjectNameList; // 内容文件名
    private String topic; // 主题
    private List<String> attachmentNameList; // 附件名,最多支持100个附件

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getRecver() {
        return recver;
    }

    public void setRecver(String recver) {
        this.recver = recver;
    }

    public List<String> getSubjectNameList() {
        return subjectNameList;
    }

    public void setSubjectNameList(List<String> subjectNameList) {
        this.subjectNameList = subjectNameList;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public List<String> getAttachmentNameList() {
        return attachmentNameList;
    }

    public void setAttachmentNameList(List<String> attachmentNameList) {
        this.attachmentNameList = attachmentNameList;
    }
}


二、工具类

package copsec.safecheckmonitor.util;

import copsec.safecheckmonitor.bean.EmlInfo;
import copsec.safecheckmonitor.conf.Config;

import javax.mail.*;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * @Author wangchang
 * @Description 邮件解析工具类
 * @Date 2024/6/21 15:15
 */
public class ParseEmlFileUtils {

    // 邮件生成的附件地址
    private static final String emlPath = Config.getConfig().getEmlPath();


    public static EmlInfo parseEmlFile(String emlFilePath) throws Exception {
        // 先判断路径存在不,不存在就创建
        Path path = Paths.get(emlPath);
        if (!Files.exists(path)) {
            Files.createDirectories(path);
        }

        // 邮件对象
        EmlInfo emlInfo = new EmlInfo();


        Properties props = new Properties();
        Session mailSession = Session.getDefaultInstance(props, null);
        InputStream source = new FileInputStream(new File(emlFilePath));
        MimeMessage message = new MimeMessage(mailSession, source);

        // 邮件主题
        String subject = message.getSubject();
        String sanitizedSubject = ComUtils.sanitizeFileName(subject);
        emlInfo.setTopic(sanitizedSubject);

        // 发件人
        Address[] froms = message.getFrom();
        String fromEmails = "";
        if (froms != null && froms.length > 0) {
            // 根据正则匹配获取邮箱,多个根据','分割
            fromEmails = Arrays.stream(froms)
                    .map(Address::toString)
                    .map(ComUtils::extractEmail)
                    .collect(Collectors.joining(", "));
        }
        emlInfo.setSender(fromEmails);

        // 收件人
        Address[] recipients = message.getRecipients(Message.RecipientType.TO);
        String toEmails = "";
        if (recipients != null) {
            // 根据正则匹配获取邮箱,多个根据','分割
            toEmails = Arrays.stream(recipients)
                    .map(Address::toString)
                    .map(ComUtils::extractEmail)
                    .collect(Collectors.joining(", "));
        }
        emlInfo.setRecver(toEmails);

        // 邮件内容
        Object content = message.getContent();
        List<String> subjectNameList = new ArrayList<>();
        List<String> attachmentNameList = new ArrayList<>();
        if (content instanceof String) {
            String fileNamePath = emlPath + sanitizedSubject + ".txt";
            ComUtils.writeFile(content.toString(), fileNamePath);
            subjectNameList.add(fileNamePath);
        } else if (content instanceof Multipart) {
            Multipart multipart = (Multipart) content;
            for (int i = 0; i < multipart.getCount(); i++) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                if (bodyPart.isMimeType("text/plain")) {
                    String subjectNamePath = emlPath + sanitizedSubject + ".txt";
                    ComUtils.writeFile(bodyPart.getContent().toString(), subjectNamePath);
                    subjectNameList.add(subjectNamePath);
                } else if (bodyPart.isMimeType("text/html")) {
                    String subjectNamePath = emlPath + sanitizedSubject + ".html";
                    ComUtils.writeFile(bodyPart.getContent().toString(), subjectNamePath);
                    subjectNameList.add(subjectNamePath);
                } else if (bodyPart instanceof MimeBodyPart && bodyPart.getDisposition() != null && bodyPart.getDisposition().equalsIgnoreCase(Part.ATTACHMENT)) {
                    String encodedFileName = bodyPart.getFileName();
                    // 解码Base64的文件名
                    String decodedFileName = MimeUtility.decodeText(encodedFileName);
                    // 保存附件到本地
                    MimeBodyPart mimeBodyPart = (MimeBodyPart) bodyPart;
                    String attchmentNamePath = emlPath + decodedFileName;
                    mimeBodyPart.saveFile(attchmentNamePath);
                    attachmentNameList.add(attchmentNamePath);
                }
            }
        }
        emlInfo.setSubjectNameList(subjectNameList);
        emlInfo.setAttachmentNameList(attachmentNameList);

        return emlInfo;
    }


}


3、所需jar包
链接:https://pan.baidu.com/s/1mZO7aULm5arNiEj5wgA1Pg
提取码:king

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王之蔑视.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值