我尝试使用SpringMVC发送电子邮件。我做了一个bean JavaMailSender并收到错误。
@Bean
public JavaMailSender javaMailSender(){
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setUsername("test");
javaMailSender.setPassword("test");
javaMailSender.setPort(56);
javaMailSender.setHost("smtp.test.ru");
return javaMailSender;
}
错误:
04-Dec-2016 20:05:50.699严重[RMI TCP连接(31)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext上下文初始化失败 java.lang.NoClassDefFoundError:org / springframework / mail / javamail / JavaMailSender
我对这个bean的上下文:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
@Configuration
@PropertySource("classpath:util.properties")
@PropertySource(value = {"classpath:auth.properties"})
public class MailContext {
@Bean
public JavaMailSender javaMailSender(){
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setUsername("test");
javaMailSender.setPassword("test");
javaMailSender.setPort(556);
javaMailSender.setHost("test.ru");
javaMailSender.setProtocol("smtp");
Properties properties = new Properties();
properties.setProperty("mail.debug", "true");
javaMailSender.setJavaMailProperties(properties);
return javaMailSender;
}
}
我的pom:
...
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
...
我做错了什么?
答案 0 :(得分:0)
您似乎需要,但在类路径上没有spring-context-support-4.3.4.RELEASE.jar
。
要在IntelliJ中将其添加到应用程序的类路径中,请编辑项目的Project Structure
,并在引用Spring jar文件的Library
下添加Libraries
。添加或修改Module
并将库添加到Module
。添加或修改Artifacts
并确保库或模块是Available Elements
的{{1}}之一。
在Artifact
菜单下,单击Run
进行Tomcat配置,在Edit Configurations
选项卡上,将工件(或库)添加到Deployment
列表。
This information at JetBrains提供了有关配置Intellij设置以在类路径上获取所需jar的更多信息。
您还可能需要在pom.xml中对依赖项进行简单的配置更改:
Deploy at server startup
(注意添加<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.4.RELEASE</version>
<scope>runtime</scope>
</dependency>
元素。More information on Maven scopes available here。)
(这些说明可能因您使用的Intellij版本而异。)
总之,您需要在运行时在应用程序的类路径上获取Spring jar。