Skip to content
Advertisement

Send Email with Spring Boot

i have a Form with From/Tp Input field and TextArea as Box. I want to send Email using Spring Boot. How can i send Email Using Spring Boot?

Thanks for Answers.

Advertisement

Answer

it works fine now

That is my Controller

@Controller
@RequestMapping("/sendmail")
public class MailController {
    @Autowired
    private MailService mailService;


    @RequestMapping(method = RequestMethod.POST)
    private String sendMail(HttpServletRequest request) {
         String covere = request.getParameter("covere");

        try {
            mailService.sendMail(covere);
            return "apply";
        } catch (MailException e) {
            e.printStackTrace();
        }
        return "apply";
    }
}

That is my Service

@Component
public class MailService {
    @Autowired
    private JavaMailSender javaMailSender;

    public void sendMail(String covere) throws MailException{

        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setTo("yottiallipierre@gmail.com");
        mail.setFrom("yottiallipierre@gmail.com");
        mail.setSubject("Test");
        mail.setText(covere);
        javaMailSender.send(mail);
    }

}

That is my property Files

spring.mail.host = smtp.gmail.com
spring.mail.port= 465
spring.mail.username = yottiallipierre@gmail.com
spring.mail.password = losangeles4004*
send.from.email= yottiallipierre@gmail.com
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.properties.mail.socketFactory.port=587
spring.mail.properties.mail.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.socketFactory.fallback=false
Advertisement