Skip to content

Commit

Permalink
Merge pull request #5 from adilk0728/email-support
Browse files Browse the repository at this point in the history
Add simple email service
  • Loading branch information
adilk0728 authored Dec 20, 2023
2 parents 748e7f2 + e17c199 commit a2fcb86
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
Expand All @@ -34,6 +38,10 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 8 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a2fcb86

Please sign in to comment.