-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58eebc8
commit 5a289bc
Showing
10 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/com/iamnbty/training/backend/business/EmailBusiness.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.iamnbty.training.backend.business; | ||
|
||
import com.iamnbty.training.backend.exception.BaseException; | ||
import com.iamnbty.training.backend.exception.EmailException; | ||
import com.iamnbty.training.backend.service.EmailService; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.FileCopyUtils; | ||
import org.springframework.util.ResourceUtils; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
@Service | ||
public class EmailBusiness { | ||
|
||
private final EmailService emailService; | ||
|
||
public EmailBusiness(EmailService emailService) { | ||
this.emailService = emailService; | ||
} | ||
|
||
public void sendActivateUserEmail(String email, String name, String token) throws BaseException { | ||
// prepare content (HTML) | ||
String html; | ||
try { | ||
html = readEmailTemplate("email-activate-user.html"); | ||
} catch (IOException e) { | ||
throw EmailException.templateNotFound(); | ||
} | ||
|
||
String finalLink = "http://localhost:4200/activate/" + token; | ||
html = html.replace("${P_NAME}", name); | ||
html = html.replace("${LINK}", finalLink); | ||
|
||
// prepare subject | ||
String subject = "Please activate your account"; | ||
|
||
|
||
emailService.send(email, subject, html); | ||
} | ||
|
||
private String readEmailTemplate(String filename) throws IOException { | ||
File file = ResourceUtils.getFile("classpath:email/" + filename); | ||
return FileCopyUtils.copyToString(new FileReader(file)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/iamnbty/training/backend/exception/EmailException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.iamnbty.training.backend.exception; | ||
|
||
public class EmailException extends BaseException { | ||
|
||
public EmailException(String code) { | ||
super("email." + code); | ||
} | ||
|
||
public static EmailException templateNotFound() { | ||
return new EmailException("template.not.found"); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/iamnbty/training/backend/schedule/UserSchedule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.iamnbty.training.backend.schedule; | ||
|
||
import com.iamnbty.training.backend.service.UserService; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Log4j2 | ||
public class UserSchedule { | ||
|
||
private final UserService userService; | ||
|
||
public UserSchedule(UserService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
// Schedule Note | ||
// 1 => second | ||
// 2 => minute | ||
// 3 => hour | ||
// 4 => day | ||
// 5 => month | ||
// 6 => year | ||
|
||
/** | ||
* Every minute (UTC Time) | ||
*/ | ||
@Scheduled(cron = "0 * * * * *") | ||
public void testEveryMinute() { | ||
log.info("Hello, What's up?"); | ||
} | ||
|
||
/** | ||
* Everyday at 00:00 (UTC Time) | ||
*/ | ||
@Scheduled(cron = "0 0 0 * * *") | ||
public void testEveryMidNight() { | ||
|
||
} | ||
|
||
/** | ||
* Everyday at 10:50 AM (Thai Time) | ||
*/ | ||
@Scheduled(cron = "0 50 10 * * *", zone = "Asia/Bangkok") | ||
public void testEverydayNineAM() { | ||
log.info("Hey Hoo!!!"); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/iamnbty/training/backend/service/EmailService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.iamnbty.training.backend.service; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
import org.springframework.mail.javamail.MimeMessagePreparator; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class EmailService { | ||
|
||
private final JavaMailSender mailSender; | ||
@Value("${app.email.from}") | ||
private String from; | ||
|
||
public EmailService(JavaMailSender mailSender) { | ||
this.mailSender = mailSender; | ||
} | ||
|
||
public void send(String to, String subject, String html) { | ||
MimeMessagePreparator message = mimeMessage -> { | ||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); | ||
helper.setFrom(from); | ||
helper.setTo(to); | ||
helper.setSubject(subject); | ||
helper.setText(html, true); | ||
}; | ||
mailSender.send(message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
spring: | ||
mail: | ||
host: smtp.gmail.com | ||
port: 587 | ||
username: YOUR_USERNAME | ||
password: YOUR_PASSWORD | ||
properties: | ||
mail: | ||
smtp: | ||
auth: true | ||
starttls: | ||
enable: true | ||
jpa: | ||
hibernate: | ||
ddl-auto: update | ||
|
@@ -16,4 +27,6 @@ app: | |
token: | ||
secret: m#MySecretGGWP2021 | ||
issuer: BackendService | ||
email: | ||
from: [email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Welcome to Backend</title> | ||
</head> | ||
<body> | ||
|
||
<h1>Hello, ${P_NAME}</h1> | ||
<h2>Please activate your account</h2> | ||
<p>Click <a href="${LINK}">here</a></p> | ||
|
||
</body> | ||
</html> |
39 changes: 39 additions & 0 deletions
39
src/test/java/com/iamnbty/training/backend/TestEmailBusiness.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.iamnbty.training.backend; | ||
|
||
import com.iamnbty.training.backend.business.EmailBusiness; | ||
import com.iamnbty.training.backend.exception.BaseException; | ||
import org.junit.jupiter.api.MethodOrderer; | ||
import org.junit.jupiter.api.Order; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestMethodOrder; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
class TestEmailBusiness { | ||
|
||
@Autowired | ||
private EmailBusiness emailBusiness; | ||
|
||
@Order(1) | ||
@Test | ||
void testSendActivateEmail() throws BaseException { | ||
emailBusiness.sendActivateUserEmail( | ||
TestData.email, | ||
TestData.name, | ||
TestData.token | ||
); | ||
} | ||
|
||
interface TestData { | ||
|
||
String email = "[email protected]"; | ||
|
||
String name = "Natthapon Pinyo"; | ||
|
||
String token = "m#@:LSDIFDISIDFO99020kkddd"; | ||
|
||
} | ||
|
||
} |