Skip to content

Commit 406cf04

Browse files
committed
refactor: gmail smtp retry 재시도 오류 수정
1 parent a8a0407 commit 406cf04

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

src/main/java/dynamicquad/agilehub/global/mail/service/EmailService.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
import dynamicquad.agilehub.global.exception.GeneralException;
44
import dynamicquad.agilehub.global.header.status.ErrorStatus;
55
import dynamicquad.agilehub.global.mail.model.EmailInfo;
6-
import dynamicquad.agilehub.issue.aspect.Retry;
76
import dynamicquad.agilehub.project.model.InviteEmailInfo;
87
import dynamicquad.agilehub.report.SummaryEmailInfo;
98
import jakarta.mail.MessagingException;
109
import jakarta.mail.internet.MimeMessage;
11-
import lombok.RequiredArgsConstructor;
12-
1310
import java.util.concurrent.CompletableFuture;
14-
15-
import org.springframework.mail.MailSendException;
11+
import lombok.RequiredArgsConstructor;
12+
import lombok.extern.slf4j.Slf4j;
1613
import org.springframework.mail.javamail.JavaMailSender;
1714
import org.springframework.mail.javamail.MimeMessageHelper;
15+
import org.springframework.retry.annotation.Backoff;
16+
import org.springframework.retry.annotation.Retryable;
1817
import org.springframework.scheduling.annotation.Async;
1918
import org.springframework.stereotype.Service;
2019
import org.springframework.util.StringUtils;
@@ -23,22 +22,21 @@
2322

2423
@Service
2524
@RequiredArgsConstructor
25+
@Slf4j
2626
public class EmailService {
2727

2828
private final JavaMailSender mailSender;
2929
private final SpringTemplateEngine templateEngine;
3030

3131
@Async("emailExecutor")
32-
@Retry(maxRetries = 3, // 최대 3번 시도
33-
retryFor = { MessagingException.class, MailSendException.class }, // 이메일 관련 예외만 재시도
34-
delay = 1000 // 1초 대기 후 재시도
35-
)
32+
@Retryable(retryFor = {GeneralException.class}, maxAttempts = 3, backoff = @Backoff(delay = 500))
3633
public CompletableFuture<Void> sendMail(EmailInfo emailInfo, String type) {
3734
return CompletableFuture.runAsync(() -> {
3835
try {
3936
MimeMessage message = createMimeMessage(emailInfo, type);
4037
mailSender.send(message);
41-
} catch (MessagingException me) {
38+
} catch (Exception e) {
39+
log.error("Gmail SMTP 이메일 발송 실패: {}", e.getMessage());
4240
throw new GeneralException(ErrorStatus.EMAIL_NOT_SENT);
4341
}
4442
});
@@ -57,16 +55,17 @@ private MimeMessage createMimeMessage(EmailInfo emailInfo, String type) throws M
5755

5856
private String getEmailContent(EmailInfo emailInfo, String type) {
5957
return StringUtils.hasText(type)
60-
? setContext(emailInfo, type)
61-
: emailInfo.getMessage();
58+
? setContext(emailInfo, type)
59+
: emailInfo.getMessage();
6260
}
6361

6462
private String setContext(EmailInfo emailInfo, String type) {
6563
Context context = new Context();
6664
if (type.equals("invite") && emailInfo instanceof InviteEmailInfo inviteEmailMessage) {
6765
context.setVariable("projectName", inviteEmailMessage.getProjectName());
6866
context.setVariable("inviteCode", inviteEmailMessage.getInviteCode());
69-
} else if (type.equals("reportSummary") && emailInfo instanceof SummaryEmailInfo summaryEmailMessage) {
67+
}
68+
else if (type.equals("reportSummary") && emailInfo instanceof SummaryEmailInfo summaryEmailMessage) {
7069
context.setVariable("projectName", summaryEmailMessage.getProjectName());
7170
context.setVariable("report", summaryEmailMessage.getReport());
7271
}

0 commit comments

Comments
 (0)