Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 메일 전송 api #68

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.Nunbody.domain.Mail.dto.resquest;

public record EmailReqDto(
String platform,
String mail,
String header,
String body
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/Nunbody/domain/Mail/service/EmailService.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public ResponseEntity<SuccessResponse<?>> getHeader(@MemberId Long memberId, @Re
return SuccessResponse.ok(mailListResponseDtoList);
}
@PostMapping("/send")
public ResponseEntity<SuccessResponse<?>> mailSend(@RequestBody EmailReqDto emailReqDto) {
emailService.sendMail(emailReqDto);
public ResponseEntity<SuccessResponse<?>> mailSend(@MemberId Long memberId, @RequestBody EmailReqDto emailReqDto) {
emailService.sendMail(memberId, emailReqDto);
return SuccessResponse.ok(null);
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/Nunbody/domain/member/service/MemberReader.java
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));
}
}
37 changes: 0 additions & 37 deletions src/main/java/com/Nunbody/external/GmailClient.java

This file was deleted.

77 changes: 77 additions & 0 deletions src/main/java/com/Nunbody/external/MailClient.java
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);
};
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/Nunbody/global/config/MailConfig.java
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;
}
}
Loading