-
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.
Merge pull request #68 from Capstone-Richam/feat/mailtest
[feat] 메일 전송 api
- Loading branch information
Showing
7 changed files
with
138 additions
and
44 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
src/main/java/com/Nunbody/domain/Mail/dto/resquest/EmailReqDto.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
10 changes: 5 additions & 5 deletions
10
src/main/java/com/Nunbody/domain/Mail/service/EmailService.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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
package com.Nunbody.domain.Mail.service; | ||
|
||
import com.Nunbody.domain.Mail.dto.resquest.EmailReqDto; | ||
import com.Nunbody.external.GmailClient; | ||
import com.Nunbody.external.MailClient; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class EmailService { | ||
private final GmailClient gmailClient; | ||
private final MailClient mailClient; | ||
|
||
public void sendMail(Long memberId, EmailReqDto emailReqDto) { | ||
mailClient.sendMail(memberId, emailReqDto); | ||
|
||
public void sendMail(EmailReqDto emailReqDto) { | ||
MimeMessage message = gmailClient.CreateMail(emailReqDto); | ||
gmailClient.send(message); | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/Nunbody/domain/member/service/MemberReader.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,17 @@ | ||
package com.Nunbody.domain.member.service; | ||
|
||
import com.Nunbody.domain.member.domain.Member; | ||
import com.Nunbody.domain.member.repository.MemberRepository; | ||
import com.Nunbody.global.error.ErrorCode; | ||
import com.Nunbody.global.error.exception.EntityNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MemberReader { | ||
private final MemberRepository memberRepository; | ||
public Member getMemberById(Long memberId){ | ||
return memberRepository.findById(memberId).orElseThrow(() -> new EntityNotFoundException(ErrorCode.ENTITY_NOT_FOUND)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,77 @@ | ||
package com.Nunbody.external; | ||
|
||
import com.Nunbody.domain.Mail.domain.PlatformType; | ||
import com.Nunbody.domain.Mail.dto.resquest.EmailReqDto; | ||
import com.Nunbody.domain.member.domain.Member; | ||
import com.Nunbody.domain.member.service.MemberReader; | ||
import com.Nunbody.global.common.EncoderDecoder; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.JavaMailSenderImpl; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Properties; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MailClient { | ||
private final MemberReader memberReader; | ||
|
||
public void sendMail(Long memberId, EmailReqDto emailReqDto) { | ||
Member member = memberReader.getMemberById(memberId); | ||
PlatformType platformType = PlatformType.getEnumPlatformTypeFromStringPlatformType(emailReqDto.platform()); | ||
JavaMailSender mailSender = createMailSender(member, platformType); | ||
String senderMail = getSenderMail(member, platformType); | ||
|
||
MimeMessage message = mailSender.createMimeMessage(); | ||
try { | ||
message.setFrom(senderMail); | ||
message.setRecipients(MimeMessage.RecipientType.TO, emailReqDto.mail()); | ||
message.setSubject(emailReqDto.header()); | ||
message.setText(emailReqDto.body(), "UTF-8", "html"); | ||
|
||
mailSender.send(message); | ||
} catch (MessagingException e) { | ||
throw new RuntimeException("Failed to create or send email message", e); | ||
} | ||
} | ||
|
||
private JavaMailSender createMailSender(Member member, PlatformType platformType) { | ||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); | ||
switch (platformType) { | ||
case NAVER: | ||
mailSender.setHost("smtp.naver.com"); | ||
mailSender.setPort(587); | ||
mailSender.setUsername(member.getNaverId()); | ||
mailSender.setPassword(EncoderDecoder.decodeFromBase64(member.getNaverPassword())); | ||
break; | ||
case GOOGLE: | ||
mailSender.setHost("smtp.gmail.com"); | ||
mailSender.setPort(587); | ||
mailSender.setUsername(member.getGmailId()); | ||
mailSender.setPassword(EncoderDecoder.decodeFromBase64(member.getGmailPassword())); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Unsupported platform type: " + platformType); | ||
} | ||
|
||
Properties props = mailSender.getJavaMailProperties(); | ||
props.put("mail.transport.protocol", "smtp"); | ||
props.put("mail.smtp.auth", "true"); | ||
props.put("mail.smtp.starttls.enable", "true"); | ||
props.put("mail.debug", "true"); | ||
props.put("mail.smtp.timeout", "5000"); | ||
|
||
return mailSender; | ||
} | ||
|
||
private String getSenderMail(Member member, PlatformType platformType) { | ||
return switch (platformType) { | ||
case NAVER -> member.getNaverId(); | ||
case GOOGLE -> member.getGmailId(); | ||
default -> throw new IllegalArgumentException("Unsupported platform type: " + platformType); | ||
}; | ||
} | ||
} |
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,36 @@ | ||
package com.Nunbody.global.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class MailConfig { | ||
|
||
@Value("${spring.mail.gmail.host}") | ||
private String gmailHost; | ||
|
||
@Value("${spring.mail.gmail.port}") | ||
private int gmailPort; | ||
|
||
@Value("${spring.mail.naver.host}") | ||
private String naverHost; | ||
|
||
@Value("${spring.mail.naver.port}") | ||
private int naverPort; | ||
|
||
public String getGmailHost() { | ||
return gmailHost; | ||
} | ||
|
||
public int getGmailPort() { | ||
return gmailPort; | ||
} | ||
|
||
public String getNaverHost() { | ||
return naverHost; | ||
} | ||
|
||
public int getNaverPort() { | ||
return naverPort; | ||
} | ||
} |