spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.56k stars 40.55k forks source link

an error occurred by springboot 3 mail fails to start #33452

Closed realboutouil closed 1 year ago

realboutouil commented 1 year ago

After migration to Spring Boot 3.0 mail we did get an issue with [MailSenderPropertiesConfiguration] as the application fails to start getting this Caused by: java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, jakarta.activation.MimeTypeRegistry

In Spring Boot 2.7.6 works with configuration.

Stack trace:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emailHandlerService' defined in file [EmailHandlerService.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'defaultEmailSenderSmtp' defined in file [DefaultEmailSenderSmtp.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'mailSender' defined in class path resource [org/springframework/boot/autoconfigure/mail/MailSenderPropertiesConfiguration.class]: Failed to instantiate [org.springframework.mail.javamail.JavaMailSenderImpl]: Factory method 'mailSender' threw exception with message: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, jakarta.activation.MimeTypeRegistry
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:793)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:242)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1344)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1188)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:561)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1405)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1325)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:880)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:784)
    ... 47 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultEmailSenderSmtp' defined in file [DefaultEmailSenderSmtp.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'mailSender' defined in class path resource [org/springframework/boot/autoconfigure/mail/MailSenderPropertiesConfiguration.class]: Failed to instantiate [org.springframework.mail.javamail.JavaMailSenderImpl]: Factory method 'mailSender' threw exception with message: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, jakarta.activation.MimeTypeRegistry
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:793)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:242)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1344)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1188)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:561)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1405)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1325)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:880)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:784)
    ... 61 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailSender' defined in class path resource [org/springframework/boot/autoconfigure/mail/MailSenderPropertiesConfiguration.class]: Failed to instantiate [org.springframework.mail.javamail.JavaMailSenderImpl]: Factory method 'mailSender' threw exception with message: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, jakarta.activation.MimeTypeRegistry
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:652)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:640)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1324)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1161)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:561)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1405)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1325)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:880)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:784)
    ... 75 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.mail.javamail.JavaMailSenderImpl]: Factory method 'mailSender' threw exception with message: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, jakarta.activation.MimeTypeRegistry
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:171)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:648)
    ... 89 common frames omitted
Caused by: java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, jakarta.activation.MimeTypeRegistry
    at java.base/java.lang.System.arraycopy(Native Method)
    at java.base/java.util.Vector.copyInto(Vector.java:205)
    at jakarta.activation.MimetypesFileTypeMap.<init>(MimetypesFileTypeMap.java:141)
    at jakarta.activation.MimetypesFileTypeMap.<init>(MimetypesFileTypeMap.java:313)
    at org.springframework.mail.javamail.ConfigurableMimeFileTypeMap.createFileTypeMap(ConfigurableMimeFileTypeMap.java:150)
    at org.springframework.mail.javamail.ConfigurableMimeFileTypeMap.getFileTypeMap(ConfigurableMimeFileTypeMap.java:123)
    at org.springframework.mail.javamail.ConfigurableMimeFileTypeMap.afterPropertiesSet(ConfigurableMimeFileTypeMap.java:110)
    at org.springframework.mail.javamail.JavaMailSenderImpl.<init>(JavaMailSenderImpl.java:115)
    at org.springframework.boot.autoconfigure.mail.MailSenderPropertiesConfiguration.mailSender(MailSenderPropertiesConfiguration.java:44)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139)
    ... 90 common frames omitted
mhalbritter commented 1 year ago

If you'd like us to spend some time investigating, please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.

realboutouil commented 1 year ago

After going back to the project i found dependencies that still uses javax librs made conflict with spring boot mail because of that i will close this issue sorry

theigl commented 1 year ago

@mohammedamineboutouil: I just ran into the same issue. Which dependency ended up causing your conflict?

realboutouil commented 1 year ago

@theigl this one

runtimeOnly "com.sun.activation:jakarta.activation:$jakartaActivationVersion"
theigl commented 1 year ago

Thanks @mohammedamineboutouil. Did you remove the dependency or upgrade it?

theigl commented 1 year ago

Ah I got it. The key part is to set the dependency to scope runtime.

MartinHaeusler commented 1 year ago

I'm facing the same issue, but I don't have any direct dependency on jakarta activation. What's the actual cause of the issue?

theigl commented 1 year ago

I didn't have a direct dependency either. Use maven dependency:tree to see if any of your libraries pulls in the package. If so, declare the dependency yourself as a runtime dependency.

korymcandrew commented 1 year ago

I didn't have a direct dependency either. Use maven dependency:tree to see if any of your libraries pulls in the package. If so, declare the dependency yourself as a runtime dependency.

This is what we used to resolve this issue. We were not using Spring Boot but were using Spring MVC, Security, Integration, etc. and were hitting this. The issue (for us) eventually turned out to be Hibernate 6.X pulling in org.glassfish.jaxb:jaxb-runtime which was pulling in a version of activation which was slightly older (2.0.1 possibly) than what was being used elsewhere (2.1.0 I believe).

Alex-Cosma commented 1 year ago

Yeah no this isn't working for me still, I tried the solutions proposed above but nothing seems to work

grozandrei commented 1 year ago

The same, not working for me neither, this issue should not be closed.

mhalbritter commented 1 year ago

If you'd like us to spend some time investigating, please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.

Alex-Cosma commented 1 year ago

There you go @mhalbritter JakartaIssueTest.zip

Thanks!

scottfrederick commented 1 year ago

@Alex-Cosma Thanks for the sample. In your case, it's the org.keycloak dependencies that transitively depend on com.sun.activation:jakarta.activation:1.2.2. Changing those lines in your build file from this:

    implementation "org.keycloak:keycloak-spring-boot-starter:20.0.1"
    implementation "org.keycloak:keycloak-admin-client:20.0.1"

to this:

    implementation("org.keycloak:keycloak-spring-boot-starter:20.0.1") {
        exclude group: "com.sun.activation", module: "jakarta.activation"
    }
    implementation("org.keycloak:keycloak-admin-client:20.0.1") {
        exclude group: "com.sun.activation", module: "jakarta.activation"
    }

fixes the problem in your sample.

With Gradle, you can also solve this problem with configuration like this:

configurations.all {
    resolutionStrategy {
        exclude group: "com.sun.activation", module: "jakarta.activation"
    }
}

For anyone else who is having this problem, please analyze the full dependency graph of your project, look for anything that transitivley depends on this com.sun.activation:jakarta.activation library, and remove the library from the application classpath.

Alex-Cosma commented 1 year ago

Thank you @scottfrederick !

estebangarciaalonso commented 1 year ago

I have the exact same issue, but I check for all references to jakarta-activation, but all references are equal...

jakarta.activation:jakarta.activation-api:2.1.0

  +--- org.springframework.boot:spring-boot-starter-mail:3.0.0
  |    +--- org.springframework.boot:spring-boot-starter:3.0.0 (*)
  |    +--- org.springframework:spring-context-support:6.0.2 (*)
  |    \--- org.eclipse.angus:jakarta.mail:1.0.0
  |         \--- jakarta.activation:jakarta.activation-api:2.1.0

I have no explicit reference to this package...

scottfrederick commented 1 year ago

@estebangarciaalonso There might be a few causes of this, but in the other examples above the problem happens when the com.sun.activation:jakarta.activation dependency is on the classpath. Please scan the entire dependency graph of your project and look for this dependency instead of jakarta.activation:jakarta.activation-api.

estebangarciaalonso commented 1 year ago

@scottfrederick Thanks for your time. It wasnt related with jakarta.activation:jakarta.activation-api at all. I had old references to javax.mail. After I deleted them, it seems to work fine (though I havent tested properly yet!)

bodiam commented 1 year ago

For anyone else who is having this problem, please analyze the full dependency graph of your project, look for anything that transitivley depends on this com.sun.activation:jakarta.activation library, and remove the library from the application classpath.

I also have this problem with Spring Boot 3 and Greenmail, but I'm using Greenmail Alpha 3, and the only dependency I have depending on com.sun.activation:jakarta.activation:2.0.1 is Greenmail:

+--- com.icegreen:greenmail-junit5:2.0.0-alpha-3
|    +--- com.icegreen:greenmail:2.0.0-alpha-3
|    |    +--- com.sun.mail:jakarta.mail:2.0.1
|    |    |    \--- com.sun.activation:jakarta.activation:2.0.1
|    |    +--- jakarta.activation:jakarta.activation-api:2.0.1 -> 2.1.0
|    |    +--- org.slf4j:slf4j-api:1.7.36 -> 2.0.4
|    |    \--- junit:junit:4.13.2 (*)
|    \--- org.junit.jupiter:junit-jupiter-api:5.9.1 (*)

Shouldn't Greenmail stop depending on this?

Nemanja3214 commented 1 year ago

Adding these two helped me, ref: https://stackoverflow.com/questions/73034101/resolved-spring-boot-starter-mail-failed-to-access-class-com-sun-activation

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>4.0.0</version>
    <exclusions>
        <exclusion>
        <groupId>org.eclipse.angus</groupId>
        <artifactId>angus-activation</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>com.sun.activation</groupId>
    <artifactId>jakarta.activation</artifactId>
    <version>2.0.1</version>
</dependency>
dbesan commented 1 year ago

Works:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.eclipse.angus</groupId>
                    <artifactId>angus-activation</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
1udaypatil1 commented 1 year ago

Hi All, I've tried everything mentioned above yet the issue still persists for me. I've raised the question here as well https://stackoverflow.com/questions/75948312/javamailsender-cannot-cast-one-of-elements-of-java-lang-object

any suggestions ?

bodiam commented 1 year ago

@1udaypatil1 This is how we solved it:

// This is here because Greenmail / Spring Boot 3.0 don't like having this dependency in the classpath.
configurations.all {
    resolutionStrategy {
        exclude("com.sun.activation", "jakarta.activation")
    }
}

With Maven you can probably do something similar, but you might have to check the whole dependency tree (mvn dependency:tree) to make sure a different dependency doesn't depend on com.sun.activation

edit: I removed the above since we upgraded to Greenmail 2.0.0, and everything still seems to work.

FWIW, I've uploaded my build.gradle.kts here: https://gist.github.com/bodiam/90ce15684f5a9b42e095151849329da0, maybe it helps.

1udaypatil1 commented 1 year ago

Thanks, removing com.sun.activation related dependencies did the job for me.

2dongyeop commented 1 year ago

Hello, I am currently implementing email authentication with JavaMailSender using Springboot 3.0.2.

I keep getting the following errors. If the spring boot version is 3 or higher, is the error not resolved?

Hello, I am currently implementing email authentication with JavaMailSender using Springboot 3.0.2.

I keep getting the following errors.

element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, jakarta.activation.MimeTypeRegistry
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:712) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:692) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:133) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:481) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1397) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:961) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:915) ~[spring-context-6.0.4.jar:6.0.4]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[spring-context-6.0.4.jar:6.0.4]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.2.jar:3.0.2]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-3.0.2.jar:3.0.2]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~[spring-boot-3.0.2.jar:3.0.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-3.0.2.jar:3.0.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-3.0.2.jar:3.0.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[spring-boot-3.0.2.jar:3.0.2]
    at io.wisoft.capstonedesign.CapstoneDesignApplication.main(CapstoneDesignApplication.java:10) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-3.0.2.jar:3.0.2]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailSender' defined in class path resource [org/springframework/boot/autoconfigure/mail/MailSenderPropertiesConfiguration.class]: Failed to instantiate [org.springframework.mail.javamail.JavaMailSenderImpl]: Factory method 'mailSender' threw exception with message: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, jakarta.activation.MimeTypeRegistry
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:657) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:645) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instanti
wilkinsona commented 1 year ago

@2dongyeop As with the other comments on this issue, it sounds like you also have javax dependencies on the classpath when they need to be jakarta. If you need some help correcting that, please follow up on Stack Overflow.

KalanaDananjaya commented 1 year ago

For anybody facing the same, I got the same issue when I had the following dependency.

<groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>${javax.activation.version}</version>

Solution was to replace it with following.

<dependency>
    <groupId>jakarta.activation</groupId>
    <artifactId>jakarta.activation-api</artifactId>
    <version>2.1.2</version>
</dependency>
molorane commented 8 months ago

Hi all,I have a Spring application that sends an email. With SpringBoot 2.7.14 it works fine. When I change to SpringBoot3+ I get this error.

Class com.sun.mail.util.LineInputStream does not implement the requested interface jakarta.mail.util.LineInputStream.

Below is the code snippet I use. Thanx


import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
import jakarta.activation.FileDataSource;
import jakarta.mail.BodyPart;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMultipart;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import [http://org.springframework.core.io](https://t.co/NSGDA1pq67).ClassPathResource;
import [http://org.springframework.core.io](https://t.co/NSGDA1pq67).FileSystemResource;
import [http://org.springframework.core.io](https://t.co/NSGDA1pq67).InputStreamSource;
import [http://org.springframework.core.io](https://t.co/NSGDA1pq67).Resource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@Autowired
private final JavaMailSender emailSender;

final Context context = new Context();
            setBasics(context, String.format("%s %s", account.getLastName(), account.getFirstName()), account.getProfileUrl());

            final String text = templateEngine.process(ACCOUNT_UPDATE_TEMPLATE, context);

            final MimeMessage message = getMimeMessage();
            final MimeMessageHelper helper = new MimeMessageHelper(message, true, UTF_8_ENCODING);
            helper.setPriority(1);
            helper.setSubject("New Account");
            helper.setFrom(fromEmail);
            helper.setTo(account.getEmail());
            helper.setText(text, true);

            // Add the inline image, referenced from the HTML code as "cid:${logo}"
            final InputStreamSource imageSource = new ClassPathResource("static/images/logo.png");
            helper.addInline(IMAGE_LOGO, imageSource, IMAGE_CONTENT_TYPE);

            emailSender.send(message);
wilkinsona commented 8 months ago

@molorane you appear to have an old non-Jakarta version of the com.sun.mail JavaMail implementation on the classpath. You should check your dependencies and their versions. If that doesn't help you to solve the problem, please follow up on Stack Overflow and include some information about all of your application's dependencies in your question.