简体   繁体   English

Spring 4 MVC 中的 javax/mail/MessagingException

[英]javax/mail/MessagingException in Spring 4 MVC

I have an application (Spring 4 MVC+Hibernate 4+MySQL+Maven integration example using annotations) , integrating Spring with Hibernate using annotation based configuration.我有一个应用程序(使用注释的 Spring 4 MVC+Hibernate 4+MySQL+Maven 集成示例),使用基于注释的配置将 Spring 与 Hibernate 集成。

I have this error when starting the appl.启动应用程序时出现此错误。

GRAVE: Excepción enviando evento inicializado de contexto a instancia de escuchador de clase org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: javax/mail/MessagingException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
    at java.lang.Class.getDeclaredMethods(Class.java:1860)
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:609)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:521)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:507)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1069)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5001)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1854)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1703)
    ... 30 more

I have this in the pom.xml我在 pom.xml 中有这个

 <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>

and I've checked the repository and is there:我检查了存储库并且在那里:

/Users/paquirrin/.m2/repository/javax/mail/mail/1.4

I've updated the maven project 1000 times, but it doesn't work!我已经更新了 Maven 项目 1000 次,但它不起作用!

I run the project in a tomcat7 at localhost我在本地主机的 tomcat7 中运行该项目

Seems you are using the wrong dependency.似乎您使用了错误的依赖项。 Try the following:请尝试以下操作:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mailapi</artifactId>
    <version>1.4.3</version>
</dependency>

Make sure to refresh the dependencies, and rebuild the project.确保刷新依赖项,并重建项目。

You should not have a need to add the mail api's to your classpath, since spring will include them transitively ( given you are using spring-boot ).您不需要将邮件 api 添加到您的类路径中,因为 spring 将传递地包含它们(假设您使用的是 spring-boot )。

In case you are using spring-framework without boot, you can (as suggested in the documentation ), also ensure the JavaMail library ( com.sun.mail:javax.mail ) is on the classpath.如果您在没有引导的情况下使用 spring-framework,您可以(如文档中所建议的那样)还确保JavaMail库( com.sun.mail:javax.mail )位于类路径中。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.3.27.RELEASE</version>
</dependency>

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

The artifact has been already moved from javax.mail >> javax.mail-api .该工件已从javax.mail >> javax.mail-api移出。

So, it is better not to use this given below.所以,最好不要使用下面给出的这个。

<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4</version>
</dependency>

Instead, use this latest dependency :相反,使用这个最新的依赖项:

<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.5.6</version>
</dependency>

After adding this dependency, then do these steps:添加此依赖项后,请执行以下步骤:

  • Go to Project Menu >> Activate Build Automatically Option >> Then Click on Clean Option there.转到项目菜单 >> 激活自动构建选项 >> 然后单击那里的清理选项。

  • After that, do Maven Clean and Maven Install .之后,执行Maven CleanMaven Install

  • Delete the war and the extracted war folder from tomcat/webapps and redeploy the war again.从 tomcat/webapps 中删除 war 和解压出来的 war 文件夹,然后重新部署 war。

Through this, the problem may be solved.这样,问题就可以解决了。

There is a requirement for some jars according to Spring 4 docs in order to make it work.根据 Spring 4 文档,需要一些 jars 才能使其工作。 You are probably missing some of them.你可能错过了其中的一些。

The following additional jars to be on the classpath of your application in order to be able to use the Spring Framework's email library.为了能够使用 Spring Framework 的电子邮件库,将在您的应用程序的类路径中添加以下额外的 jar。

All of these libraries are freely available on the web.所有这些库都可以在网络上免费获得。

Or there might be something with Maven.或者 Maven 可能有什么问题。 Sometimes it helps if you remove .m2 folder and then run mvn install on your project to download all the dependencies with the right versions.有时,如果您删除.m2文件夹,然后在您的项目上运行mvn install以下载具有正确版本的所有依赖项,这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM