|
| 1 | +package com.back.domain.member.member.email; |
| 2 | + |
| 3 | +import com.back.global.exception.ServiceException; |
| 4 | +import jakarta.mail.MessagingException; |
| 5 | +import jakarta.mail.internet.MimeMessage; |
| 6 | +import lombok.RequiredArgsConstructor; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.springframework.mail.javamail.JavaMailSender; |
| 9 | +import org.springframework.mail.javamail.MimeMessageHelper; |
| 10 | +import org.springframework.stereotype.Service; |
| 11 | + |
| 12 | +import java.io.UnsupportedEncodingException; |
| 13 | + |
| 14 | +@Slf4j |
| 15 | +@Service |
| 16 | +@RequiredArgsConstructor |
| 17 | +public class EmailService { |
| 18 | + private final JavaMailSender mailSender; |
| 19 | + |
| 20 | + /** |
| 21 | + * 간단한 텍스트 이메일 발송 |
| 22 | + */ |
| 23 | + public void sendSimpleEmail(String to, String subject, String text) { |
| 24 | + try { |
| 25 | + MimeMessage message = mailSender.createMimeMessage(); |
| 26 | + MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); |
| 27 | + |
| 28 | + helper. setFrom( "[email protected]", "JobMate"); |
| 29 | + helper.setTo(to); |
| 30 | + helper.setSubject(subject); |
| 31 | + helper.setText(text, false); // false = plain text |
| 32 | + |
| 33 | + mailSender.send(message); |
| 34 | + log.info("텍스트 이메일 발송 성공: {}", to); |
| 35 | + } catch (MessagingException | UnsupportedEncodingException e) { |
| 36 | + log.error("텍스트 이메일 발송 실패: {}", to, e); |
| 37 | + throw new ServiceException("500-1", "텍스트 이메일 발송에 실패했습니다."); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * HTML 이메일 발송 |
| 43 | + */ |
| 44 | + public void sendHtmlEmail(String to, String subject, String htmlContent) { |
| 45 | + try { |
| 46 | + MimeMessage message = mailSender.createMimeMessage(); |
| 47 | + MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); |
| 48 | + |
| 49 | + helper. setFrom( "[email protected]", "JobMate"); |
| 50 | + helper.setTo(to); |
| 51 | + helper.setSubject(subject); |
| 52 | + helper.setText(htmlContent, true); // true = HTML |
| 53 | + |
| 54 | + mailSender.send(message); |
| 55 | + log.info("HTML 이메일 발송 성공: {}", to); |
| 56 | + } catch (MessagingException | UnsupportedEncodingException e) { |
| 57 | + log.error("HTML 이메일 발송 실패: {}", to, e); |
| 58 | + throw new ServiceException("500-2", "HTML 이메일 발송에 실패했습니다."); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * 인증번호 이메일 발송 (멘토 회원가입용) |
| 64 | + */ |
| 65 | + public void sendVerificationCode(String to, String verificationCode) { |
| 66 | + String subject = "[FiveLogic] 멘토 회원가입 인증번호"; |
| 67 | + String htmlContent = buildVerificationEmailHtml(verificationCode); |
| 68 | + sendHtmlEmail(to, subject, htmlContent); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * 인증번호 이메일 HTML 템플릿 |
| 73 | + */ |
| 74 | + private String buildVerificationEmailHtml(String verificationCode) { |
| 75 | + return """ |
| 76 | + <!DOCTYPE html> |
| 77 | + <html> |
| 78 | + <head> |
| 79 | + <meta charset="UTF-8"> |
| 80 | + <style> |
| 81 | + body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; } |
| 82 | + .container { max-width: 600px; margin: 0 auto; padding: 20px; } |
| 83 | + .header { background-color: #4CAF50; color: white; padding: 20px; text-align: center; } |
| 84 | + .content { padding: 30px; background-color: #f9f9f9; } |
| 85 | + .code-box { background-color: #fff; border: 2px solid #4CAF50; padding: 20px; text-align: center; margin: 20px 0; } |
| 86 | + .code { font-size: 32px; font-weight: bold; color: #4CAF50; letter-spacing: 5px; } |
| 87 | + .footer { text-align: center; padding: 20px; color: #666; font-size: 12px; } |
| 88 | + </style> |
| 89 | + </head> |
| 90 | + <body> |
| 91 | + <div class="container"> |
| 92 | + <div class="header"> |
| 93 | + <h1>JobMate 멘토 회원가입</h1> |
| 94 | + </div> |
| 95 | + <div class="content"> |
| 96 | + <h2>인증번호 확인</h2> |
| 97 | + <p>안녕하세요. FiveLogic입니다.</p> |
| 98 | + <p>멘토 회원가입을 위한 인증번호를 안내드립니다.</p> |
| 99 | + <p>아래 인증번호를 입력하여 회원가입을 완료해주세요.</p> |
| 100 | +
|
| 101 | + <div class="code-box"> |
| 102 | + <div class="code">%s</div> |
| 103 | + </div> |
| 104 | +
|
| 105 | + <p><strong>※ 인증번호는 5분간 유효합니다.</strong></p> |
| 106 | + <p>본인이 요청하지 않은 경우, 이 메일을 무시하셔도 됩니다.</p> |
| 107 | + </div> |
| 108 | + <div class="footer"> |
| 109 | + <p>© 2025 FiveLogic. All rights reserved.</p> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + </body> |
| 113 | + </html> |
| 114 | + """.formatted(verificationCode); |
| 115 | + } |
| 116 | +} |
0 commit comments