-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore : 메일 의존성 추가 * docs : Project Site Link 추가 * feat : 메일 기능 추가 * feat : 메일 기능 사용법 SampleCase 추가 * feat : 메일 위치 상수 클래스 생성
- Loading branch information
1 parent
31c5a36
commit 0835235
Showing
10 changed files
with
178 additions
and
7 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
Api/src/main/java/picasso/server/api/mail/MailPathConstants.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,5 @@ | ||
package picasso.server.api.mail; | ||
|
||
public class MailPathConstants { | ||
public static String SAMPLE_MAIL = "mail/sample"; | ||
} |
39 changes: 39 additions & 0 deletions
39
Api/src/main/java/picasso/server/api/mail/TestMailController.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 picasso.server.api.mail; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import picasso.server.common.mail.SendMailUtil; | ||
|
||
import java.util.HashMap; | ||
|
||
/* | ||
* TODO : 해당 컨트롤러 사용법 안내를 위한 TestController로 추후 삭제가 필요합니다. | ||
*/ | ||
@Slf4j | ||
@RequestMapping("/mail") | ||
@RequiredArgsConstructor | ||
@RestController | ||
public class TestMailController { | ||
private final SendMailUtil sendMailUtil; | ||
|
||
/** | ||
* @param mail 메일을 파라미터로 필요 id@mailHost | ||
* @return | ||
*/ | ||
@GetMapping("/test") | ||
public boolean testMail(@RequestParam("mail") String mail) { | ||
return sendMailUtil.sendMail( | ||
mail, | ||
"테스트메일입니다.", | ||
MailPathConstants.SAMPLE_MAIL, | ||
new HashMap<>() {{ | ||
put("a", "A변수 데이터 입니다."); | ||
put("b", "B변수 데이터 입니다."); | ||
}}); | ||
} | ||
|
||
} |
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,16 @@ | ||
<!DOCTYPE html> | ||
<html | ||
lang="ko" | ||
xmlns="http://www.w3.org/1999/xhtml" | ||
xmlns:th="http://www.thymeleaf.org" | ||
> | ||
<body> | ||
<div> | ||
<h1> 테스트 SampleMail 입니다.</h1> | ||
<h1 th:text="${a}"></h1> | ||
<br> | ||
|
||
<div th:text="${b}"></div> | ||
</div> | ||
</body> | ||
</html> |
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
11 changes: 11 additions & 0 deletions
11
Common/src/main/java/picasso/server/common/exception/IllegalArgumentException.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,11 @@ | ||
package picasso.server.common.exception; | ||
|
||
import static picasso.server.common.exception.GlobalException.ILLEGAL_ARGUMENT_ERROR; | ||
|
||
public class IllegalArgumentException extends BaseException { | ||
public static final BaseException EXCEPTION = new IllegalArgumentException(); | ||
|
||
public IllegalArgumentException() { | ||
super(ILLEGAL_ARGUMENT_ERROR); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Common/src/main/java/picasso/server/common/exception/MailSendException.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,11 @@ | ||
package picasso.server.common.exception; | ||
|
||
import static picasso.server.common.exception.GlobalException.MAIL_SEND_ERROR; | ||
|
||
public class MailSendException extends BaseException { | ||
public static final MailSendException EXCEPTION = new MailSendException(); | ||
|
||
public MailSendException() { | ||
super(MAIL_SEND_ERROR); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
Common/src/main/java/picasso/server/common/mail/SendMailUtil.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,71 @@ | ||
package picasso.server.common.mail; | ||
|
||
import jakarta.mail.Message; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Component; | ||
import org.thymeleaf.context.Context; | ||
import org.thymeleaf.spring6.SpringTemplateEngine; | ||
import picasso.server.common.exception.IllegalArgumentException; | ||
import picasso.server.common.exception.MailSendException; | ||
|
||
import java.util.Map; | ||
|
||
import static org.apache.commons.codec.CharEncoding.UTF_8; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class SendMailUtil { | ||
private final JavaMailSender javaMailSender; | ||
private final SpringTemplateEngine templateEngine; | ||
|
||
|
||
/** | ||
* Key는 현재 Mail에 사용중인 변수를, Value는 해당 메일 페이지에 들어갈 값을 입력하면 됩니다. | ||
* | ||
* @param toUser 발송자 메일 | ||
* @param title 메일 제목 | ||
* @param pagePath ThymeLeaf(HTML)파일 경로 | ||
* @param content ThymeLeaf에 들어갈 변수 Map (Not Null!!!) | ||
* @return | ||
*/ | ||
public boolean sendMail(String toUser, String title, String pagePath, Map<String, Object> content) { | ||
try { | ||
parameterValidate(toUser, title, content); | ||
MimeMessage message = javaMailSender.createMimeMessage(); | ||
message.addRecipients(Message.RecipientType.TO, toUser); | ||
message.setSubject(title, UTF_8); | ||
message.setText(getContent(pagePath, content), UTF_8, "html"); | ||
javaMailSender.send(message); | ||
log.info("[SendMail Success] ToUser >>> {}, Title >>> {}, PagePath >>> {}", toUser, title, pagePath); | ||
return true; | ||
} catch (MessagingException e) { | ||
log.error("[SendMail Failed] Exception Reason >>> MessageException, ToUser >>> {}, Title >>> {}, PagePath >>> {}", toUser, title, pagePath); | ||
throw MailSendException.EXCEPTION; | ||
} catch (Exception e) { | ||
log.error("[SendMail Failed] Exception Reason >>> {}, ToUser >>> {}, Title >>> {}, PagePath >>> {}", e.getClass(), toUser, title, pagePath); | ||
throw MailSendException.EXCEPTION; | ||
} | ||
} | ||
|
||
|
||
//Valid, NotNull를 쓸까했는데... 안먹는걸로 기억이나서.. | ||
private void parameterValidate(String toUser, String title, Map<String, Object> content) { | ||
if (toUser == null || title == null || content == null || | ||
title.isEmpty() || toUser.isEmpty()) { | ||
throw IllegalArgumentException.EXCEPTION; | ||
} | ||
} | ||
|
||
// html파일에서 메일내용을 가져옴 | ||
private String getContent(String pagePath, Map<String, Object> content) { | ||
Context context = new Context(); | ||
content.forEach(context::setVariable); | ||
return templateEngine.process(pagePath, context); | ||
} | ||
|
||
} |
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