diff --git a/pom.xml b/pom.xml index e15ce7b..569769b 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,10 @@ org.springframework.boot spring-boot-starter + + org.springframework.boot + spring-boot-starter-mail + org.springframework.boot spring-boot-starter-data-jdbc @@ -34,6 +38,10 @@ h2 runtime + + org.springframework.boot + spring-boot-starter-thymeleaf + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/com/ad/markalive/service/email/EmailSenderService.java b/src/main/java/com/ad/markalive/service/email/EmailSenderService.java new file mode 100644 index 0000000..56942b2 --- /dev/null +++ b/src/main/java/com/ad/markalive/service/email/EmailSenderService.java @@ -0,0 +1,39 @@ +package com.ad.markalive.service.email; + +import org.springframework.mail.MailException; +import org.springframework.mail.MailSender; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.stereotype.Component; + +@Component +public class EmailSenderService { + private MailSender mailSender; + private SimpleMailMessage templateMessage; + + public EmailSenderService(MailSender mailSender) { + this.mailSender = mailSender; + } + + public void sendEmail(String toAddress, String fromAddress, String subject, String body) { + SimpleMailMessage templateMessage = new SimpleMailMessage(); + templateMessage.setTo(toAddress); + templateMessage.setFrom(fromAddress); + templateMessage.setSubject(subject); + templateMessage.setText(body); + try{ + this.mailSender.send(templateMessage); + } catch (MailException ex) { + // simply log it and go on... + System.err.println(ex.getMessage()); + } + + } + + public SimpleMailMessage getTemplateMessage() { + return templateMessage; + } + + public void setTemplateMessage(SimpleMailMessage templateMessage) { + this.templateMessage = templateMessage; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 514d78b..d5ef20b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -8,3 +8,11 @@ spring.datasource.platform=h2 spring.sql.init.mode=always #spring.jpa.defer-datasource-initialization=true +#mail properties +spring.mail.host=smtp.gmail.com +spring.mail.port=587 +spring.mail.username= +spring.mail.password= +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.starttls.enable=true +