/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.*;
import javax.mail.*;

public class MailAuthenticator extends Authenticator {

    private String username,  password;

    MailAuthenticator(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public void readMail() throws Exception {

        // Get system properties
        Properties properties = System.getProperties();

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties, this);
        URLName url = new URLName("pop3://test@192.168.10.205");

        // Get a Store object for the given URLName.
        Store store = session.getStore(url);
        store.connect();

        //Create a Folder object corresponding to the given name.
        Folder folder = store.getFolder("inbox");

        // Open the Folder.
        folder.open(Folder.READ_ONLY);

        Message[] message = folder.getMessages();

        // Display message.
        for (int i = 0; i < message.length; i++) {
            System.out.println("------------ Message " + (i + 1) + " ------------");
            System.out.println("SentDate : " + message[i].getSentDate());
            System.out.println("From : " + message[i].getFrom()[0]);
            System.out.println("Subject : " + message[i].getSubject());
            System.out.println("Content : " + message[i].getContent());
        }
        folder.close(true);
        store.close();
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }

    public static void main(String args[]) throws Exception {
        new MailAuthenticator("test", "test").readMail();
    }
}