Spring Boot - Sending Email via Gmail
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: [email protected]
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
agata
Updated on June 06, 2022Comments
-
agata almost 2 years
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: [email protected] 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!"); } }
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!"); } }
-
Vinay Prajapati about 6 yearsIT still don't work for me. Also why we are declaring same properties in application.properties and then in config file?
-
agata about 6 yearsI have deleted the method, I have only app.properties
-
Andrea Girardi over 5 yearsI have added spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com and it works on my side