728x90

비밀번호 찾기를 시도한 사용자의 비밀번호를 초기화 시키고 초기화된 비밀번호를 사용자 이메일로 보낸다.

1. 인증 절차

  • 구글 계정 관리 -> 보안 -> 앱 비밀번호 -> [메일], [windows 컴퓨터] 2개 선택 후 생성
  • 여기서 나오는 비밀번호를 application.properties에 추가할 것이다.

2. 설정

  • gmail -> 톱니바퀴 클릭 후 모든 설정보기

톱니바퀴

  • 전달 및 모든 POP/IMAP 진입

  • POP 다운로드 -> 모든 메일에 POP 사용하기체크
  • IMAP 액세스 -> IMAP사용 체크
  • 변경사항 저장

3. 의존성 추가

// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail'

 

4. SMTP 설정

# GoogleMail
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=사용자 구글 아이디
spring.mail.password=인증 비밀번호
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.smtp.auth=true
  • username=test2@gmail.com
  • password=아까 인증에서 비밀번호

5. MailService

@Autowired
private JavaMailSender javaMailSender;

// 메일을 통해 임시 비밀번호 전송
public void sendMail(String username, String userEmail, String temporaryPassword) {
    List<String> toUserList = new ArrayList<>();
    toUserList.add(userEmail);
    int userSize = toUserList.size();

    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    simpleMailMessage.setTo((String[]) toUserList.toArray(new String[userSize]));
    simpleMailMessage.setSubject("임시 비밀번호를 보내드립니다.");
    simpleMailMessage.setText(username + "님의 임시 비밀번호 : " + temporaryPassword);

    javaMailSender.send(simpleMailMessage);
}

 

* 참고 사이트

https://kitty-geno.tistory.com/43

 

Spring Boot | 메일 발송하기 (Google SMTP)

▶ 스프링 부트 메일 발송하기 (Google SMTP) 1. Google 홈페이지 > Google 계정 관리(우측상단) 2. 보안 > 앱 비밀번호 앱 비밀번호는 위에 2단계 인증을 해야 생성됩니다. 3. 메일, Windows 컴퓨터 4. 앱 비..

kitty-geno.tistory.com

 

+ Recent posts