SubEthaSMTP server running fine on port 25001. After a successful authentication from my client code to Subethasmtp server i am having this error message org.subethamail.smtp.server.Session: Unexpected error in the SMTP handler thread java.lang.NoClassDefFoundError: jakarta/mail/internet/AddressException
This is my SMTPServerConfig class
@Configuration
public class SMTPServerConfig {
private final SMTPServer smtpServer;
private final SimpleMessageListener marketingMsgListener;
private final UsernamePasswordValidator authValidator;
private final EasyAuthenticationHandlerFactory easyAuth;
public SMTPServerConfig(SimpleMessageListener marketingMsgListener) {
authValidator = new SimpleAuthValidatorImpl();
easyAuth = new EasyAuthenticationHandlerFactory(authValidator);
this.marketingMsgListener = marketingMsgListener;
this.smtpServer = SMTPServer
.port(25001)
.simpleMessageListener(this.marketingMsgListener)
.requireAuth(true)
.authenticationHandlerFactory(easyAuth)
.hostName("localhost")
.hideTLS(false)
.enableTLS(true)
.requireTLS(false)
.requireAuth(true)
.connectionTimeout(1, TimeUnit.MINUTES)
.maxMessageSize(10000)
.maxConnections(20)
.build();
this.smtpServer.stop();
this.smtpServer.start();
}
I am using usernamepasswordvalidator
@Configuration
public class SimpleAuthValidatorImpl implements UsernamePasswordValidator {
private final String CREDENTIALS_LOGIN = "christclau";
private final String CREDENTIALS_PASSWORD = "password";
@Override
public void login(String username, String password, MessageContext context) throws LoginFailedException {
if(CREDENTIALS_LOGIN.equals(username) && CREDENTIALS_PASSWORD.equals(password)){
System.out.println("yes Authenticated successfully");
}else{
System.err.println("no Invalid authentication !");
throw new LoginFailedException();
}
}
}
This is my mail client to send message to the server
public void sendMail(Mail mail) {
final String username = "christclau";
final String password = "password";
Properties prop = new Properties();
prop.put("mail.smtp.host", "localhost");
prop.put("mail.smtp.port", "25001");
prop.put("mail.smtp.auth", "true");
//prop.put("mail.debug", "true");
//prop.put("mail.smtp.starttls.enable", "true"); //TLS
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(mail.getFrom()));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(mail.getTo())
);
message.setSubject(mail.getSubject());
message.setText("HI you have done sending mail with outlook");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
This is the output message
DEBUG: setDebug: Jakarta Mail version 1.6.7
DEBUG: getProvider() returningjavax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: protocolConnect returning false, host=localhost, user=MY PC, password=<null>
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "localhost", port 25001, isSSL false 220 localhost ESMTP SubEthaSMTP null
DEBUG SMTP: connected to host "localhost", port: 25001
EHLO DESKTOP-FFJA5IP
250-localhost
250-8BITMIME
250-SIZE 10000
250-STARTTLS
250-CHUNKING
250-AUTH PLAIN LOGIN
250 Ok
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "10000"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "Ok", arg ""
DEBUG SMTP: protocolConnect login, host=localhost, user=christclau, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
yes Authenticated successfully
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
2022-02-26 11:21:40.081 ERROR 9452 --- [127.0.0.1:50466] org.subethamail.smtp.server.Session : Unexpected error in the SMTP handler thread
java.lang.NoClassDefFoundError: jakarta/mail/internet/AddressException
at org.subethamail.smtp.internal.command.MailCommand.execute(MailCommand.java:74) ~[subethasmtp-6.0.1.jar:na]
at org.subethamail.smtp.internal.server.RequireTLSCommandWrapper.execute(RequireTLSCommandWrapper.java:32) ~[subethasmtp-6.0.1.jar:na]
at org.subethamail.smtp.internal.server.RequireAuthCommandWrapper.execute(RequireAuthCommandWrapper.java:35) ~[subethasmtp-6.0.1.jar:na]
at org.subethamail.smtp.internal.server.CommandHandler.handleCommand(CommandHandler.java:86) ~[subethasmtp-6.0.1.jar:na]
at org.subethamail.smtp.server.Session.runCommandLoop(Session.java:261) ~[subethasmtp-6.0.1.jar:na]
at org.subethamail.smtp.server.Session.run(Session.java:170) ~[subethasmtp-6.0.1.jar:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.lang.ClassNotFoundException: jakarta.mail.internet.AddressException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na]
... 9 common frames omitted
This is my dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>com.github.davidmoten</groupId>
<artifactId>subethasmtp</artifactId>
<version>6.0.1</version>
</dependency>
CodePudding user response:
Spring Boot 2.6 and earlier still use the javax.mail
namespace for JakartMail (formerly JavaMail), while your SubethaSMTP version seems to expect the jakarta.mail
namespace. Spring Boot explicitly specifies versions of the JakartaMail package, see Spring Boot Dependency Versions and search for jakarta.mail. For Spring Boot 2.6.4, it lists version 1.6.7, and that version is still based on the javax.mail
namespace.
You need to downgrade SubethaSMTP to a version that still uses the javax.mail
namespace of JakartaMail/JavaMail, and wait for Spring Boot 3 before using a version that needs the jakarta.mail
namespace.
In theory, you can also set the Maven property jakarta-mail.version
to 2.0.1, but this could cause problems in other parts of Spring Boot, so I do not recommend that.