diff --git a/server_9dokme/build.gradle b/server_9dokme/build.gradle index 072a73ea..9f5ee527 100644 --- a/server_9dokme/build.gradle +++ b/server_9dokme/build.gradle @@ -59,6 +59,9 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' //jwt implementation 'io.jsonwebtoken:jjwt:0.9.1' + //구글 이메일 + implementation 'org.springframework.boot:spring-boot-starter-mail' + } diff --git a/server_9dokme/src/main/java/com/example/server_9dokme/member/controller/EmailController.java b/server_9dokme/src/main/java/com/example/server_9dokme/member/controller/EmailController.java new file mode 100644 index 00000000..15411bcd --- /dev/null +++ b/server_9dokme/src/main/java/com/example/server_9dokme/member/controller/EmailController.java @@ -0,0 +1,25 @@ +package com.example.server_9dokme.member.controller; + +import com.example.server_9dokme.member.dto.request.MailDto; +import com.example.server_9dokme.member.service.EmailService; +import jakarta.validation.Valid; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@Slf4j +@RequestMapping("/api") +public class EmailController { + @Autowired + private EmailService emailService; + @PostMapping("/mail") + public String execMail(@RequestBody @Valid MailDto dto) { + + emailService.sendSimpleMessage(dto); + return "전송 완료"; + } +} diff --git a/server_9dokme/src/main/java/com/example/server_9dokme/member/dto/request/MailDto.java b/server_9dokme/src/main/java/com/example/server_9dokme/member/dto/request/MailDto.java new file mode 100644 index 00000000..d5fe39d1 --- /dev/null +++ b/server_9dokme/src/main/java/com/example/server_9dokme/member/dto/request/MailDto.java @@ -0,0 +1,15 @@ +package com.example.server_9dokme.member.dto.request; +import lombok.Data; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Data +@NoArgsConstructor +@Getter +@Setter +public class MailDto { + private String emailAddr; // 수신 이메일 + private String emailTitle; // 메일 제목 + private String emailContent;// 메일 내용 +} \ No newline at end of file diff --git a/server_9dokme/src/main/java/com/example/server_9dokme/member/service/EmailService.java b/server_9dokme/src/main/java/com/example/server_9dokme/member/service/EmailService.java new file mode 100644 index 00000000..11fa064b --- /dev/null +++ b/server_9dokme/src/main/java/com/example/server_9dokme/member/service/EmailService.java @@ -0,0 +1,29 @@ +package com.example.server_9dokme.member.service; + +import com.example.server_9dokme.member.dto.request.MailDto; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +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.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class EmailService { + + @Value("${google.email}") + private String FROM_ADDRESS; + @Autowired + private JavaMailSender emailSender; + public void sendSimpleMessage(MailDto mailDto) { + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom(FROM_ADDRESS); + message.setTo(mailDto.getEmailAddr()); + message.setSubject(mailDto.getEmailTitle()); + message.setText(mailDto.getEmailContent()); + emailSender.send(message); + } +}