메일 메일 기다려

leverest96·2023년 4월 6일
0

Spring / Java

목록 보기
12/20
post-thumbnail

회원가입할 때 내가 나 자신이 맞는지 확인하기 위해 메일로 인증번호를 받아 사용하는 경우가 많다.
이전 프로젝트에 적용이 되어있지만 내가 직접 해본 것은 아니어서 개인 프로젝트에서 시도해봤다.

  1. dependency 추가

    implementation 'org.springframework.boot:spring-boot-starter-mail'
  2. yml 설정

    spring:
      mail:
        host: smtp.gmail.com
        port: 587
    
        username: ${GMAIL_USERNAME:test@gmail.com}
        password: ${GMAIL_PASSWORD:test}
    
        properties:
          mail:
            smtp:
              auth: true
    
              starttls:
                enable: true
  3. properties

    @ConfigurationProperties(prefix = "gmail")
    @Getter
    @RequiredArgsConstructor
    public class GmailProperties {
        private final String username;
        private final String password;
    }
    • 나의 경우 환경변수로 해당 변수들을 등록해놨기 때문에 property 클래스로 따로 받아왔다.
  4. Service

    private final JavaMailSender mailSender;
    
    public boolean verificationSender(String email) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
    
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
    
        helper.setTo(email);
        helper.setSubject("subject");
        helper.setText("text");
    
        mailSender.send(message);
    
        return true;
    }
profile
응애 난 애기 개발자

0개의 댓글