66import jakarta .mail .MessagingException ;
77import jakarta .mail .internet .MimeMessage ;
88import lombok .RequiredArgsConstructor ;
9+ import org .slf4j .Logger ;
10+ import org .slf4j .LoggerFactory ;
911import org .springframework .beans .factory .annotation .Value ;
1012import org .springframework .mail .javamail .JavaMailSender ;
1113import org .springframework .mail .javamail .MimeMessageHelper ;
14+ import org .springframework .scheduling .annotation .Async ;
1215import org .springframework .stereotype .Service ;
1316import org .thymeleaf .TemplateEngine ;
1417import org .thymeleaf .context .Context ;
@@ -19,62 +22,92 @@ public class EmailService {
1922
2023 private final TemplateEngine templateEngine ;
2124 private final JavaMailSender javaMailSender ;
25+ private static final Logger log = LoggerFactory .getLogger (EmailService .class );
2226
2327 @ Value ("${spring.mail.username}" )
2428 private String from ;
2529
26- public void sendEmail (String to , String name , MailType mailType ) throws MessagingException {
27- if (mailType == null ){
28- throw new CustomException (ErrorCode .MAIL_TYPE_NOT_VALID );
30+ @ Async
31+ public void sendEmail (String to , String name , MailType mailType ) {
32+ try {
33+ if (mailType == null ) {
34+ throw new CustomException (ErrorCode .MAIL_TYPE_NOT_VALID );
35+ }
36+
37+ // 메일 제목 및 템플릿 설정
38+ String subject = getSubject (mailType );
39+ String emailContent = getEmailContent (mailType );
40+ String buttonText = getButtonText (mailType );
41+ String buttonUrl = "https://dmu-dasom.or.kr/recruit/result" ;
42+
43+ // HTML 템플릿에 전달할 데이터 설정
44+ Context context = new Context ();
45+ context .setVariable ("name" , name ); // 지원자 이름 전달
46+ context .setVariable ("emailContent" , emailContent ); // 이메일 내용 전달
47+ context .setVariable ("buttonUrl" , buttonUrl ); // 버튼 링크 전달
48+ context .setVariable ("buttonText" , buttonText );
49+
50+ // HTML 템플릿 처리
51+ String htmlBody = templateEngine .process ("email-template" , context );
52+
53+ // 이메일 생성 및 전송
54+ MimeMessage message = javaMailSender .createMimeMessage ();
55+ MimeMessageHelper helper = new MimeMessageHelper (message , true , "UTF-8" );
56+
57+ helper .setTo (to );
58+ helper .setSubject (subject );
59+ helper .setText (htmlBody , true );
60+ helper .
setFrom ((
from !=
null && !
from .
isEmpty ()) ?
from :
"[email protected] " );
61+
62+ // Content-Type을 명시적으로 설정
63+ message .setContent (htmlBody , "text/html; charset=utf-8" );
64+
65+ javaMailSender .send (message );
66+ log .info ("Email sent successfull {}" , to );
67+ } catch (MessagingException e ) {
68+ log .error ("Failed to send email to {}: {}" , to , e .getMessage ());
69+ } catch (CustomException e ) {
70+ log .error ("Email sending error for {}: {}" , to , e .getMessage ());
2971 }
72+ }
3073
31- // 메일 제목 및 템플릿 설정
32- String subject ;
33- String emailContent ;
34- String buttonUrl = "https://dmu-dasom.or.kr/recruit/result" ;
35- String buttonText ;
74+ // 메일 유형에 맞는 제목 반환
75+ private String getSubject (MailType mailType ){
76+ switch (mailType ){
77+ case DOCUMENT_RESULT :
78+ return "동양미래대학교 컴퓨터소프트웨어공학과 전공 동아리 DASOM 서류 결과 안내" ;
79+ case FINAL_RESULT :
80+ return "동양미래대학교 컴퓨터소프트웨어공학과 전공 동아리 DASOM 최종 면접 결과 안내" ;
81+ default :
82+ throw new CustomException (ErrorCode .MAIL_TYPE_NOT_VALID );
83+ }
84+ }
3685
86+ // 메일 유형에 맞는 본문 반환
87+ private String getEmailContent (MailType mailType ){
3788 switch (mailType ) {
38- case DOCUMENT_RESULT -> {
39- subject = "동양미래대학교 컴퓨터소프트웨어공학과 전공 동아리 DASOM 서류 결과 안내" ;
40- emailContent = "먼저 다솜 34기에 많은 관심을 두고 지원해 주셔서 감사드리며,<br>" +
89+ case DOCUMENT_RESULT :
90+ return "먼저 다솜 34기에 많은 관심을 두고 지원해 주셔서 감사드리며,<br>" +
4191 "내부 서류 평가 결과 및 추후 일정에 관해 안내해드리고자 이메일을 발송하게 되었습니다.<br>" +
4292 "서류 전형 결과는 아래 버튼 혹은 홈페이지를 통해 확인이 가능합니다." ;
43- buttonText = "서류 결과 확인하기" ;
44- }
45- case FINAL_RESULT -> {
46- subject = "동양미래대학교 컴퓨터소프트웨어공학과 전공 동아리 DASOM 최종 면접 결과 안내" ;
47- emailContent = "먼저 다솜 34기에 많은 관심을 두고 지원해 주셔서 감사드리며,<br>" +
93+ case FINAL_RESULT :
94+ return "먼저 다솜 34기에 많은 관심을 두고 지원해 주셔서 감사드리며,<br>" +
4895 "최종 면접 결과 및 추후 일정에 관해 안내해드리고자 이메일을 발송하게 되었습니다.<br>" +
4996 "최종 면접 결과는 아래 버튼 혹은 홈페이지를 통해 확인이 가능합니다." ;
50- buttonText = "최종 결과 확인하기" ;
51- }
52- default -> throw new IllegalStateException ("Unexpected value: " + mailType );
97+ default :
98+ throw new CustomException (ErrorCode .MAIL_TYPE_NOT_VALID );
5399 }
54-
55- // HTML 템플릿에 전달할 데이터 설정
56- Context context = new Context ();
57- context .setVariable ("name" , name ); // 지원자 이름 전달
58- context .setVariable ("emailContent" , emailContent ); // 이메일 내용 전달
59- context .setVariable ("buttonUrl" , buttonUrl ); // 버튼 링크 전달
60- context .setVariable ("buttonText" , buttonText );
61-
62- // HTML 템플릿 처리
63- String htmlBody = templateEngine .process ("email-template" , context );
64-
65- // 이메일 생성 및 전송
66- MimeMessage message = javaMailSender .createMimeMessage ();
67- MimeMessageHelper helper = new MimeMessageHelper (message , true , "UTF-8" );
68-
69- helper .setTo (to );
70- helper .setSubject (subject );
71- helper .setText (htmlBody , true );
72- helper .
setFrom ((
from !=
null && !
from .
isEmpty ()) ?
from :
"[email protected] " );
73-
74- // Content-Type을 명시적으로 설정
75- message .setContent (htmlBody , "text/html; charset=utf-8" );
76-
77- javaMailSender .send (message );
78100 }
79101
102+ // 메일 유형에 맞는 버튼 텍스트 반환
103+ private String getButtonText (MailType mailType ) {
104+ switch (mailType ) {
105+ case DOCUMENT_RESULT :
106+ return "서류 결과 확인하기" ;
107+ case FINAL_RESULT :
108+ return "최종 결과 확인하기" ;
109+ default :
110+ throw new CustomException (ErrorCode .MAIL_TYPE_NOT_VALID );
111+ }
112+ }
80113}
0 commit comments