Search code examples
javaspring-bootmime

Spring Boot 3.1: Problem with MIME-generation - works at Spring Boot 2.7


I have a simple Spring-Boot app that generates a MIME-structure. I do it for tests in a simple component MyComp.

package com.example.demo;

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import org.slf4j.*;
import org.springframework.stereotype.*;
import javax.annotation.*;    // change to jakarta.annnotation.*  if Spring Boot 3

@Component
public class MyComp
{
    @PostConstruct
    private void init () 
    {   
        try
        {
            Properties ps = new Properties ();
            Session ses = Session.getInstance (ps);
    
            MimeMessage mm = new MimeMessage (ses);
            Multipart mp = new MimeMultipart ("related");
            
            MimeBodyPart mbp = new MimeBodyPart ();
        
            mbp.setContent ("abc", "application/soap+xml");
            mbp.setHeader ("Content-Transfer-Encoding", "binary");
            mbp.setHeader ("Content-Disposition", "inline");
            mbp.setContentID ("<mimepart_head>");
            mp.addBodyPart (mbp);
                
            mm.setContent (mp);
            mm.saveChanges ();
    
            mm.setHeader ("Content-Type", mm.getContentType () + "; start=\"mimepart_head\"");
     
            ByteArrayOutputStream baos = new ByteArrayOutputStream ();
            mm.writeTo (baos);    //!!!!!!!!!!
            System.out.println(baos.toString());
        }
        catch (Exception e)
        {
            System.out.println (e.getMessage());
        }
    }
}

It works fine when I use Spring-Boot 2 (highest Spring Boot 2 number 2.7.18).

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.18</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

If I change to Spring-Boot 3.1.11 it fails with an exception at line

mm.writeTo (baos);

The exceptionis about: no object DCH for MIME type application/soap+xml

Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type application/soap+xml
    at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:885)
    at javax.activation.DataHandler.writeTo(DataHandler.java:316)
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1694)
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:996)
    at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:561)
    at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:84)
    at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:883)
    at javax.activation.DataHandler.writeTo(DataHandler.java:316)
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1694)
    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1913)
    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1887)
    at com.example.demo.MyComp.init(MyComp.java:70)

Any idea about the reason? How can I get it run at Spring 3?


Solution

  • The problem is you are using incompatible libraries.

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

    This is a JavaEE dependency and for Spring Boot it would be better to use the spring-boot-starter-mail instead. That will pull in all necessary and compatible dependencies.

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

    If you move to that dependency first, your 2.7 version will still work. Now when upgrading to 3.1 (I would suggest 3.2) your code will stop compiling. Spring Boot 3 requires a move to JakartaEE from JavaEE. Meaning you need to change the javax.mail imports to jakarta.mail in your code.