hanker

Spring boot - 이메일 전송 (2) 본문

SPRING

Spring boot - 이메일 전송 (2)

hanker 2021. 10. 7. 09:29

다국어 처리 이메일 인증 전송

 

- 쿠키 값을 가지고 다국어 처리를 위한 jquery-cookie cdn 추가

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>

 

- 선택한 나라에 맞는 언어로 이메일을 전송하기 위해 쿠키 값을 가져와 Controller로 데이터 전송 (처리는 Service단에서 처리)

<!-- email 인증-->
<script th:fragment="register">
   $(function(){
        $("#certEmail").click(function(){
            // getCookie (ex : ?lang=en)
            var lang = $.cookie('lang');

            var data = {
                email : $("#inputEmail").val(),
                lang : lang
            };

            $.ajax({
                url : "/certifiedEmail",
                type : "post",
                data : data,
                dataType : "json",
                success: [function(rs){
                   console.log(rs);
                }], error : function(rs){
                }
           });
       });
   });
</script>

 

- Controller

@PostMapping("/certifiedEmail")
public String certifiedEmail(@RequestParam("email") String email, @RequestParam("lang") String lang){

    loginService.mailSend(email, lang);

    // MappingJackson2JsonView Bean 등록
    return "jsonView";
}

 

- Service

@RequiredArgsConstructor
@Service
public class LoginService {

    private final JavaMailSender javaMailSender;

    public void mailSend(String email, String lang) {
        ConfigurableApplicationContext ctx = new GenericApplicationContext(); // Context 객체 생성
        ConfigurableEnvironment env = ctx.getEnvironment();
        MutablePropertySources propertySources = env.getPropertySources();

        try{
            if(lang.equals("en")){
                propertySources.addLast(new ResourcePropertySource("classpath:messages/message_en.properties"));
            } else {
                propertySources.addLast(new ResourcePropertySource("classpath:messages/message.properties"));
            }
            String title = new String(env.getProperty("spring.mail.title").getBytes("ISO-8859-1"), "UTF-8");
            String content = new String(env.getProperty("spring.mail.content").getBytes("ISO-8859-1"), "UTF-8");
            
            // Email 인증 난수 생성
            Random random = new Random();
            String key = "";
            for(int i = 0 ; i < 3 ; i++){
                int index = random.nextInt(25)+65;
                key += (char) index;
            }
            int numIndex = random.nextInt(9999)+1000;
            key += numIndex;

            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setTo(email);
            simpleMailMessage.setSubject(title);
            simpleMailMessage.setText(content + key);

            javaMailSender.send(simpleMailMessage);
        } catch(IOException e){
            e.printStackTrace();
        }
    }
}

 

 

- message.properties

## 이메일 인증
spring.mail.title=JJStudy 인증 메일입니다.
spring.mail.content=인증 번호 :

 

- message_en.properties

## 이메일 인증
spring.mail.title=JJStudy Email authentication.
spring.mail.content=Authentication number :

 

 

- lang=ko

이메일 입력 후 인증요청
인증 메일(ko)

lang=en

이메일 입력 후 인증
인증 메일(en)