【问题标题】:Spring Boot - Sending Email via GmailSpring Boot - 通过 Gmail 发送电子邮件
【发布时间】:2017-10-15 08:56:54
【问题描述】:

我正在尝试使用 Spring Boot 发送电子邮件,但我不断收到相同的错误。我试过更新maven,清理项目,我不知道我还能做什么。

Field serviceSendEmail in com.stopcozi.resource.AppointmentResource required 
a bean of type 'sendEmail.SendEmail' that could not be found.
Action:
Consider defining a bean of type 'sendEmail.SendEmail' in your configuration.

我检查了所有链接并尝试配置一个 bean,但我无法使其工作。我已经尝试了这里的一切:Spring Boot 1.2.5.RELEASE - Sending E-mail via Gmail SMTP。 我使用的是春季版本:1.4.5.RELEASE。 请看一下我的代码,非常感谢。

application.properties

spring.mail.host: smtp.gmail.com
spring.mail.port: 587
spring.mail.username: xxx@gmail.com
spring.mail.password: xxx
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.starttls.enable: true
spring.mail.properties.mail.smtp.starttls.required: true
spring.mail.properties.mail.smtp.ssl.enable = true
spring.mail.test-connection=true

pom.xml

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.5.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
 ....other dependencies

StopCoziApplication.java

@SpringBootApplication
@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})  
public class StopCoziApplication {

public static void main(String[] args) {
    SpringApplication.run(StopCoziApplication.class, args);
}

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

        javaMailSender.setHost("smtp.gmail.com");
        javaMailSender.setPort(587);

        javaMailSender.setJavaMailProperties(getMailProperties());
        javaMailSender.setUsername("xxx@gmail.com");
        javaMailSender.setPassword("xxx");

        return javaMailSender;
    }

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

SendEmail.java

@Service
public class SendEmail  {

@Autowired
private JavaMailSender javaMailSender;

@PostConstruct
public void sendMail(String to,String body) {

    System.out.println("Sending email...");

    SimpleMailMessage message = new SimpleMailMessage();
    message.setTo(to);
    message.setFrom("xxx@gmail.com");
    message.setSubject("Confirm appointment");
    message.setText(body);
    javaMailSender.send(message);

    System.out.println("Email Sent!");
    }

 }

添加这是我调用 AppointmentResource.java 的类

public class AppointmentResource {

    @Autowired
    private AppointmentService appointmentService;

    @Autowired
    SendEmail serviceSendEmail;

    @RequestMapping("/{id}/confirm")
    public void confirmAppointment(@PathVariable("id") Long id) {
        appointmentService.confirmAppointment(id);
        Appointment appointment = appointmentService.findAppointment(id);
        String email = appointment.getUser().getEmail();
        serviceSendEmail.sendMail(email, "Your appointment was confirmed"
        +appointment.getAgency()+" service "+appointment.getService()+" "
                + "date "+appointment.getDate()+ " Have a nice day!");
    }
}

【问题讨论】:

  • 您在哪里将 SendEmail 声明为 bean?
  • 没有将 SendEmail 声明为 bean,只有 JavaMailSender,是否应该将其声明为 bean?(如果是,为什么?)。我正在使用 Spring boot,通常应该在没有 bean 的情况下工作,只有 application.properties。
  • “没有豆子”,这不是真的。你在 SendEmail 类上有一个 @Service 注释——这使它成为一个 Spring bean。但是,它必须定义在您的应用程序类的同一个包或子包中,否则 Spring Boot 不会自动找到它。 (或者你必须在你的应用类中添加一个@ComponentScan("sendEmail")注解,让Spring Boot扫描包sendEmail)。
  • 谢谢!这就是问题所在。

标签: java maven email spring-boot


【解决方案1】:

答案:

正如@Jesper 建议的那样,问题是我没有将 SendEmail 类放在同一个包或我的应用程序类的子包中。我所做的是将 SendEmail.java 放在一个子包 (com.stopcozi.sendEmail) 中。

我没有手动创建和配置@Bean,我只是让 Spring Boot 完成这项工作。 只需要正确包中的SendEmail.java 和application.properties。其余的(AppointmentResource.java 和 pom.xml)没问题。在 app.properties 中,我使用了不同的端口。
应用程序.properties

#spring-boot-starter-mail properties
spring.mail.host: smtp.gmail.com
spring.mail.port: 465
spring.mail.username: xxx@gmail.com
spring.mail.password: xxx
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.starttls.enable: true
spring.mail.properties.mail.smtp.starttls.required: true
spring.mail.properties.mail.smtp.ssl.enable: true
spring.mail.test-connection: true

【讨论】:

  • 它仍然不适合我。还有为什么我们在 application.properties 然后在配置文件中声明相同的属性?
  • 我已经删除了方法,我只有app.properties
  • 我添加了 spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com 并且它在我这边工作
猜你喜欢
  • 1970-01-01
  • 2012-01-07
  • 2023-03-19
  • 2016-09-26
  • 2011-06-01
  • 2018-03-21
相关资源
最近更新 更多