This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
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.
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/main/java/com/example/daemawiki/domain/mail/service/MailSend.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,73 @@ | ||
package com.example.daemawiki.domain.mail.service; | ||
|
||
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.domain.mail.repository.AuthMailRepository; | ||
import jakarta.mail.internet.InternetAddress; | ||
import jakarta.mail.internet.MimeMessage; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.Random; | ||
|
||
@Service | ||
public class MailSend { | ||
private final AuthCodeRepository codeRepository; | ||
private final JavaMailSender mailSender; | ||
|
||
public MailSend(AuthCodeRepository authCodeRepository, JavaMailSender javaMailSender) { | ||
this.codeRepository = authCodeRepository; | ||
this.mailSender = javaMailSender; | ||
} | ||
|
||
@Value("${admin.mail}") | ||
private static String admin; | ||
|
||
private static final Random rand = new Random(); | ||
|
||
public Mono<Void> execute(AuthCodeRequest request) { | ||
return sendMail(request.mail()) | ||
.doOnNext(mailSender::send) | ||
.then(); | ||
} | ||
|
||
private Mono<MimeMessage> sendMail(String to) { | ||
String authCode = getRandomCode(); | ||
|
||
return Mono.fromCallable(() -> { | ||
MimeMessage message = mailSender.createMimeMessage(); | ||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); | ||
|
||
helper.setTo(to); | ||
helper.setSubject("DSM 메일 인증"); | ||
|
||
String mail = getMailTemplate(authCode); | ||
|
||
helper.setText(mail); | ||
helper.setFrom(new InternetAddress(admin, "DSM-MAIL-AUTH")); | ||
|
||
return message; | ||
}).flatMap(message -> codeRepository.save(AuthCode.builder() | ||
.mail(to) | ||
.code(authCode).build()) | ||
.thenReturn(message)); | ||
} | ||
|
||
private String getMailTemplate(String key) { | ||
return "<div style='margin: 10px; background-color: #f5f5f5; padding: 20px; border-radius: 10px;'>" | ||
+ "<p style='font-size: 16px; color: #333;'><b><span style='color: #007bff;'>D</span><span style='color: #ffcc00;'>S</span><span style='color: #ff0000;'>M</span></b> 이메일 인증 코드 :</p>" | ||
+ "<p style='font-size: 24px; font-weight: bold; color: #007bff; letter-spacing: 3px;'>" + key + "</p>" | ||
+ "<p style='font-size: 14;font-style: italic; color: #999;'>인증 코드는 3시간 동안 유효합니다.</p>" | ||
+ "</div>"; | ||
} | ||
|
||
private String getRandomCode() { | ||
int num = rand.nextInt(900000) + 100000; | ||
return String.valueOf(num); | ||
} | ||
|
||
} |