3

I am trying to send an email using Spring boot, but I am keep getting the same error. I tried update maven, clean projects, I don't know what else can I do.

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.

I've checked all the links and tried to configure a bean too, but I can't make it work. I've tried everything from here: Spring Boot 1.2.5.RELEASE - Sending E-mail via Gmail SMTP. I am using spring version: 1.4.5.RELEASE. Please take a look at my code, thanks a lot.

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!");
    }

 }

add this is the class where I make the call 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

Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
agata
  • 481
  • 2
  • 9
  • 29
  • Where have you declared SendEmail as a bean ? – jr593 May 16 '17 at 10:55
  • didn't declare SendEmail as a bean, only JavaMailSender, should it be declared as a bean?(if yes, why?). I am using Spring boot, normally should work without beans, only with application.properties. – agata May 16 '17 at 11:04
  • 1
    "Without beans", that's not true. You do have a `@Service` annotation on class `SendEmail` - that makes it a Spring bean. It must, however, be defined in the same package or a subpackage of your application class, otherwise Spring Boot will not find it automatically. (Or you must add a `@ComponentScan("sendEmail")` annotation to your application class, to let Spring Boot scan the package `sendEmail`). – Jesper May 16 '17 at 11:41
  • Thanks! That was the problem. – agata May 16 '17 at 12:18

1 Answers1

3

The answer:

As @Jesper suggested, the problem was I didn't put the SendEmail class in in the same package or a subpackage of my application class. What I did was put the SendEmail.java in a subpackage (com.stopcozi.sendEmail).

I didn't create and configure @Bean manually, I just let spring boot do the job. Need only SendEmail.java in the right package and the application.properties. The rest (AppointmentResource.java and pom.xml) were ok. In app.properties I used a different port.
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
Gavin Clarke
  • 379
  • 3
  • 15
agata
  • 481
  • 2
  • 9
  • 29