-
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
1 parent
5666ed2
commit 7f742e0
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/on/server/global/mail/MailController.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,23 @@ | ||
package com.on.server.global.mail; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1/mail") | ||
public class MailController { | ||
|
||
private final MailService mailService; | ||
|
||
@GetMapping("/test") | ||
public Integer sendAuthNumMail( | ||
@RequestHeader String targetMailAddr | ||
) { | ||
return mailService.sendAuthNumMail(targetMailAddr); | ||
} | ||
|
||
} |
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,57 @@ | ||
package com.on.server.global.mail; | ||
|
||
import com.on.server.global.common.ResponseCode; | ||
import com.on.server.global.common.exceptions.InternalServerException; | ||
import com.on.server.global.util.StaticValue; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class MailService { | ||
|
||
private final JavaMailSender javaMailSender; | ||
|
||
@Value("${spring.mail.sender}") | ||
private static String senderEmail; | ||
|
||
public Integer sendAuthNumMail(String targetMailAddr) { | ||
Integer number = createNumber(); | ||
|
||
MimeMessage message = CreateMail( | ||
targetMailAddr, | ||
StaticValue.AUTH_NUMBER_MAIL_SUBJECT, | ||
StaticValue.AUTH_NUMBER_MAIL_BODY + "<h1>" + number + "</h1>" | ||
); | ||
javaMailSender.send(message); | ||
|
||
return number; | ||
} | ||
|
||
private MimeMessage CreateMail(String targetMailAddr, String mailSubject, String mailBody) { | ||
MimeMessage message = javaMailSender.createMimeMessage(); | ||
|
||
try { | ||
message.setFrom(senderEmail); | ||
message.setRecipients(MimeMessage.RecipientType.TO, targetMailAddr); | ||
message.setSubject(mailSubject); | ||
message.setText(mailBody,"UTF-8", "html"); | ||
} catch (MessagingException e) { | ||
throw new InternalServerException(ResponseCode.INTERNAL_SERVER, "메일 내용 생성에 실패했습니다."); | ||
} | ||
|
||
return message; | ||
} | ||
|
||
private Integer createNumber() { | ||
return (int)(Math.random() * (90000)) + 100000; // (int) Math.random() * (최댓값-최소값+1) + 최소값 | ||
} | ||
|
||
} |
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