From 5677ca4fdf11c9bcbc93d28ceb50528aa565fe58 Mon Sep 17 00:00:00 2001 From: yim-yongwoo Date: Mon, 26 Jun 2023 16:14:43 +0900 Subject: [PATCH 1/7] =?UTF-8?q?=EC=9D=B4=EB=A9=94=EC=9D=BC=EC=BB=A8?= =?UTF-8?q?=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/firstproject/controller/EmailAuthController.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/com/example/firstproject/controller/EmailAuthController.java diff --git a/src/main/java/com/example/firstproject/controller/EmailAuthController.java b/src/main/java/com/example/firstproject/controller/EmailAuthController.java new file mode 100644 index 0000000..84faddd --- /dev/null +++ b/src/main/java/com/example/firstproject/controller/EmailAuthController.java @@ -0,0 +1,4 @@ +package com.example.firstproject.controller; + +public class EmailAuthController { +} From 9a5d4476d6acea41054e529a08f13c834ba2722d Mon Sep 17 00:00:00 2001 From: yim-yongwoo Date: Mon, 26 Jun 2023 16:28:47 +0900 Subject: [PATCH 2/7] hello --- .../com/example/firstproject/controller/EmailAuthController.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/example/firstproject/controller/EmailAuthController.java b/src/main/java/com/example/firstproject/controller/EmailAuthController.java index 84faddd..567763b 100644 --- a/src/main/java/com/example/firstproject/controller/EmailAuthController.java +++ b/src/main/java/com/example/firstproject/controller/EmailAuthController.java @@ -1,4 +1,5 @@ package com.example.firstproject.controller; public class EmailAuthController { + //hello } From f9cad050a67a07f22b9ebc6fcf45cb986096ba24 Mon Sep 17 00:00:00 2001 From: yim-yongwoo Date: Fri, 7 Jul 2023 15:05:48 +0900 Subject: [PATCH 3/7] =?UTF-8?q?=EC=9D=B4=EB=A9=94=EC=9D=BC=EC=A0=84?= =?UTF-8?q?=EC=86=A1=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 + .../controller/EmailAuthController.java | 5 -- .../controller/EmailController.java | 24 +++++ .../firstproject/service/EmailService.java | 19 ++++ .../service/impl/EmailServiceImpl.java | 89 +++++++++++++++++++ src/main/java/config/EmailConfig.java | 24 +++++ src/main/java/resource/EmailResource.java | 15 ++++ src/main/resources/application.properties | 7 ++ 8 files changed, 181 insertions(+), 5 deletions(-) delete mode 100644 src/main/java/com/example/firstproject/controller/EmailAuthController.java create mode 100644 src/main/java/com/example/firstproject/controller/EmailController.java create mode 100644 src/main/java/com/example/firstproject/service/EmailService.java create mode 100644 src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java create mode 100644 src/main/java/config/EmailConfig.java create mode 100644 src/main/java/resource/EmailResource.java 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/EmailAuthController.java b/src/main/java/com/example/firstproject/controller/EmailAuthController.java deleted file mode 100644 index 567763b..0000000 --- a/src/main/java/com/example/firstproject/controller/EmailAuthController.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.example.firstproject.controller; - -public class EmailAuthController { - //hello -} 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..0f097bf --- /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 "yongwoo1207@naver.com"; + } + public static String getUserpwd() { + return "gnem36zjvl"; + } + +} 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 From 977308587e65d4639fe0c8726d848b0b12d0f7db Mon Sep 17 00:00:00 2001 From: yim-yongwoo Date: Sat, 8 Jul 2023 18:18:24 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=EB=8B=A4=EC=8B=9C=20=EC=98=AC=EB=A6=AC?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/firstproject}/config/EmailConfig.java | 9 ++++----- .../example/firstproject/controller/EmailController.java | 2 ++ .../example/firstproject}/resource/EmailResource.java | 2 +- .../firstproject/service/impl/EmailServiceImpl.java | 6 ++---- src/main/resources/application.properties | 2 +- 5 files changed, 10 insertions(+), 11 deletions(-) rename src/main/java/{ => com/example/firstproject}/config/EmailConfig.java (81%) rename src/main/java/{ => com/example/firstproject}/resource/EmailResource.java (86%) diff --git a/src/main/java/config/EmailConfig.java b/src/main/java/com/example/firstproject/config/EmailConfig.java similarity index 81% rename from src/main/java/config/EmailConfig.java rename to src/main/java/com/example/firstproject/config/EmailConfig.java index e188883..98767f6 100644 --- a/src/main/java/config/EmailConfig.java +++ b/src/main/java/com/example/firstproject/config/EmailConfig.java @@ -1,13 +1,11 @@ -package config; +package com.example.firstproject.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; +import com.example.firstproject.resource.EmailResource; @Configuration public class EmailConfig { @@ -17,8 +15,9 @@ public JavaMailSender javaMailService() { javaMailSender.setHost(EmailResource.getMailserver()); // 메일 도메인 서버 주소 javaMailSender.setUsername(EmailResource.getUsername()); // 메일 유저 이름 javaMailSender.setPassword(EmailResource.getUserpwd()); // 메일 패스워드 - javaMailSender.setPort(465); // 메일 인증서버 포트 + javaMailSender.setPort(587); // 메일 인증서버 포트 return javaMailSender; } + } \ No newline at end of file diff --git a/src/main/java/com/example/firstproject/controller/EmailController.java b/src/main/java/com/example/firstproject/controller/EmailController.java index 50fb8cf..4e88eb4 100644 --- a/src/main/java/com/example/firstproject/controller/EmailController.java +++ b/src/main/java/com/example/firstproject/controller/EmailController.java @@ -5,7 +5,9 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +@RestController public class EmailController { // 회원가입 메일 서비스 diff --git a/src/main/java/resource/EmailResource.java b/src/main/java/com/example/firstproject/resource/EmailResource.java similarity index 86% rename from src/main/java/resource/EmailResource.java rename to src/main/java/com/example/firstproject/resource/EmailResource.java index 0f097bf..b32f830 100644 --- a/src/main/java/resource/EmailResource.java +++ b/src/main/java/com/example/firstproject/resource/EmailResource.java @@ -1,4 +1,4 @@ -package resource; +package com.example.firstproject.resource; public class EmailResource { public static String getMailserver() { diff --git a/src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java b/src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java index ecf9387..9b4fa69 100644 --- a/src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java +++ b/src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java @@ -49,7 +49,7 @@ public MimeMessage createMessage(String to) throws MessagingException, Unsupport @Override public String createKey() { Random random = new Random(); - int num = random.nextInt(8); + int num = random.nextInt(8) * 12345; String key = String.valueOf(num); return key; } @@ -84,6 +84,4 @@ public void sendEmail(String to, String subject, String message){ simpleMailMessage.setText(message); this.mailSender.send(simpleMailMessage); - }*/ - - + }*/ \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 50a9865..e94e978 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -32,7 +32,7 @@ spring.datasource.initialization-mode=never spring.jpa.hibernate.ddl-auto=update -mail.smtp.socketFactory.port=465 +mail.smtp.socketFactory.port=587 mail.smtp.auth=SSL mail.smtp.starttls.enable=true mail.smtp.starttls.required=true From 69c3af9845a7e0eb24f8d3e399e77e58930c40e1 Mon Sep 17 00:00:00 2001 From: yim-yongwoo Date: Tue, 18 Jul 2023 13:33:48 +0900 Subject: [PATCH 5/7] =?UTF-8?q?=EB=B3=B4=EC=95=88=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/firstproject/resource/EmailResource.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/firstproject/resource/EmailResource.java b/src/main/java/com/example/firstproject/resource/EmailResource.java index b32f830..085b75c 100644 --- a/src/main/java/com/example/firstproject/resource/EmailResource.java +++ b/src/main/java/com/example/firstproject/resource/EmailResource.java @@ -6,10 +6,10 @@ public static String getMailserver() { } public static String getUsername() { - return "yongwoo1207@naver.com"; + return "이메일"; } public static String getUserpwd() { - return "gnem36zjvl"; + return "비밀번호"; } } From a4111797f49c81dd91954853249a50768f942be2 Mon Sep 17 00:00:00 2001 From: yim-yongwoo Date: Tue, 18 Jul 2023 13:43:07 +0900 Subject: [PATCH 6/7] =?UTF-8?q?Revert=20"=EB=8B=A4=EC=8B=9C=20=EC=98=AC?= =?UTF-8?q?=EB=A6=AC=EA=B8=B0"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 977308587e65d4639fe0c8726d848b0b12d0f7db. --- .../example/firstproject/controller/EmailController.java | 2 -- .../firstproject/service/impl/EmailServiceImpl.java | 6 ++++-- .../example/firstproject => }/config/EmailConfig.java | 9 +++++---- .../firstproject => }/resource/EmailResource.java | 2 +- src/main/resources/application.properties | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) rename src/main/java/{com/example/firstproject => }/config/EmailConfig.java (81%) rename src/main/java/{com/example/firstproject => }/resource/EmailResource.java (86%) diff --git a/src/main/java/com/example/firstproject/controller/EmailController.java b/src/main/java/com/example/firstproject/controller/EmailController.java index 4e88eb4..50fb8cf 100644 --- a/src/main/java/com/example/firstproject/controller/EmailController.java +++ b/src/main/java/com/example/firstproject/controller/EmailController.java @@ -5,9 +5,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; -@RestController public class EmailController { // 회원가입 메일 서비스 diff --git a/src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java b/src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java index 9b4fa69..ecf9387 100644 --- a/src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java +++ b/src/main/java/com/example/firstproject/service/impl/EmailServiceImpl.java @@ -49,7 +49,7 @@ public MimeMessage createMessage(String to) throws MessagingException, Unsupport @Override public String createKey() { Random random = new Random(); - int num = random.nextInt(8) * 12345; + int num = random.nextInt(8); String key = String.valueOf(num); return key; } @@ -84,4 +84,6 @@ public void sendEmail(String to, String subject, String message){ simpleMailMessage.setText(message); this.mailSender.send(simpleMailMessage); - }*/ \ No newline at end of file + }*/ + + diff --git a/src/main/java/com/example/firstproject/config/EmailConfig.java b/src/main/java/config/EmailConfig.java similarity index 81% rename from src/main/java/com/example/firstproject/config/EmailConfig.java rename to src/main/java/config/EmailConfig.java index 98767f6..e188883 100644 --- a/src/main/java/com/example/firstproject/config/EmailConfig.java +++ b/src/main/java/config/EmailConfig.java @@ -1,11 +1,13 @@ -package com.example.firstproject.config; +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 com.example.firstproject.resource.EmailResource; +import resource.EmailResource; + +import java.util.Properties; @Configuration public class EmailConfig { @@ -15,9 +17,8 @@ public JavaMailSender javaMailService() { javaMailSender.setHost(EmailResource.getMailserver()); // 메일 도메인 서버 주소 javaMailSender.setUsername(EmailResource.getUsername()); // 메일 유저 이름 javaMailSender.setPassword(EmailResource.getUserpwd()); // 메일 패스워드 - javaMailSender.setPort(587); // 메일 인증서버 포트 + javaMailSender.setPort(465); // 메일 인증서버 포트 return javaMailSender; } - } \ No newline at end of file diff --git a/src/main/java/com/example/firstproject/resource/EmailResource.java b/src/main/java/resource/EmailResource.java similarity index 86% rename from src/main/java/com/example/firstproject/resource/EmailResource.java rename to src/main/java/resource/EmailResource.java index 085b75c..ab5b91a 100644 --- a/src/main/java/com/example/firstproject/resource/EmailResource.java +++ b/src/main/java/resource/EmailResource.java @@ -1,4 +1,4 @@ -package com.example.firstproject.resource; +package resource; public class EmailResource { public static String getMailserver() { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e94e978..50a9865 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -32,7 +32,7 @@ spring.datasource.initialization-mode=never spring.jpa.hibernate.ddl-auto=update -mail.smtp.socketFactory.port=587 +mail.smtp.socketFactory.port=465 mail.smtp.auth=SSL mail.smtp.starttls.enable=true mail.smtp.starttls.required=true From 6129f7179bc261aa12882b975b1365f1ef41e44b Mon Sep 17 00:00:00 2001 From: yim-yongwoo Date: Wed, 19 Jul 2023 15:49:49 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=B3=B5=EA=B5=AC=20-?= =?UTF-8?q?=20=EB=B9=84=EB=B0=80=EB=B2=88=ED=98=B8=20=EC=A0=9C=EA=B1=B0(pr?= =?UTF-8?q?operties=EC=97=90=20=EB=84=A3=EB=8A=94=20=EB=B0=A9=EC=8B=9D?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B0=94=EA=BF=94=EC=95=BC=20=ED=95=A0?= =?UTF-8?q?=EB=93=AF)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/resource/EmailResource.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/resource/EmailResource.java b/src/main/java/resource/EmailResource.java index ab5b91a..104cc9d 100644 --- a/src/main/java/resource/EmailResource.java +++ b/src/main/java/resource/EmailResource.java @@ -6,10 +6,10 @@ public static String getMailserver() { } public static String getUsername() { - return "이메일"; + return "이메일 입력하기"; } public static String getUserpwd() { - return "비밀번호"; + return "비밀번호 입력하기"; } }