해결 링크 : ↗바로가기
public String sendEmail(UserVO userVO) {
String host = "smtp.naver.com";
String guest = "@naver.com"; // 발신자 메일
String password = ""; // 발신자 패스워드
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(guest, password);
}
}
);
// 인증번호 난수 6자리 설정
String crtfcNo = RandomStringUtils.randomNumeric(6);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(guest));
// 메일 대상
message.addRecipient(Message.RecipientType.TO, new InternetAddress(userVO.getUserId()));
// 메일 제목
message.setSubject("제목");
// 메일 내용
message.setText("귀하의 이메일 인증번호는 " + crtfcNo + " 입니다.\n인증번호를 복사하여 입력해주세요.");
// send the message
Transport.send(message);
System.out.println("Success Message Send");
} catch (MessagingException e) {
e.printStackTrace();
}
return crtfcNo;
}
'''