I'm trying to test the delivery of an email via Gmail using Kotlin but when I run the project, a console project, I get the error
arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, jakarta.activation.MailcapRegistry
The code is quite straightforward. This is the main.kt file:
fun main(args: Array<String>) {
// Testing email
val mailService = MailService()
mailService.sendEmail("aaa.bbb@gmail.com",
"aaa.bbb@gmail.com", "Test", "This is a test")
}
and the MailService class is the following:
import jakarta.mail.MessagingException
import jakarta.mail.Authenticator
import jakarta.mail.Message
import jakarta.mail.PasswordAuthentication
import jakarta.mail.Session
import jakarta.mail.internet.InternetAddress
import jakarta.mail.internet.MimeMessage
import java.util.*
class MailService {
private val session : Session
init {
val username = "aaa.bbb@gmail.com"
val password = "123456789"
val properties = Properties()
properties["mail.smtp.host"] = "smtp.gmail.com"
properties["mail.smtp.port"] = "587"
properties["mail.smtp.auth"] = "true";
properties["mail.smtp.starttls.enable"] = "true"; //TLS
val authenticator = object : Authenticator() {
override fun getPasswordAuthentication() : PasswordAuthentication {
return PasswordAuthentication(username, password)
}
}
session = Session.getInstance(properties, authenticator)
session.debug = true
}
fun sendEmail(fromEmailAddress : String,
toEmailAddress : String,
subject : String,
bodyText : String) : Unit {
try {
var email = MimeMessage(session);
email.setFrom(InternetAddress(fromEmailAddress, false));
email.addRecipient(Message.RecipientType.TO, InternetAddress(toEmailAddress, false));
email.subject = subject
email.setText(bodyText)
val smtpTransport = session.getTransport("smtp")
smtpTransport.connect()
smtpTransport.sendMessage(email, email.allRecipients)
smtpTransport.close()
}
catch (messagingException: MessagingException) {
messagingException.printStackTrace()
}
}
}
and these are the dependencies in the POM file:
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>1.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.8.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.mail/jakarta.mail-api -->
<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
<version>2.1.2</version>
<exclusions>
<exclusion>
<groupId>com.sun.activation</groupId>
<artifactId>jakarta.activation</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://github.com/eclipse-ee4j/angus-mail -->
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
</project>
I tried the solution Tomcat 10, Java 17 - JavaMailSender cannot cast one of elements of java.lang.Object[] but it didn't work. I'm not getting anymore ideas where the problem is. It might be that the issue is under my nose but I'm not seeing it :-) Any suggestion? Thank you.