sitelink1 | https://blog.naver.com/till-its-over/222980880983 |
---|---|
sitelink2 | |
sitelink3 | |
sitelink4 | |
sitelink5 | |
sitelink6 |
다음과 같은 네이버 메일 설정이 필요하다
javax.mail jar 라이브러리는 다음의 경로들중에 하나에서 다운받는다
- https://mvnrepository.com/artifact/javax.mail/mail/1.4.7
- https://mvnrepository.com/artifact/com.sun.mail/javax.mail
코드는 다음과 같다 (전송 테스트 성공)
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class TestJavaMail {
public static void main(String[] args) {
Properties p = System.getProperties();
p.put("mail.smtp.starttls.enable", "true"); // gmail은 true 고정
p.put("mail.smtp.host", "smtp.naver.com"); // smtp 서버 주소
p.put("mail.smtp.auth","true"); // gmail은 true 고정
p.put("mail.smtp.port", "587"); // 네이버 포트
Authenticator auth = new MyAuthentication();
//session 생성 및 MimeMessage생성
Session session = Session.getDefaultInstance(p, auth);
MimeMessage msg = new MimeMessage(session);
try{
//편지보낸시간
msg.setSentDate(new Date());
InternetAddress from = new InternetAddress() ;
from = new InternetAddress("sender_test@naver.com"); //발신자 아이디
// 이메일 발신자
msg.setFrom(from);
// 이메일 수신자
InternetAddress to = new InternetAddress("receiver_test@gmail.com");
msg.setRecipient(Message.RecipientType.TO, to);
// 이메일 제목
msg.setSubject("메일 전송 테스트", "UTF-8");
// 이메일 내용
msg.setText("티스토리 테스트", "UTF-8");
// 이메일 헤더
msg.setHeader("content-Type", "text/html");
//메일보내기
javax.mail.Transport.send(msg, msg.getAllRecipients());
}catch (AddressException addr_e) {
addr_e.printStackTrace();
}catch (MessagingException msg_e) {
msg_e.printStackTrace();
}catch (Exception msg_e) {
msg_e.printStackTrace();
}
}
}
class MyAuthentication extends Authenticator {
PasswordAuthentication pa;
public MyAuthentication(){
String id = "test@naver.com"; //네이버 이메일 아이디
String pw = "test"; //네이버 비밀번호
// ID와 비밀번호를 입력한다.
pa = new PasswordAuthentication(id, pw);
}
// 시스템에서 사용하는 인증정보
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
}