【发布时间】: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