我写了一个代码来解析.eml文件到JSON文件.我使用下面的依赖关系

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>jakarta.mail</artifactId>
    <version>2.0.1</version>
</dependency>

以下是我的代码

/**
     * Convert .eml file to .json file
     *
     * @param inputFile     .eml file
     * @param outputFile    .json file
     * @return  output file
     * @throws Exception Exception
*/
public File executeTransform(File inputFile, File outputFile) throws Exception {
        try {
            Properties properties = new Properties();
            Session session = Session.getDefaultInstance(properties);

            // Read the .eml file
            InputStream emlInputStream = new FileInputStream(inputFile);
            MimeMessage emlMessage = new MimeMessage(session, emlInputStream);

            // Convert the email message to JSON. convertEmlToJson() This function reads eml, extracts required data from it, puts that data in ObjectNode, and returns that ObjectNode.
            ObjectNode jsonNode = convertEmlToJson(emlMessage);

            // Write JSON to a file
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
            objectMapper.writeValue(outputFile, jsonNode);
        } catch (Exception e) {
            System.err.println("Test log : error occurred " + e.getMessage());
            throw e;
        }
        return outputFile;
    }

运行此代码时,出现以下错误

java.lang.reflect.InvocationTargetException and cause java.lang.NoSuchMethodError: 'void com.sun.mail.util.LineInputStream.<init>(java.io.InputStream, boolean)'

并获得此行的上述错误

MimeMessage emlMessage = new MimeMessage(session, emlInputStream);

有人知道我需要做什么来解决这个问题吗?

推荐答案

我通过添加下面的依赖项而不是jakarta.mail解决了这个问题

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

我正在使用JAVA11.而在Java 17之前,jakarta.mailjavax.mail.

如果你想使用jakarta.mail,你必须升级到Java 17,它有jakarta.mail内置的API,或者

javax.mail将与Java11或更低版本一起使用.

Java相关问答推荐

如果给定层次 struct 级别,如何从其预序穿越构造n元树

Spring boot:Bean和动态扩展器

最小拓Flutter 排序的时间复杂度是多少?

Select 按位运算序列

了解Android Studio中的调试器输出

弹簧靴和龙目岛

JVM会优化这个数学运算吗?

在运行MVN测试时,为什么构建失败,并显示了java.lang.ClassNotFoundException:java.net.http.HttpResponse?

自定义批注的外推属性值

类型集合的Jackson JsonNode:类型引用的对象读取器应该是Singleton吗?

Log4j与jdk21兼容吗?

在macOS上读取文件会导致FileNotFound,即使文件存在(并且具有权限)

如何在Spring Security中设置一个任何人都可以打开的主页?

如何使用Hibernate v6.2构建NamingStrategy,以表名作为所有列的前缀?

Java返回生成器的实现

如何通过gradle命令行从build.gradle获得Java targetCompatibility

java21预览未命名的符号用于try-with-resources

ControlsFX RangeSlider在方向垂直时滞后

为什么我得到默认方法的值而不是被覆盖的方法的值?

为什么当我输入变量而不是直接输入字符串时,我的方法不起作用?