[Spring Boot] Gmail SMTP 사용하기

귀찮Lee·2022년 9월 11일
0

MOCCO Project (22.09~10)

목록 보기
2/5

◎ Gmail 설정

◎ property 설정

mail:
  smtp:
    host: smtp.gmail.com
    port: 587
    username: {{ gmail-ID }}
    password: {{ 기기용 앱 비밀번호 }}
    auth: true
    starttls:
      enable: true

◎ Email 보내기

  • Configuration

    @Configuration
    public class EmailConfiguration {
    
        @Value("${mail.smtp.host}")
        private String host;
        @Value("${mail.smtp.port}")
        private int port;
        @Value("${mail.smtp.username}")
        private String username;
        @Value("${mail.smtp.password}")
        private String password;
        @Value("${mail.smtp.auth}")
        private String auth;
        @Value("${mail.smtp.starttls.enable}")
        private String tlsEnable;
    
        // EmailSendable은 상황마다 다르게 설정 
        @Bean
        @Primary
        public EmailSendable simpleEmailSender(){
            return new SimpleEmailSender(javaMailSender());
        }
    
        @Bean
        public JavaMailSender javaMailSender() {
            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
            mailSender.setHost(host);
            mailSender.setPort(port);
            mailSender.setUsername(username);
            mailSender.setPassword(password);
    
            Properties props = mailSender.getJavaMailProperties();
            props.put("mail.smtp.auth", auth);
            props.put("mail.smtp.starttls.enable", tlsEnable);
    
            return mailSender;
        }
    }
  • Interface

    public interface EmailSendable {
        void send(String[] to, String subject, String Message) throws InterruptedException;
    }
  • implements

    @AllArgsConstructor
    public class SimpleEmailSender implements EmailSendable{
    
        private final JavaMailSender javaMailSender;
    
        @Override
        public void send(String[] to, String subject, String message) throws InterruptedException {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setTo(to);
            mailMessage.setText(message);
            mailMessage.setSubject(subject);
            mailMessage.setText(message);
            javaMailSender.send(mailMessage);
    
            System.out.println("Sent simple email!");
        }
    }
  • Text 보내기

    @Service
    @AllArgsConstructor
    public class EmailSender {
        private final EmailSendable emailSendable;
    
        public void setMailSender(String[] to, String subject, String message) throws MailSendException,
                InterruptedException {
            emailSendable.send(to, subject, message);
        }
        public void setEmailSenderSendOne(String to, String subject, String message) throws MailSendException,
                InterruptedException{
            emailSendable.send(new String[]{to}, subject, message);
        }
    }
  • 사용 예시

        @GetMapping("/send-mail")
        public String checkSendMail(@RequestParam String email) {
            String[] mail = new String[]{email};
            try{
                emailSender.setMailSender(mail, "TEAM17 이메일 전송 테스트입니다.", "TEAM17 이메일 전송 테스트입니다.");
            } catch (Exception e){
                throw new BusinessLogicException(ExceptionCode.FAIL_SEND_EMAIL);
            }
            return email + " 메일함 확인하세요.";
        }
profile
배운 것은 기록하자! / 오류 지적은 언제나 환영!

0개의 댓글