5

我正在使用Spring Boot 2.0.0.BUILD-SNAPSHOT. 自动装配JavaMailSenderJavaMailSenderImpl. 如果我配置@AutowiredJavaMailSender,我得到以下错误。

***************************
APPLICATION FAILED TO START
***************************

Description:

Field mailSender in com.hm.assetmanagment.service.MailService required a bean of type 'org.springframework.mail.javamail.JavaMailSenderImpl' that could not be found.
    - Bean method 'mailSender' not loaded because @ConditionalOnClass did not find required class 'javax.mail.internet.MimeMessage'

Action:

Consider revisiting the conditions above or defining a bean of type 'org.springframework.mail.javamail.JavaMailSenderImpl' in your configuration.

下面是我的 pom.xml,它有 spring boot starter email。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hm.assetmanagement</groupId>
    <artifactId>AssetManagementSystem</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>AssetManagementSystem</name>
    <description>AssetManagementSystem</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- <dependency> 
            <groupId>javax.mail</groupId> 
            <artifactId>mail</artifactId> 
            <version>1.4.7</version> 
        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <!-- <exclusions>
            <exclusion>                  
                <groupId>com.sun.mail</groupId>
                <artifactId>javax.mail</artifactId>
            </exclusion>
        </exclusions>-->
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

package com.hm.assetmanagment.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MailService {

    @Autowired
    private JavaMailSenderImpl mailSender;

    @RequestMapping("/assets/allocateemail/{name}/{email}/{assetid}")
    private void sendAssetAllocationEmail(String name, String email, String assetid) {
        /*SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setTo(email);
        simpleMailMessage.setSubject("Asset Allocation Confirmation");
        simpleMailMessage.setText("Hello " + name + "," + assetid + "\n" + " - Asset allocated to you");
        mailSender.send(simpleMailMessage);*/

    }
}

我尝试通过依赖手动添加 javax.mail jar/spring 上下文支持,但它没有工作。

application.properties:
spring.mail.host=smtp.gmail.com
spring.mail.username=xxx@gmail.com
spring.mail.password=xxx
spring.mail.port=587

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

spring.thymeleaf.cache=false
debug=true

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Main Class:
package com.hm.assetmanagment;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories
@SpringBootApplication
public class AssetManagementSystemApplication {

    public static void main(String[] args) {
        SpringApplication.run(AssetManagementSystemApplication.class, "--debug");
        //SpringApplication.run(AssetManagementSystemApplication.class, args);
    }
}

请指导。

来自我 Nov22 的更新:

如果我创建新类并在该类上启用自动配置,JavaMailSender 和 JavaMailSenderImpl 将正确自动装配,并且 Spring Boot 应用程序成功启动。我已经在 application.class 中启用了自动配置。使用 enableautoconfiguration 配置两个类可以吗?有没有其他方法可以自动装配 JavaMailSender?

@EnableAutoConfiguration
public class MailConfig {

    @Autowired
    private JavaMailSenderImpl mailSender;
}
4

3 回答 3

7

好吧,看起来你根本没有配置Mailer,因此 Spring 找不到 bean 来连接它。如果这是一个依赖问题,那么你会得到一个ClassNotFoundExceptionor NoClassDefException,这不是你的情况。无论如何尝试像这样配置您的邮件程序:

@Configuration 
public class MailConfig {

    @Bean
    public JavaMailSender javaMailService() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        javaMailSender.setHost("myHost");
        javaMailSender.setPort(25);

        javaMailSender.setJavaMailProperties(getMailProperties());

        return javaMailSender;
    }

    private Properties getMailProperties() {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.auth", "false");
        properties.setProperty("mail.smtp.starttls.enable", "false");
        properties.setProperty("mail.debug", "false");
        return properties;
    }
}

原始答案在这里

于 2016-11-20T19:42:32.133 回答
1

通过查看您的代码,您需要添加以下属性才能工作

示例 app.yml 文件 spring mail: default-encoding: UTF-8 host: localhost port: 8025 protocol: smtp test-connection: false properties.mail.smtp: starttls.enable: true


Here is the reference 
https://docs.spring.io/spring-boot/docs/2.1.x/reference/html/boot-features-email.html

确保您正在阅读应用程序属性

于 2020-07-06T06:10:20.730 回答
0

改变

@Autowired
private JavaMailSenderImpl mailSender;

@Autowired
private JavaMailSender mailSender;

这样做的原因是,如果 bean 类实现了接口,则默认情况下 Spring 会创建 JDK 代理。JDK 代理实现了与 bean 类相同的接口,但不扩展该类。

于 2016-11-20T19:40:03.593 回答