spring boot mail无法找到JavaMailSender来自动装配

时间:2016-08-03 09:40:47

标签: spring-boot email-integration

只是尝试运行以下测试。

我收到NoSuchBeanDefinitionException:运行时没有[org.springframework.mail.javamail.JavaMailSender]类型的限定bean。

我缺少什么?

pom:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.1</version>
    </dependency>

测试类:

@RunWith(SpringRunner.class)
public class SendEmailTest {

@Autowired
private JavaMailSender javaMailService;

@Test
public void testSendEmail() {

    SimpleMailMessage mailMessage = new SimpleMailMessage();
    String msgText = "This is your <b>mail message</b> from the <h3>Java Test</h3> !";

    mailMessage.setTo("xxx.yyy@zzzz.com");
    mailMessage.setSubject("A test from Java");
    mailMessage.setText(msgText);

    javaMailService.send(mailMessage);
}

}

1 个答案:

答案 0 :(得分:-1)

试试这个,

<?php
    $response = array();
    $response["param1"] = "param1";
    echo json_encode($response);
?>

在您的配置类中,

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes = YourApplication.class)
    public class SendEmailTest {

        @Autowired
        private JavaMailSender javaMailService;

    }