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 += "
"; + msg += "

안녕하세요

"; + msg += "

회원가입 인증 코드 입니다.

"; + msg += "
"; + msg += ePw + "

"; + msg += "
"; + + // 내용, charset 타입, subtype / StringBuffer 타입 인자로 못들어간다. + message.setText(msg, "utf-8", "html");// 내용, charset 타입, subtype + // 보내는 사람의 이메일 주소, 보내는 사람 이름 + message.setFrom(new InternetAddress("yongwoo1207@naver.com", "Froot admin"));// 보내는 사람 + + return message; + } + + // 랜덤 인증 코드 만들기 + @Override + public String createKey() { + Random random = new Random(); + int num = random.nextInt(8); + String key = String.valueOf(num); + return key; + } + + // 메일 발송 + // sendSimpleMessage 의 매개변수로 들어온 to 는 곧 이메일 주소가 되고, + // MimeMessage 객체 안에 내가 전송할 메일의 내용을 담는다. + // 그리고 bean 으로 등록해둔 javaMail 객체를 사용해서 이메일 send!! + @Override + public String sendSimpleMessage(String to) throws Exception { + ePw = createKey(); + MimeMessage message = createMessage(to); //메일 발송 + try { //여긴 예외`처리 그냥 복사해옴. + emailSender.send(message); + } catch (MailException es){ + es.printStackTrace(); + throw new IllegalAccessException(); + } + + return ePw; // 메일로 보냈던 인증 코드를 서버로 반환 + } +} + + + +/* @Override + public void sendEmail(String to, String subject, String message){ + SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); + simpleMailMessage.setFrom("yongwoo1207@naver.com"); + simpleMailMessage.setTo(to); + simpleMailMessage.setSubject(subject); + simpleMailMessage.setText(message); + + this.mailSender.send(simpleMailMessage); + }*/ + + diff --git a/src/main/java/config/EmailConfig.java b/src/main/java/config/EmailConfig.java new file mode 100644 index 0000000..e188883 --- /dev/null +++ b/src/main/java/config/EmailConfig.java @@ -0,0 +1,24 @@ +package config; + + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.JavaMailSenderImpl; +import resource.EmailResource; + +import java.util.Properties; + +@Configuration +public class EmailConfig { + @Bean + public JavaMailSender javaMailService() { + JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); + javaMailSender.setHost(EmailResource.getMailserver()); // 메일 도메인 서버 주소 + javaMailSender.setUsername(EmailResource.getUsername()); // 메일 유저 이름 + javaMailSender.setPassword(EmailResource.getUserpwd()); // 메일 패스워드 + javaMailSender.setPort(465); // 메일 인증서버 포트 + + return javaMailSender; + } +} \ No newline at end of file diff --git a/src/main/java/resource/EmailResource.java b/src/main/java/resource/EmailResource.java new file mode 100644 index 0000000..104cc9d --- /dev/null +++ b/src/main/java/resource/EmailResource.java @@ -0,0 +1,15 @@ +package resource; + +public class EmailResource { + public static String getMailserver() { + return "smtp.naver.com"; + } + + public static String getUsername() { + return "이메일 입력하기"; + } + public static String getUserpwd() { + return "비밀번호 입력하기"; + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 59cfca7..50a9865 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -31,3 +31,10 @@ spring.datasource.data=classpath:/data.sql spring.datasource.initialization-mode=never spring.jpa.hibernate.ddl-auto=update + +mail.smtp.socketFactory.port=465 +mail.smtp.auth=SSL +mail.smtp.starttls.enable=true +mail.smtp.starttls.required=true +mail.smtp.socketFactory.fallback=true +mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory \ No newline at end of file