javaspring-bootjakarta-mail

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.


Solution

  • 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

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

    Make sure to run gradle clean build command after this