-
[JAVA] JAVAMail APIBack/JAVA 2020. 6. 15. 15:05
==================================================
[관련 프로토콜]
- SMTP(Simple Mail Transfer Protocol)
: 메일 송신 프로토콜.
- POP(Post Office Protocol, POP3)
: 메일 수신 프로토콜. 단순 수신 메일박스 저장.
- IMAP(Internet Message Access Protocol,IMAP4)
: 향상된 수신 프로토콜. 메시지 전송하고 여러폴더에 공유
- MIME(Multipurpose Internet Mail Extensions)
: 송수신 메시지가 어떤 내용인지 정의함
==================================================
[핵심 클래스]
자바 메일 모든 작업은
Session 클래스를 객체화 시킴으로서 시작된다.(HttpSession,jsp session과 다른거임 ^-^;)
Session : 메일 서버와의 세션 관리
Message : 메일 메시지 정보 및 내용
Address : 이메일 주소
Authenticator : 아이디/비번호 정보
Transport : 메일 전송
Store / Folder : 메일 박스 및 메일 폴더 지정
==================================================
[Session] 메일 서버 정보 지정. (메일 도메인, 접속id, pw)
:: Properties 객체로 관리props.put("mail.smtp.host","smtp.naver.com"); props.put("mail.smtp.auth","true"); props.put("mail.smtp.port", 465); ** props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.trust", host);
Session session
= Session.getDefaultInstance(props);
= Session.getDefaultInstance(props,authenticator);
** port: 메일서버 포트 지정, 미지정시 프로토콜 기본 포트
==================================================
[Authentication ]
메일을 송수신할 때 메일서버에 접속하기 위한 인증에 사용
Authenticator auth = new PasswordAuthenticator();
==================================================[Message] 추상클래스. MimeMessage로 구현해 사용
Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("")); ** 보낸이 지정. 미지정시 lllegalWriteException message.addRecipient( Message.RecipientType.TO, new InternetAddress(to)); ** 받는이지정. 미지정시 lllegalWriteException message.setSubject(""); message.setText(""); message.setSentDate(new Date()); message.setContent(“Hello”, “text/plain”); 메시지의 MIME 형을 지정 +) addRecipients(Message.RecipientType, Address[]) getAllRecipients() -- 받는이 모든 주소들 리턴(배열)
==================================================
Message.RecipientType.BCC :: 숨은 참조
Message.RecipientType.CC :: 참조
Message.RecipientType.TO :: 받는 사람
==================================================
[Address ] 추상클래스. InternetAddress로 구현해 사용** 주소배열 가능 Address address[] = new Address[2]; address[0] = new InternetAddress(“tingle@pe.kr”); address[0] = new InternetAddress(“aerhee@pe.kr");
==================================================
메일 서버에는 사용자마다 고유 메일 박스가 존재한다.
메일 박스는 해당 사용자의 메시지가 저장되어 있다.
이러한 메일 박스에 접근하는 객체가 Store, Folder
==================================================[Store] :: 메일박스에 접속
Store store = session.getStore(“IMAP”); store.connect(host, username, password); ** 작업 완료후 반드시 close
==================================================
[Folder] :: 메일박스에 있는 폴더에 접속** POP3는 하나의 폴더만을 허용하며 이름은 "INBOX"
** IMAP의 경우 폴더 목록을 읽은 후 특정 폴더 메시지를 가져와야 한다.Folder folder = store.getFolder(“INBOX”); folder.open(Folder.READ_ONLY); Message message[] = folder.getMessage(); ** getMessage() :: 헤더정보만 가져옴.
'Back > JAVA' 카테고리의 다른 글
[JAVA] Apache Tika 아파치 티카 (0) 2020.08.12 [JAVA] API 호출 - HttpURLConnection (0) 2020.07.28 [Serializable - 자바 직렬화] (0) 2020.02.28