일반 JavaMail - Connecting Gmail pop3 server.

황제낙엽 2018.08.20 08:54 조회 수 : 1217

sitelink1 https://sites.google.com/site/parn755/home/java 
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  

I tried to find out how to connect gmail server and retrieve mails but most information seem to be too old/ambigous or they do not show complete code. 

So I'm trying to make valid sources. Below code just makes connection to pop3 server.

 

 

 

referenced links:

https://javamail.java.net/nonav/docs/JavaMail-1.5.pdf : Javamail API document ver 1.5

http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes : Javamail FAQ

 

 

 

 

 

Source: 

 

import java.util.Properties;

import javax.mail.Folder;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Store;

import javax.mail.URLName;

 

public class ReadTest {

 static String protocol = "pop3";

 static String host = "pop.gmail.com";

 static String user = "gmail ID";

 static String password = "password";

 static String port = "995";

 static String url = null;

 static String root = null;

 static String pattern = "%";

 static boolean recursive = false;

 static boolean verbose = false;

 static boolean debug = true;

 

 public static void main(String[] args) {

  // Get a Properties object

  Properties props = System.getProperties();

  

  // Let's check out what's in default Properties

  System.out.println(props.toString());

  

  //Before opening session, set port number and ssl authentication

  props.put("mail.pop3.port", "995");

  props.put("mail.pop3.ssl.enable", "true");

  

  //properties are added correctly?

  System.out.println(props.toString());

  

  // Get a Session object

  Session session = Session.getInstance(props, null);

  

  // let's see what's going on by setting debug as true

  session.setDebug(debug);

  // Get a Store object

  Store store = null;

  Folder rf = null;  

  

  

  try {

   if (url != null) {

    URLName urln = new URLName(url);

    store = session.getStore(urln);

    store.connect();

   } else {

    if (protocol != null) {

     store = session.getStore(protocol);

    }

    else {

     store = session.getStore();    

    }

    // Connect

    if (host != null || user != null || password != null) {   

     store.connect(host, user, password);

    }

    else {

     store.connect();

    }

   }

  } catch (MessagingException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

 

}

 

 

 

 

 

 

Result (omitted properties values)

 

DEBUG: setDebug: JavaMail version 1.5.0

DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]

DEBUG POP3: mail.pop3.rsetbeforequit: false

DEBUG POP3: mail.pop3.disabletop: false

DEBUG POP3: mail.pop3.forgettopheaders: false

DEBUG POP3: mail.pop3.cachewriteto: false

DEBUG POP3: mail.pop3.filecache.enable: false

DEBUG POP3: mail.pop3.keepmessagecontent: false

DEBUG POP3: mail.pop3.starttls.enable: false

DEBUG POP3: mail.pop3.starttls.required: false

DEBUG POP3: mail.pop3.apop.enable: false

DEBUG POP3: mail.pop3.disablecapa: false

DEBUG POP3: connecting to host "pop.gmail.com", port 995, isSSL true

+OK Gpop ready for requests from 61.75.27.199 yw8pf6744109pac.0

CAPA

+OK Capability list follows

USER

RESP-CODES

EXPIRE 0

LOGIN-DELAY 300

TOP

UIDL

X-GOOGLE-VERHOEVEN

X-GOOGLE-RICO

.

DEBUG POP3: authentication command trace suppressed

DEBUG POP3: authentication command succeeded

 

 

번호 제목 글쓴이 날짜 조회 수
266 한글 인코딩의 이해 2편: 유니코드와 Java를 이용한 한글 처리 file 황제낙엽 2019.05.07 830
265 응답 헤더의 Content-disposition 속성 황제낙엽 2019.04.16 1048
264 StringUtils - 문자열 처리 유틸리티 file 황제낙엽 2019.04.15 661
263 File.length() 에 대하여 황제낙엽 2019.03.24 910
262 File.delete() 와 File.deleteOnExit() 황제낙엽 2019.03.24 3077
261 List to Array / Array to List 황제낙엽 2019.03.24 576
260 Oracle JAVA 유료화에 관련한 최신 기사 황제낙엽 2019.01.23 643
259 Iterator.next() - NoSuchElementException 황제낙엽 2018.10.28 802
258 OracleJDK 유료화 FAQ (Oracle Java 의 유료화에 대한 어느분의 정리) 황제낙엽 2018.10.11 662
257 메일서버(daum.net)에 POP3를 이용하여 메일 가져오기 예제 file 황제낙엽 2018.10.09 1609
256 Sending mail through Java using SMTP of gmail file 황제낙엽 2018.09.13 1088
255 Read or get mails using pop in java (using gmail) file 황제낙엽 2018.09.13 11924
254 Collections.sort() , Comparator 황제낙엽 2018.08.23 746
» JavaMail - Connecting Gmail pop3 server. 황제낙엽 2018.08.20 1217
252 JavaMail - 네이버 메일 수신하기(POP3) 황제낙엽 2018.08.20 1793
251 JavaMail - POP3로 메일 읽어오기 - 단순샘플 황제낙엽 2018.08.20 723
250 [HttpURLConnection, HttpsURLConnection] Response 로 받은 데이터가 압축되어 있는 경우(gzip, deflate) 황제낙엽 2018.08.16 891
249 [HttpURLConnection, HttpsURLConnection] 자바 Http / https 의 결과를 주고받을때 세션을 유지 황제낙엽 2018.08.12 804
248 [HttpURLConnection] 자바(Java) URL 접속 및 세션 관리 file 황제낙엽 2018.08.12 779
247 org.apache.commons.io.FilenameUtils (getExtension) 황제낙엽 2018.04.01 1887