SubEthaSMTP SMTP 处理程序线程中出现意外错误 NoClassDefFoundError jakarta/mail/internet/AddressException

SubEthaSMTP Unexpected error in the SMTP handler thread NoClassDefFoundError jakarta/mail/internet/AddressException

SubEthaSMTP 服务器 运行 在端口 25001 上正常。从我的客户端代码到 Subethasmtp 服务器的身份验证成功后,我收到此错误消息 org.subethamail.smtp.server.Session:SMTP 处理程序线程中出现意外错误 java.lang.NoClassDefFoundError: jakarta/mail/internet/AddressException

这是我的 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();
   }

我正在使用用户名密码验证器

 @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();
      }
   }
 }

这是我的邮件客户端向服务器发送消息

 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();
    }
  }

这是输出消息

 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:<christoozu@gmail.com>
 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

这是我的依赖

 <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>
 

Spring Boot 2.6 及更早版本仍然使用 JakartaMail(以前称为 JavaMail)的 javax.mail 命名空间,而您的 SubEtha SMTP 版本似乎需要 jakarta.mail 命名空间。 Spring 引导明确指定 JakartaMail 包的版本,请参阅 Spring Boot Dependency Versions 并搜索 jakarta.mail。对于 Spring Boot 2.6.4,它列出了版本 1.6.7,该版本仍然基于 javax.mail 命名空间。

您需要将 SubEtha SMTP 降级到仍然使用 JakartaMail/JavaMail 的 javax.mail 命名空间的版本,并在使用需要 [=] 的版本之前等待 Spring Boot 3 11=] 命名空间。

理论上你也可以将Maven 属性 jakarta-mail.version设置为2.0.1,但是这样会导致Spring Boot其他部分出现问题,所以不推荐那。