Spring Boot - Senden von E-Mails über Gmail

Ich bin versucht, senden Sie eine E-Mail mit Spring boot, aber ich bin immer der gleiche Fehler. Ich habe versucht, update von maven, saubere Projekte, ich weiß nicht, was ich tun kann.

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.

Habe ich überprüft alle links und versucht zu konfigurieren, dass ein bean auch, aber ich kann nicht damit es funktioniert. Ich habe versucht, alles von hier: Spring Boot 1.2.5.RELEASE - Senden von E-Mails über Google Mail SMTP.
Ich bin mit spring version: 1.4.5.RELEASE.
Bitte werfen Sie einen Blick auf meinen code, vielen Dank.

Anwendung.Eigenschaften

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("[email protected]");
        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("[email protected]");
    message.setSubject("Confirm appointment");
    message.setText(body);
    javaMailSender.send(message);

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

 }

hinzufügen, dies ist die Klasse, wo ich den Aufruf 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!");
    }
}

Spring Boot - Senden von E-Mails über Gmail

  • Wo haben Sie erklärt SendEmail als eine Bohne ?
  • nicht erklären SendEmail-wie eine Bohne, nur JavaMailSender, sollte es erklärt werden wie eine Bohne?(wenn ja, warum?). Ich bin mit Spring boot, normalerweise funktionieren sollte, ohne Bohnen, nur mit der Anwendung.Eigenschaften.
  • "Ohne Bohnen", das ist nicht wahr. Sie haben eine @Service annotation auf class SendEmail - das macht es eine Spring bean. Es muss jedoch definiert werden in der gleichen Verpackung oder einem subpackage Ihrer Anwendung Klasse, sonst Spring Boot wird nicht automatisch finden. (Oder Sie müssen eine @ComponentScan("sendEmail") Anmerkung zu Ihrem Antrag-Klasse, zu lassen, Spring Boot-scan das Paket sendEmail).
  • Danke! Das war das problem.
InformationsquelleAutor agata | 2017-05-16
Schreibe einen Kommentar