gpt4 book ai didi

java.lang.NoClassDefFoundError : Could not initialize class javax. mail.internet.InternetAddress

转载 作者:行者123 更新时间:2023-12-01 13:37:54 30 4
gpt4 key购买 nike

我正在尝试使用 JavaMail API 发送电子邮件。这是我在 servlet 上的代码:

package com.lsp.web;

import com.lsp.service.Mailer;
import javax.ejb.EJB;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "contact", urlPatterns = {"/contact"})
public class ContactServlet extends SpringInjectedServlet {
@EJB
private Mailer emailBean;

@Override
public void init() throws ServletException {

}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String customerEmail = req.getParameter("email");
String subject = req.getParameter("subject");
String body = req.getParameter("message");

String error = null;
String succMess = null;

try {
javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
ia.validate();
emailBean.send(customerEmail, subject, body);
req.setAttribute("succMessage", succMess);
req.getRequestDispatcher("sent.jsp").forward(req, resp);

} catch (javax.mail.internet.AddressException ae) {
error = "您指出的邮箱地址不存在";
req.setAttribute("errorMessage", error);
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}
catch (MessagingException mex) {
error = "发送失败";
req.setAttribute("errorMessage", error);
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}
}
}

在我检查用户地址的行:

javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
ia.validate();

我遇到了一个异常(exception)。java.lang.NoClassDefFoundError:无法初始化类 javax.mail.internet.InternetAddress

在 pom.xml 中,我添加了这些行:

<!--JavaMail API-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.1</version>
</dependency>

<!--EJB-->
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
</dependency>

我正在使用 Tomcat。有人可以告诉我为什么会发生这种情况以及如何解决该问题。

谢谢。

最佳答案

请参阅:https://stackoverflow.com/a/28935760/1128668您已将 mail-api.jar 包含在您的项目中。这只是 API 规范。修复方法是替换此:

<!-- DO NOT USE - it's just the API, not an implementation -->
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>

该 API 的引用实现:

<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>

我知道它的包名称中有 sun,但那是最新版本。

关于java.lang.NoClassDefFoundError : Could not initialize class javax. mail.internet.InternetAddress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21124530/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com