일반 JavaMail - Connecting Gmail pop3 server.

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

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

 

 

번호 제목 글쓴이 날짜 조회 수
347 java.util.Optional 에서의 map() 함수의 이해 황제낙엽 2026.06.24 666
346 linux(ubuntu) 의 jdk 21 이 설정된 환경에서 jdk 17 로 다운그레이드 절차 황제낙엽 2025.09.22 1532
345 [ChatGPT] javax.* 패키지와 jakarta.* 패키지의 혼합 사용 방안 황제낙엽 2025.04.15 2459
344 [ChatGPT] *.servlet.http.HttpServlet 클래스를 사용할 수 있는 주요 WAS 목록 황제낙엽 2025.04.14 2038
343 [ChatGPT] JDBC API에서 java.sql과 javax.sql 패키지의 차이점 황제낙엽 2025.01.31 2218
342 jakarta.servlet 패키지를 이용한 File Upload 구현 (2024.03.03, Servlet 5.0) 황제낙엽 2025.01.02 2310
341 [ChatGPT] Java EE가 Jakarta EE로 전환 황제낙엽 2025.01.01 2525
340 [ChatGPT] File.delete() 함수로 파일 삭제가 되지 않는 경우 황제낙엽 2024.12.10 2809
339 [ChatGPT] <jsp:include>에서 flush="true"를 설정시의 몇 가지 문제점 과 대안 황제낙엽 2024.07.19 2493
338 [Copilot] JSP, Servlet, web.xml, mysql 한글 설정 황제낙엽 2024.07.12 2555
337 Base64 클래스의 encode, decode 에서 URL-safe 황제낙엽 2024.07.10 2624
336 [Copilot] JSP 에서 다른 페이지를 현재 페이지에 포함시키는 문법 (include) 황제낙엽 2024.06.26 2570
335 Exception 출력에 대한 고찰 황제낙엽 2024.06.10 2198
334 google-auth-library-oauth2-http 라이브러리 다운로드 황제낙엽 2023.11.19 4025
333 firebase-admin-java 라이브러리 다운로드 (firebase admin sdk library) 황제낙엽 2023.11.19 2805
332 Enum 활용 (개인블로그, Effective Java) file 황제낙엽 2023.11.02 2759
331 Enum 활용 (우아한기술블로그) file 황제낙엽 2023.11.02 2790
330 servlet name 에 camel case 를 사용해서는 안되는 이유 황제낙엽 2023.10.24 2508
329 org.apache.commons.fileupload.servlet.ServletFileUpload 이용 예시 황제낙엽 2023.08.25 3336
328 ServletFileUpload.parseRequest() 실행 결과 리스트가 비어있는 오류 황제낙엽 2023.08.25 2935