简体   繁体   English

SpringMVC发送电子邮件

[英]SpringMVC send email

I'm trying to send email using SpringMVC. 我正在尝试使用SpringMVC发送电子邮件。 I've made a bean JavaMailSender and get an error. 我做了一个豆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;
}

Error: 错误:

04-Dec-2016 20:05:50.699 SEVERE [RMI TCP Connection(31)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed java.lang.NoClassDefFoundError: org/springframework/mail/javamail/JavaMailSender 2016年12月4日20:05:50.699严重[RMI TCP Connection(31)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext上下文初始化失败java.lang.NoClassDefFoundError:org / springframework / mail / javamail / JavaMailSender

My context with this 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;
    }
}

My pom : 我的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>
...

What did I do wrong? 我做错了什么?

It appears that you need, but do not have spring-context-support-4.3.4.RELEASE.jar on your classpath. 看来您需要,但您的类路径上没有spring-context-support-4.3.4.RELEASE.jar

To add this to your application's classpath in IntelliJ, edit your project's Project Structure , and add a Library under Libraries referencing the Spring jar file. 按此在的IntelliJ添加到应用程序的类路径中,编辑您的项目的Project Structure ,并添加一个LibraryLibraries参考弹簧jar文件。 Add or edit your Module and add the library to the Module . 添加或编辑Module ,并将库添加到Module Add or edit Artifacts and make sure the library or module is one of the Available Elements for the Artifact . 添加或编辑Artifacts ,并确保库或模块是ArtifactAvailable Elements之一。

Under the Run menu, click Edit Configurations for your Tomcat configuration, on the Deployment tab, add the artifact (or library) to the Deploy at server startup list. 在“ Run菜单下,单击“ Tomcat配置”的Run Edit Configurations ”,在“ Deployment选项卡上,将工件(或库)添加到“ Deploy at server startup列表中。

This information at JetBrains provides more information on configuring your Intellij setup to get desired jars on your classpath. JetBrains上的此信息提供有关配置Intellij设置以在类路径上获取所需jar的更多信息。

It is also possible that you need to make a simple configuration change to your dependency in your pom.xml: 您还可能需要对pom.xml中的依赖项进行简单的配置更改:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.3.4.RELEASE</version>
    <scope>runtime</scope>
</dependency>

(NOTE the addition of the <scope> element. More information on Maven scopes available here .) (请注意,添加了<scope>元素。 有关Maven范围的更多信息,请点击此处 。)

(These instructions may vary depending on the version of Intellij that you are using.) (这些说明可能会因所使用的Intellij版本而异。)

In summary, you need to get the Spring jar on your application's classpath at runtime. 总之,您需要在运行时在应用程序的类路径上获取Spring jar。

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

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