Manoj
Manoj

Reputation: 1540

jakarta.mail.Provider: com.sun.mail.imap.IMAPProvider not a subtype

I am new to Springboot and Java. I am trying to send an email from my Gmail account via Spring Boot. So far I was able to run the code after all configurations. But throws

jakarta.mail.Provider: com.sun.mail.imap.IMAPProvider not a subtype

When calling the send method of MailSender.

@Service
@EnableAutoConfiguration
public class EmailService {

    @Autowired
    private MailSender mailSender;

    public void sendEmail(String to, String subject, String text) {
        try{
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            mailSender.send(message);
        } catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

application.properties

    spring.mail.host=smtp.gmail.com
    spring.mail.port=587
    spring.mail.username=mygmailaccount
    spring.mail.password=password
    spring.mail.properties.mail.smtp.auth = true
    spring.mail.properties.mail.smtp.starttls.enable = true
    # Other properties
    spring.mail.properties.mail.smtp.connectiontimeout=5000
    spring.mail.properties.mail.smtp.timeout=5000
    spring.mail.properties.mail.smtp.writetimeout=5000

build.gradle

 implementation 'org.springframework.boot:spring-boot-starter-mail'
 testImplementation 'org.springframework.boot:spring-boot-starter-test'
 //implementation group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'
 implementation group: 'com.sun.mail', name: 'jakarta.mail', version: '1.6.7'

Tried everything. Still not able to fix this. Thanks in advance.

Upvotes: 2

Views: 3755

Answers (3)

Guss
Guss

Reputation: 32374

For me the problem was that a dependency has brought in an upgrade from javax.mail and javax.activation to jakarta.mail and jakarta.activation and that caused a conflict with my project that used the old javax stuff.

I remove the (obsolete) javax stuff from my project and updated to the jakarta stuff - and that solved the problem for me.

Upvotes: 0

Shudhanshu Choudhary
Shudhanshu Choudhary

Reputation: 11

Just check for duplicate maven dependency for javax.mail and remove it if it's there in pom.xml. That worked for me

Upvotes: 1

Syed Sheheryar Umair
Syed Sheheryar Umair

Reputation: 109

My understanding till now says that this is a version compatibility issue between your Jakarta and SpringBoot. If I was in your place I would do below steps

  • Remove the explicit dependency on Jakarta and let the Spring Boot handle it.
  • Comment out your last line of build.gradle file and it will look like this

//implementation group: 'com.sun.mail', name: 'jakarta.mail', version: '1.6.7'

Make sure to run gradle clean build command after this

Upvotes: 2

Related Questions