Skip to content
Open
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 @@ -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'

}


Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/example/firstproject/service/EmailService.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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 += "<div style='margin:100px'>";
msg += "<h1>안녕하세요</h1>";
msg += "<h3>회원가입 인증 코드 입니다.</h3>";
msg += "<br>";
msg += ePw + "<div><br/>";
msg += "</div>";

// 내용, 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);
}*/


24 changes: 24 additions & 0 deletions src/main/java/config/EmailConfig.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
15 changes: 15 additions & 0 deletions src/main/java/resource/EmailResource.java
Original file line number Diff line number Diff line change
@@ -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 "비밀번호 입력하기";
}

}
7 changes: 7 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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