sitelink1  
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  

업무진행중 작성한 실제 구동 가능한 예제 프로그램

daum.net 메일서버에 pop3로 접속하여 긴급이라는 제목의 메일들을 가져온다

 

 

import java.util.Enumeration;

import java.util.Properties;

 

import javax.mail.Authenticator;

import javax.mail.Folder;

import javax.mail.Header;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.NoSuchProviderException;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Store;

import javax.mail.internet.MimeMultipart;

 

public class TestGetPOPMails {

    private static final String MAIL_POP_HOST = "pop.daum.net";

    private static final String MAIL_STORE_TYPE = "pop3";

    private static final String POP_USER = ""; //아이디 (user@DomainName.com)

    private static final String POP_PASSWORD = "";  //비밀번호

    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++) {

            for (int i = messages.length - 1; i > -1; i--) {

                Message message = messages[i];

 

                if (message.getSubject().indexOf("긴급") > -1) {

                    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("Date: " + message.getHeader("Date")[0]);

                    System.out.println("Body: " + getTextFromMessage(message));

                }

            }

 

            // 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 String getTextFromMessage(Message message) throws MessagingException, IOException {

        String result = "";

        if (message.isMimeType("text/plain")) {

            result = message.getContent().toString();

        } else if (message.isMimeType("multipart/*")) {

            MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();

            result = getTextFromMimeMultipart(mimeMultipart);

        }

        return result;

    }

 

    public static String getTextFromMimeMultipart(MimeMultipart mimeMultipart)  throws MessagingException, IOException{

        String result = "";

        int count = mimeMultipart.getCount();

        for (int i = 0; i < count; i++) {

            BodyPart bodyPart = mimeMultipart.getBodyPart(i);

            if (bodyPart.isMimeType("text/plain")) {

                result = result + "\n" + bodyPart.getContent();

                break; // without break same text appears twice in my tests

            } else if (bodyPart.isMimeType("text/html")) {

                String html = (String) bodyPart.getContent();

                result = result + "\n" + org.jsoup.Jsoup.parse(html).text();

            } else if (bodyPart.getContent() instanceof MimeMultipart){

                result = result + getTextFromMimeMultipart((MimeMultipart)bodyPart.getContent());

            }

        }

        return result;

    }

 

    public static void main(String[] args) {

 

        getMails(POP_USER, POP_PASSWORD);

 

    }

}

 
 
관련 패키지는 첨부파일 다운로드
번호 제목 글쓴이 날짜 조회 수
33 File 클래스 정리 황제낙엽 2019.07.29 91
32 파일 사이즈를 반환하는 유틸 함수 황제낙엽 2019.07.29 120
31 BufferedReader, BufferedWriter를 활용한 빠른 입출력 황제낙엽 2019.07.29 77
30 File.length() 에 대하여 황제낙엽 2019.03.24 221
29 File.delete() 와 File.deleteOnExit() 황제낙엽 2019.03.24 1887
» 메일서버(daum.net)에 POP3를 이용하여 메일 가져오기 예제 file 황제낙엽 2018.10.09 940
27 org.apache.commons.io.FilenameUtils (getExtension) 황제낙엽 2018.04.01 1209
26 File 을 다루기 위한 유틸 클래스 file 황제낙엽 2018.02.28 99
25 HttpsURLConnection 을 사용한 SSL서버 접속 file 황제낙엽 2017.08.02 231
24 HttpURLConnection 사용 샘플( JSP , SERVLET ) 황제낙엽 2017.08.01 254
23 HttpURLConnection 사용하기 황제낙엽 2017.08.01 393
22 [HttpURLConnection] POST로 파라미터 넘기기 황제낙엽 2017.08.01 507
21 HttpURLConnection POST 방식 사용하기 황제낙엽 2017.08.01 370
20 servlet 에서의 json 한글처리 황제낙엽 2013.04.23 1519
19 com.oreilly.servlet.multipart 를 이용한 파일 업로드 file 황제낙엽 2013.03.19 104
18 Jar파일에 포함된 리소스 접근하는 방법(How to read a resource from a JAR file ) file 황제낙엽 2012.06.24 164
17 16비트 CRC 체크용 클래스 (사용자 클래스) 황제낙엽 2010.03.14 406
16 파일을 읽어서 CRC 값을 연산하는 메서드 (java.util.zip.CRC32) 황제낙엽 2010.03.14 137
15 byte배열에 대한 CRC 를 계산하는 메서드 (java.util.zip.CRC32) 황제낙엽 2010.03.14 2166
14 org.apache.commons.fileupload.servlet.ServletFileUpload 를 이용한 파일 업로드 file 황제낙엽 2009.11.19 129