Sangil's blog

https://github.com/ChoiSangIl Admin

Java 네이버 메일 보내기 DEV / PROGRAMING

2020-04-15 posted by sang12


Java에서 naver smtp 서버를 이용해서 메일 보내는 방법을 공유 합니다. 다른 블로그 문서 참고하다가 다른 mail 라이브러리를 참고해서 삽질 많이했네요. 아래 mail api를 사용해주세요.

-naver smtp 설정하기



-gradle

// https://mvnrepository.com/artifact/javax.mail/javax.mail-api
compile group: 'javax.mail', name: 'mail', version: '1.4.7'

-java

Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.naver.com");
props.put("mail.smtp.port", "25");
props.put("defaultEncoding", "utf-8");
props.put("mail.smtp.auth", "true");
			
try {
	String sender = mailId + "@naver.com"; 
	String subject = "[블로그 댓글] 게시글 번호: "; 
	String body = "내용";
				
	Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
		protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
			return new javax.mail.PasswordAuthentication(mailId, mailPw);
		}
	});
				
	session.setDebug(false); //Debug 모드 설정.
	Message mimeMessage = new MimeMessage(session);
	mimeMessage.setFrom(new InternetAddress(sender)); 
	mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("sang12@kakao.com") ); //수신자 셋팅
	mimeMessage.setSubject(subject); //제목 세팅
	mimeMessage.setText(body); //본문 세팅
	Transport.send(mimeMessage);
} catch (Exception e) {
	System.out.println("메일보내기 오류 : "+ e.getMessage());
}

#java 메일 보내기 #네이버 메일 보내기 #java smtp
REPLY