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

[BUILD SUCCESS] [BUILD FAIL] [BUILD FAIL] [BUILD FAIL] Feature 64 이메일 전송 api 구현 #65

Merged
merged 7 commits into from
Apr 5, 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
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ dependencies {

// jpa json 타입
implementation 'io.hypersistence:hypersistence-utils-hibernate-55:3.5.2'

//spring boot starter mail
implementation 'org.springframework.boot:spring-boot-starter-mail'
}

tasks.named('test') {
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/example/demo/config/MailConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.demo.config;

import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@Configuration
public class MailConfig {

@Value("${spring.mail.host}")
private String host;
@Value("${spring.mail.port}")
private int port;
@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
@Value("${spring.mail.properties.mail.smtp.auth}")
private String auth;
@Value("${spring.mail.properties.mail.smtp.starttls.enable}")
private String enable;

@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(username);
mailSender.setPassword(password);

Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", auth);
props.put("mail.smtp.starttls.enable", enable);
props.put("mail.debug", "true");

return mailSender;
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/example/demo/controller/MailController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.demo.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;

import com.example.demo.domain.auth.CafegoryTokenManager;
import com.example.demo.dto.EmailSendRequest;
import com.example.demo.dto.EmailTemplateResponse;
import com.example.demo.service.MailService;

import lombok.RequiredArgsConstructor;

@Controller
@RequiredArgsConstructor
public class MailController {

private final CafegoryTokenManager cafegoryTokenManager;
private final MailService mailService;

@PostMapping("/email")
public ResponseEntity<EmailTemplateResponse> sendEmail(
@RequestHeader("Authorization") String authorization, @RequestBody EmailSendRequest request) {
cafegoryTokenManager.getIdentityId(authorization);
EmailTemplateResponse response = mailService.sendEmail(request);
return ResponseEntity.ok(response);
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/example/demo/domain/EmailSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.demo.domain;

public interface EmailSender {

void sendSimpleMessage(String to, String subject, String text);
}
28 changes: 28 additions & 0 deletions src/main/java/com/example/demo/domain/EmailSenderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.demo.domain;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class EmailSenderImpl implements EmailSender {

@Autowired
private JavaMailSender mailSender;

@Value("${mail.from}")
private String mailFrom;

@Override
public void sendSimpleMessage(String to, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
//setFrom에 들어가는 이메일 주소는 실제 발송 주소와 관련이 없다.
message.setFrom(mailFrom);
message.setTo(to);
message.setSubject(subject);
message.setText(text);
mailSender.send(message);
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/example/demo/domain/EmailTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.demo.domain;

import lombok.Getter;

@Getter
public enum EmailTemplate {

STUDYONCE_LOCATION_CHANGED("[Cafegory] 카페 장소가 변경되었습니다.", "자세한 내용은 마이페이지를 확인 해 주세요.");

EmailTemplate(String subject, String content) {
this.subject = subject;
this.content = content;
}

private final String subject;
private final String content;

}
20 changes: 20 additions & 0 deletions src/main/java/com/example/demo/dto/EmailSendRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.demo.dto;

import java.util.List;

import lombok.Getter;

@Getter
public class EmailSendRequest {

private String messageType;
private List<Long> memberIds;

public EmailSendRequest() {
}

public EmailSendRequest(String messageType, List<Long> memberIds) {
this.messageType = messageType;
this.memberIds = memberIds;
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/example/demo/dto/EmailTemplateResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.demo.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class EmailTemplateResponse {

private final String title;
private final String content;
}
9 changes: 9 additions & 0 deletions src/main/java/com/example/demo/service/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.demo.service;

import com.example.demo.dto.EmailSendRequest;
import com.example.demo.dto.EmailTemplateResponse;

public interface MailService {

EmailTemplateResponse sendEmail(EmailSendRequest request);
}
38 changes: 38 additions & 0 deletions src/main/java/com/example/demo/service/MailServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.demo.service;

import static com.example.demo.exception.ExceptionType.*;

import org.springframework.stereotype.Service;

import com.example.demo.domain.EmailSender;
import com.example.demo.domain.EmailTemplate;
import com.example.demo.domain.MemberImpl;
import com.example.demo.dto.EmailSendRequest;
import com.example.demo.dto.EmailTemplateResponse;
import com.example.demo.exception.CafegoryException;
import com.example.demo.repository.MemberRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class MailServiceImpl implements MailService {

private final EmailSender emailSender;
private final MemberRepository memberRepository;

@Override
public EmailTemplateResponse sendEmail(EmailSendRequest request) {
EmailTemplate template = EmailTemplate.valueOf(request.getMessageType());
for (Long memberId : request.getMemberIds()) {
emailSender.sendSimpleMessage(findMemberById(memberId).getEmail(), template.getSubject(),
template.getContent());
}
return new EmailTemplateResponse(template.getSubject(), template.getContent());
}

private MemberImpl findMemberById(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new CafegoryException(MEMBER_NOT_FOUND));
}
}
17 changes: 16 additions & 1 deletion src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ spring:
properties:
hibernate:
format_sql: true #이쁘게 해줌
mail:
host: smtp.gmail.com
port: 587
username: [email protected]
password: test cafe gory pass

properties:
mail:
smtp:
auth: true
starttls:
enable: true

logging:
level:
org.hibernate.SQL: trace #콘솔에 남기는게 아니라 로그로 남음.
Expand All @@ -30,6 +43,8 @@ oauth:
auth: https://nid.naver.com
api: https://openapi.naver.com


jwt:
secret: 01234567890123456789012345678901234567890123456789

mail:
from: [email protected]
Loading