diff --git a/src/main/java/com/example/daemawiki/domain/mail/service/MailSend.java b/src/main/java/com/example/daemawiki/domain/mail/service/MailSend.java index 48b49c86..e4b6d598 100644 --- a/src/main/java/com/example/daemawiki/domain/mail/service/MailSend.java +++ b/src/main/java/com/example/daemawiki/domain/mail/service/MailSend.java @@ -3,6 +3,8 @@ import com.example.daemawiki.domain.mail.dto.AuthCodeRequest; import com.example.daemawiki.domain.mail.model.AuthCode; import com.example.daemawiki.domain.mail.repository.AuthCodeRepository; +import com.example.daemawiki.global.exception.MailSendFailedException; +import jakarta.mail.MessagingException; import jakarta.mail.internet.InternetAddress; import jakarta.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Value; @@ -12,6 +14,7 @@ import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; +import java.io.UnsupportedEncodingException; import java.util.Random; @Service @@ -30,32 +33,41 @@ public MailSend(AuthCodeRepository authCodeRepository, JavaMailSender javaMailSe private static final Random rand = new Random(); public Mono execute(AuthCodeRequest request) { - return sendMail(request.mail()) + String authCode = getRandomCode(); + String mail = request.mail(); + + return sendMail(mail, authCode) .doOnNext(mailSender::send) + .then(saveAuthCode(mail, authCode)) .then(); } - private Mono sendMail(String to) { - String authCode = getRandomCode(); - + private Mono sendMail(String to, String authCode) { return Mono.fromCallable(() -> { - MimeMessage message = mailSender.createMimeMessage(); - MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); + try { + MimeMessage message = mailSender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); + + helper.setTo(to); + helper.setSubject("DSM 메일 인증"); - helper.setTo(to); - helper.setSubject("DSM 메일 인증"); + String mail = getMailTemplate(authCode); - String mail = getMailTemplate(authCode); + helper.setText(mail); + helper.setFrom(new InternetAddress(admin, "DSM-MAIL-AUTH")); - helper.setText(mail); - helper.setFrom(new InternetAddress(admin, "DSM-MAIL-AUTH")); + return message; + } catch (MessagingException | UnsupportedEncodingException e) { + throw MailSendFailedException.EXCEPTION; + } + }).subscribeOn(Schedulers.boundedElastic()); + } - return message; - }).subscribeOn(Schedulers.boundedElastic()) - .flatMap(message -> codeRepository.save(AuthCode.builder() - .mail(to) - .code(authCode).build()) - .thenReturn(message)); + private Mono saveAuthCode(String to, String authCode) { + return codeRepository.save(AuthCode.builder() + .mail(to) + .code(authCode) + .build()); } private String getMailTemplate(String key) {