[Spring Boot] Gmail SMTP를 이용해서 JavaMailSender 사용하기

dOcOb·2023년 2월 16일
0

Spring Boot

목록 보기
1/1

Server code


build.gradle

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

application.yml

#java mail sender
spring:
  mail:
    host: smtp.gmail.com
    port: 587
    username: 보내는이메일
    password: 앱비밀번호
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true

MailDTO

@Getter
@Setter
@NoArgsConstructor
public class MailDTO {
    private String address;
    private String title;
    private String message;
}

MailHandler

@Service
public class MailHandler {
    private JavaMailSender sender;
    private MimeMessage message;
    private MimeMessageHelper messageHelper;

    // 생성자
    public MailHandler(JavaMailSender jSender) throws
            MessagingException {
        this.sender = jSender;
        message = jSender.createMimeMessage();
        messageHelper = new MimeMessageHelper(message, true, "UTF-8");
    }

    // 보내는 사람 이메일
    public void setFrom(String fromAddress) throws MessagingException {
        messageHelper.setFrom(fromAddress);
    }

    // 받는 사람 이메일
    public void setTo(String email) throws MessagingException {
        messageHelper.setTo(email);
    }

    // 제목
    public void setSubject(String subject) throws MessagingException {
        messageHelper.setSubject(subject);
    }

    // 메일 내용
    public void setText(String text, boolean useHtml) throws MessagingException {
        messageHelper.setText(text, useHtml);
    }

    // 첨부 파일
    public void setAttach(String displayFileName, String pathToAttachment) throws MessagingException, IOException {
        File file = new ClassPathResource(pathToAttachment).getFile();
        FileSystemResource fsr = new FileSystemResource(file);

        messageHelper.addAttachment(displayFileName, fsr);
    }

    // 이미지 삽입
    public void setInline(String contentId, String pathToInline) throws MessagingException, IOException {
        File file = new ClassPathResource(pathToInline).getFile();
        FileSystemResource fsr = new FileSystemResource(file);

        messageHelper.addInline(contentId, fsr);
    }

    // 발송
    public void send() {
        try {
            sender.send(message);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}

MailRestController

@RestController
public class MailRestController {
    @Autowired
    MailService mailService;
    @PostMapping("/mailCerti")
    public void mailCerti(MailDTO mail) {
        mailService.mailSend(mail);
    }
}

MailService

@Service
@AllArgsConstructor
@NoArgsConstructor
public class MailService {
    @Autowired
    MailHandler mailHandler;
    private static final String FROM_ADDRESS = "SMTP 이메일";

    public void mailSend(MailDTO mailDto) throws MessagingException {
        mailHandler.setFrom(FROM_ADDRESS);
        mailHandler.setTo(mailDto.getAddress());
        mailHandler.setSubject(mailDto.getTitle());
        mailHandler.setText(mailDto.getMessage(), true);
        mailHandler.send();
    }
}



Front 예시


email_confirm_num = Math.floor(Math.random()*1000000)
$.ajax({
    url: "/mailCerti",
    type: "post",
    data: {
        address: $("#email").val(),
        title: "Deli email confirm",
        message: "<h1>"+email_confirm_num+"</h1>"
    }
});



코드 작동 순서


  1. Controller에서 MailDTO에 값을 담는다.
  2. MailService에서 MailHandler에 값을 세팅하고 메일을 보낸다.
profile
반은 해야 시작이다.

0개의 댓글