코딩과 결혼합니다

231018 - [Game_Crew]트러블슈팅 : 이메일 인증 구현하기 본문

코딩과 매일매일♥/Game_Crew

231018 - [Game_Crew]트러블슈팅 : 이메일 인증 구현하기

코딩러버 2023. 10. 18. 19:02
728x90

📌 의존성 추가하기

implementation 'org.springframework.boot:spring-boot-starter-mail:2.7.0'

 

 

📌 네이버 pop3 / SMTP 설정

 

//properties 설정

spring.mail.host=smtp.naver.com
spring.mail.port=465
spring.mail.username=abcd@naver.com
spring.mail.password=abcdef
spring.mail.properties.debug=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable= true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.trust=smtp.naver.com
  • spring.mail.properties.debug: 디버그 모드로 설정, 메일 전송 과정에 대한 자세한 로그 출력
  • spring.mail.properties.mail.smtp.auth: SMTP 인증(authentication)을 활성화. 이 값이 true로 설정되면, SMTP 서버에 로그인할 때 계정 정보 필요
  • spring.mail.properties.mail.smtp.ssl.enable: SSL 보안 연결( 메일 전송 과정에서 데이터 암호화를 제공 )을 활성화
  • spring.mail.properties.mail.smtp.starttls.enable: STARTTLS(TLS 시작) 연결을 활성화. STARTTLS는 평문 통신 채널에서 암호화 통신 채널로 업그레이드
  • spring.mail.properties.mail.smtp.ssl.trust: 신뢰할 수 있는 SMTP 서버 호스트를 지정

 

📌 MailConfig

@Configuration
@PropertySource("classpath:application.properties")
public class Email {

    @Value("${spring.mail.username}")
    private String id;
    @Value("${spring.mail.password}")
    private String password;
    @Value("${spring.mail.host}")
    private String host;
    @Value("${spring.mail.port}")
    private int port;

    @Bean
    public JavaMailSender javaMailService() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        javaMailSender.setHost(host); // smtp 서버 주소
        javaMailSender.setUsername(id); // 설정(발신) 메일 아이디
        javaMailSender.setPassword(password); // 설정(발신) 메일 패스워드
        javaMailSender.setPort(port); //smtp port
        javaMailSender.setJavaMailProperties(getMailProperties()); // 메일 인증서버 정보 가져온다.
        javaMailSender.setDefaultEncoding("UTF-8");
        return javaMailSender;
    }

    private Properties getMailProperties() {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", "smtp"); // 프로토콜 설정
        properties.setProperty("mail.smtp.auth", "true"); // smtp 인증
        properties.setProperty("mail.smtp.starttls.enable", "true"); // smtp starttls 사용
        properties.setProperty("mail.debug", "true"); // 디버그 사용
        properties.setProperty("mail.smtp.ssl.trust", "smtp.mailplug.co.kr"); // ssl 인증 서버 주소
        properties.setProperty("mail.smtp.ssl.enable", "true"); // ssl 사용
        return properties;
    }
}

 

 

📌EmailService

@PropertySource("classpath:application.properties")
@Slf4j
@RequiredArgsConstructor
@Service
public class EmailService {

    private final JavaMailSender javaMailSender;

    //인증번호 생성
    private final String ePw = createKey();

    @Value("${spring.mail.username}")
    private String id;

    public MimeMessage createMessage(String to)throws MessagingException, UnsupportedEncodingException {
        log.info("보내는 대상 : "+ to);
        log.info("인증 번호 : " + ePw);
        MimeMessage  message = javaMailSender.createMimeMessage();

        message.addRecipients(MimeMessage.RecipientType.TO, to); // to 보내는 대상
        message.setSubject("Game_Crew 회원가입 인증 코드: "); //메일 제목

        // 메일 내용 메일의 subtype을 html로 지정하여 html문법 사용 가능
        String msg="";
        msg += "<h1 style=\"font-size: 30px; padding-right: 30px; padding-left: 30px;\">이메일 주소 확인</h1>";
        msg += "<p style=\"font-size: 17px; padding-right: 30px; padding-left: 30px;\">아래 확인 코드를 회원가입 화면에서 입력해주세요.</p>";
        msg += "<div style=\"padding-right: 30px; padding-left: 30px; margin: 32px 0 40px;\"><table style=\"border-collapse: collapse; border: 0; background-color: #F4F4F4; height: 70px; table-layout: fixed; word-wrap: break-word; border-radius: 6px;\"><tbody><tr><td style=\"text-align: center; vertical-align: middle; font-size: 30px;\">";
        msg += ePw;
        msg += "</td></tr></tbody></table></div>";

        message.setText(msg, "utf-8", "html"); //내용, charset타입, subtype
        message.setFrom(new InternetAddress(id,"prac_Admin")); //보내는 사람의 메일 주소, 보내는 사람 이름

        return message;
    }

    // 인증코드 만들기
    public static String createKey() {
        StringBuffer key = new StringBuffer();
        Random rnd = new Random();

        for (int i = 0; i < 6; i++) { // 인증코드 6자리
            key.append((rnd.nextInt(10)));
        }
        return key.toString();
    }

    /*
        메일 발송
        sendSimpleMessage의 매개변수로 들어온 to는 인증번호를 받을 메일주소
        MimeMessage 객체 안에 내가 전송할 메일의 내용을 담아준다.
        bean으로 등록해둔 javaMailSender 객체를 사용하여 이메일 send
     */
    public String sendSimpleMessage(String to)throws Exception {
        MimeMessage message = createMessage(to);
        try{
            javaMailSender.send(message); // 메일 발송
        }catch(MailException es){
            es.printStackTrace();
            throw new IllegalArgumentException();
        }
        return ePw; // 메일로 보냈던 인증 코드를 서버로 리턴
    }
}

 

문제

필요 타입: javax.mail.internet.MimeMessage
제공된 타입: jakarta.mail.internet.MimeMessage

시도

Spring Boot 2.7.0 버전부터는 Jakarta EE의 패키지 구조를 따르기 위해 일부 의존성 패키지가 변경 됨. 따라서, 해당 버전에서는 jakarta.mail 패키지를 사용 해야 한다.

의존성 변경(1)

implementation 'org.springframework.boot:spring-boot-starter-mail:2.7.0'
implementation 'com.sun.mail:jakarta.mail:1.6.x' // jakarta.mail 의존성 추가

실패 ➡️ 시도(2)

Spring Boot 3.4.0 버전에서 org.springframework.boot:spring-boot-starter-mail 의존성을 사용하면, Spring Boot 자체가 필요한 메일 관련 라이브러리를 알아서 가져오기 때문에 별도로 com.sun.mail:jakarta.mail:1.6.x 같은 의존성을 추가할 필요가 없다.

의존성 변경(2)

implementation 'org.springframework.boot:spring-boot-starter-mail'

결론

'org.springframework.boot:spring-boot-starter-mail:2.7.0'은 Spring Boot 2.7.0 버전에 맞는 메일 서비스 스타터 패키지로
호환되지 않아 오류가 발생했을 가능성이 크다.

'implementation 'org.springframework.boot:spring-boot-starter-mail'' 로 변경하였을 때, Gradle 프로젝트에서 사용 중인 Spring Boot와 호환되는 최신 버전의 spring-boot-starter-mail 패키지를 가져오게 되며 문제를 해결

 

 

📌AuthController

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/auth")
public class AuthController {

    private final EmailService emailService;

    @PostMapping("/signup/email/check")
    @ResponseBody
    public String mailConfirm(@RequestParam String email) throws Exception {
        String code = emailService.sendSimpleMessage(email);
        log.info("인증코드 : " + code);
        return code;
    }
}