title: Spring Boot - Sending Email via Gmail
body: <p>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.</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>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: <a href="https://stackoverflow.com/questions/31721298/spring-boot-1-2-5-release-sending-e-mail-via-gmail-smtp">Spring Boot 1.2.5.RELEASE - Sending E-mail via Gmail SMTP</a>.
  I am using spring version: 1.4.5.RELEASE. 
  Please take a look at my code, thanks a lot. </p>
  
  <p>application.properties</p>
  
  <pre><code>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
  </code></pre>
  
  <p>pom.xml</p>
  
  <pre><code>&lt;parent&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;1.4.5.RELEASE&lt;/version&gt;
    &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
  &lt;/dependency&gt;
   ....other dependencies
  </code></pre>
  
  <p>StopCoziApplication.java</p>
  
  <pre><code>@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;
      }
  </code></pre>
  
  <p>SendEmail.java</p>
  
  <pre><code>@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!");
      }
  
   }
  </code></pre>
  
  <p>add this is the class where I make the call AppointmentResource.java</p>
  
  <pre><code>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!");
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/QebN1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QebN1.png" alt="enter image description here"></a></p>