sitelink1 http://www.nikhilnishchal.com/tech-blogs/-/blogs/read-mail 
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  

To read emails from any mailbox generally required many times.
By following code using POP3 we can read mails of any mailbox.
Following codes describes to read mails from gmail(you need to provide proper user and password).
To, read mails from gmail/email client we also need to update its setting to enable pop. In gmail from mail setting we can enable it.

package com.popmails;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

/**
 * 
 * @author nikhil.nishchal
 *
 */
public class GetPOPMails {

	private static final String MAIL_POP_HOST = "pop.gmail.com";
	private static final String MAIL_STORE_TYPE = "pop3";
	private static final String POP_USER = "xxxxxx@gmail.com";
	private static final String POP_PASSWORD = "xxxxx";
	private static final String POP_PORT = "995";
	
	public static void getMails(String user,String password) {
		try {

			// create properties field
			Properties properties = new Properties();
			properties.put("mail.pop3.host", MAIL_POP_HOST);
			properties.put("mail.pop3.port", POP_PORT);
			properties.put("mail.pop3.starttls.enable", "true");
			properties.put("mail.pop3.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
			//Session emailSession = Session.getDefaultInstance(properties);
			Session emailSession = Session.getDefaultInstance(properties , new Authenticator() {
			    @Override
			            protected PasswordAuthentication getPasswordAuthentication() {
			                return new PasswordAuthentication( POP_USER , POP_PASSWORD);
			            }
			});
			// create the POP3 store object and connect with the pop server
			Store store = emailSession.getStore(MAIL_STORE_TYPE);

			store.connect(MAIL_POP_HOST, user, password);

			// create the folder object and open it
			Folder emailFolder = store.getFolder("INBOX");
			emailFolder.open(Folder.READ_ONLY);

			// retrieve the messages from the folder in an array and print it
			Message[] messages = emailFolder.getMessages();
			System.out.println("messages.length---" + messages.length);

			for (int i = 0, n = messages.length; i < n; i++) {
				Message message = messages[i];
				System.out.println("---------------------------------");
				System.out.println("Email Number " + (i + 1));
				System.out.println("Subject: " + message.getSubject());
				System.out.println("From: " + message.getFrom()[0]);
				System.out.println("Body: " + message.getContent().toString());

			}

			// close the store and folder objects
			emailFolder.close(false);
			store.close();

		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		getMails(POP_USER, POP_PASSWORD);

	}

}

For this, we need Java mail API.
Java mail API we can download from http://www.oracle.com/technetwork/java/javamail/index-138643.html

 

 

 

* javax.mail.AuthenticationFailedException: [AUTH] Username and password not accepted. 오류 발생시 보안 수준이 낮은 앱에서 계정에 액세스하도록 허용해야 한다

  -> https://myaccount.google.com/lesssecureapps

 

 

번호 제목 글쓴이 날짜 조회 수
271 [HttpURLConnection, HttpsURLConnection] 코드참조용 샘플프로젝트 secret 황제낙엽 2019.01.18 0
270 Iterator.next() - NoSuchElementException 황제낙엽 2018.10.28 344
269 OracleJDK 유료화 FAQ (Oracle Java 의 유료화에 대한 어느분의 정리) 황제낙엽 2018.10.11 128
268 메일서버(daum.net)에 POP3를 이용하여 메일 가져오기 예제 file 황제낙엽 2018.10.09 940
267 Sending mail through Java using SMTP of gmail file 황제낙엽 2018.09.13 163
» Read or get mails using pop in java (using gmail) file 황제낙엽 2018.09.13 861
265 Collections.sort() , Comparator 황제낙엽 2018.08.23 48
264 JavaMail - Connecting Gmail pop3 server. 황제낙엽 2018.08.20 206
263 JavaMail - 네이버 메일 수신하기(POP3) 황제낙엽 2018.08.20 1413
262 JavaMail - POP3로 메일 읽어오기 - 단순샘플 황제낙엽 2018.08.20 107
261 [HttpURLConnection, HttpsURLConnection] Response 로 받은 데이터가 압축되어 있는 경우(gzip, deflate) 황제낙엽 2018.08.16 254
260 [HttpURLConnection, HttpsURLConnection] 자바 Http / https 의 결과를 주고받을때 세션을 유지 황제낙엽 2018.08.12 77
259 [HttpURLConnection] 자바(Java) URL 접속 및 세션 관리 file 황제낙엽 2018.08.12 109
258 json-rpc 에서 한글 문제 황제낙엽 2018.08.08 175
257 org.apache.commons.io.FilenameUtils (getExtension) 황제낙엽 2018.04.01 1209
256 이미지 파일의 화면사이즈와 포맷(확장자) 구하기 황제낙엽 2018.04.01 472
255 File 을 다루기 위한 유틸 클래스 file 황제낙엽 2018.02.28 99
254 상수의 데이터 타입 황제낙엽 2018.01.26 33
253 Java에서 User-Agent 파써 사용하기 황제낙엽 2017.11.20 418
252 현재 월,일,시간,분,초 등등 가져오기 황제낙엽 2017.11.02 858