Spring Boot/STUDY

[Spring Boot] JPA 프로젝트 - 비밀번호 찾기 및 변경 기능 구현(1)

코맹 2024. 6. 27. 16:51

 

비밀번호 찾기 및 변경 기능을 하기 전 메일 전송 테스트를 해볼려고 한다!

 

build.gradle 메일을 보내기 위한 디펜던시 추가
// 메일전송 디펜던시
implementation 'org.springframework.boot:spring-boot-starter-mail'

 

 

application.properties 메일 설정 입력
## 메일설정
spring.mail.host=smtp.naver.com
spring.mail.port=465
spring.mail.username=이메일(수신자)
spring.mail.password=비밀번호
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.ssl.trust=smtp.naver.com
spring.mail.properties.mail.debug=true

 

 

네이버 메일 SMTP 설정 > 환경설정 > POP3/IMAP 설정

네이버 이메일 설정

 

 

/config/SecurityConfig.java CSRF 설정 변경
// .csrf((csrf) -> csrf.ignoringRequestMatchers(new AntPathRequestMatcher("/h2-console/**")))
   .csrf(csrf -> csrf.disable())

 

 

/service/MailService.java 생성
package com.eunji.backboard.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MailService {
  private final JavaMailSender javaMailSender;
  // private final PasswordEncoder passwordEncoder;

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

  public void sendMail(String to, String subject, String message){
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();   // MIME type 설정

    try {
      // MimeMessageHelper로 MimeMessage 구성, 이메일에 작성되는 글은 UTF-8로 생성
      MimeMessageHelper mmh = new MimeMessageHelper(mimeMessage, false, "UTF-8");
      // 이메일 수신자 설정
      mmh.setTo(to);
      // 이메일 제목 설정
      mmh.setSubject(subject);
      // 본문 내용 설정
      mmh.setText(message);
      // 이메일 발신자 설정
      mmh.setFrom(new InternetAddress(from));
      // 이메일 전송
      javaMailSender.send(mimeMessage);
      
    } catch (MessagingException e) {
      throw new RuntimeException();
      
    }
  }

}

 

 

/restcontroller/MailController.java 생성
package com.eunji.backboard.restcontroller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.eunji.backboard.service.MailService;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;

@RestController
@RequiredArgsConstructor
@RequestMapping("/mail")
public class MailController {
  private final MailService mailService;

  @PostMapping("/test-email")
  @ResponseBody
  public ResponseEntity<HttpStatus> testEmail() {
    String to = "받을 이메일주소";
    String subject = "전송 테스트 메일";
    String message = "테스트 메일 메시지입니다.";

    mailService.sendMail(to, subject, message);
      
    return new ResponseEntity<HttpStatus>(HttpStatus.OK);

  }
}

 

 

postman에서 테스트

  • Status: 200 OK

 

 

메일 확인