diff --git a/build.gradle b/build.gradle index 7de1d55..7768e69 100644 --- a/build.gradle +++ b/build.gradle @@ -26,6 +26,9 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.springframework.boot:spring-boot-starter-validation' + //이메일 전송 + implementation 'org.springframework.boot:spring-boot-starter-mail:2.7.1' + } diff --git a/src/main/java/com/example/firstproject/controller/EmailController.java b/src/main/java/com/example/firstproject/controller/EmailController.java new file mode 100644 index 0000000..50fb8cf --- /dev/null +++ b/src/main/java/com/example/firstproject/controller/EmailController.java @@ -0,0 +1,24 @@ +package com.example.firstproject.controller; + +import com.example.firstproject.service.impl.EmailServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +public class EmailController { + + // 회원가입 메일 서비스 + @Autowired + EmailServiceImpl EmailServiceImpl; + + @PostMapping("login/mailConfirm") + @ResponseBody + String mailConfirm(@RequestParam("email") String email) throws Exception { + + String code = EmailServiceImpl.sendSimpleMessage(email); + System.out.println("인증코드 : " + code); + return code; + } + +} diff --git a/src/main/java/com/example/firstproject/service/EmailService.java b/src/main/java/com/example/firstproject/service/EmailService.java new file mode 100644 index 0000000..7cf99a8 --- /dev/null +++ b/src/main/java/com/example/firstproject/service/EmailService.java @@ -0,0 +1,19 @@ +package com.example.firstproject.service; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import java.io.UnsupportedEncodingException; + +public interface EmailService { + + //메일 내용 작성 + MimeMessage createMessage(String to) throws MessagingException, UnsupportedEncodingException; + //MessagingException는 메세지 관련 예외처리를 위한 예외 클래스 + //UnsupportedEncodingException는 지원되지 않는 인코딩을 처리하기 위한 예외 클래스 + + // 랜덤 인증 코드 전송 + String createKey(); + + //메일 전송 + String sendSimpleMessage(String to) throws Exception; +} diff --git a/src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java b/src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java new file mode 100644 index 0000000..ecf9387 --- /dev/null +++ b/src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java @@ -0,0 +1,89 @@ +package com.example.firstproject.service.impl; + +import com.example.firstproject.service.EmailService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mail.MailException; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.stereotype.Service; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import java.io.UnsupportedEncodingException; +import java.util.Random; + +@Service //bean을 만들고, 객체를 생성 +public class EmailServiceImpl implements EmailService {//extends는 클래스 확장, implements는 인터페이스를 구현 + + @Autowired + JavaMailSender emailSender;//의존성 주입 + + private String ePw; //인증번호 + + @Override + public MimeMessage createMessage(String to) throws MessagingException, UnsupportedEncodingException { + + MimeMessage message = emailSender.createMimeMessage(); + + message.addRecipients(Message.RecipientType.TO, to); //보내는 대상 + message.setSubject("Froot 회원가입 이메일 인증"); + + String msg = ""; + msg += "