Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- isNotEmpty
- pem
- Python
- 자바
- 명령어
- 티스토리챌린지
- springboot
- Linux
- github
- java
- spring
- 리눅스
- DBMS
- analytics4
- iBatis
- PostgreSQL
- Javascript
- MariaDB
- SQL
- IntelliJ
- mysql
- git
- Kibana
- pandas
- docker
- 오블완
- 호이스팅
- oracle
- mssql
- 404error
Archives
- Today
- Total
hanker
Spring boot - 이메일 전송 (2) 본문
반응형
다국어 처리 이메일 인증 전송
- 쿠키 값을 가지고 다국어 처리를 위한 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
- lang=en
반응형
'SPRING' 카테고리의 다른 글
Java - 웹 크롤링 이미지 파일 다운로드(Web Crawling img Download) - jsoup (2) (0) | 2021.11.09 |
---|---|
Java - 웹 크롤링(Web Crawling) - jsoup (1) (0) | 2021.11.09 |
Spring Boot - 이메일 전송(1)(JavaMailSender) (2) | 2021.10.04 |
Spring Security - 로그인 경로 지정(java) (0) | 2021.08.25 |
Spring boot - boot 실행 시 sql 문 실행 (0) | 2021.08.23 |