Spring Boot - 通过Gmail发送电子邮件

时间:2017-05-16 10:18:40

标签: java maven email spring-boot

我正在尝试使用Spring启动发送电子邮件,但我一直收到同样的错误。我尝试了更新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。 我使用的是spring版本: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!");
    }
}

enter image description here

1 个答案:

答案 0 :(得分:2)

答案:

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

我没有手动创建和配置@Bean,我只是让spring boot完成这项工作。 只需要右侧包中的SendEmail.java和application.properties。其余的(AppointmentResource.java和pom.xml)还可以。在app.properties中,我使用了不同的端口 application.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
相关问题