views:

334

answers:

4

Hi I want to write a java program where I will provide my email id and password. and I want to read all new unread messages that arrived to that email id. I donot know how to write program for that.

The below program works fine for gmail. but it does not work for yahoomail because for yahoo pop3 is not configured. I want a generic code which will work for all email id.

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

public class ReadMail {

    public static void main(String args[]) throws Exception {



//        String host = "pop.gmail.com";
//        String user = "xyz";
//        String password = "12345";

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

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties, null);

        // Get a Store object that implements the specified protocol.
        Store store = session.getStore("pop3s");

        //Connect to the current host using the specified username and password.
        store.connect(host, user, password);

        //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.print("Message : ");

            InputStream stream = message[i].getInputStream();
            while (stream.available() != 0) {
                System.out.print((char) stream.read());
            }
            System.out.println();
        }

        folder.close(true);
        store.close();
    }
}
A: 

You need the javax.mail package, and the documentation of it. Read the documentation. Then you know.

Daniel
I read it but i am not clear still now
Deepak
A: 

You need to know more than just login-pass. Things like mail server address, mail server type, port for connections, etc. You should probably check out Java Mail API, or Commons Email.

UPD:

You create a Session using Session.getDefaultInstance() method (which takes connection Properties object and authenticator), get a Store from this Session using Session.getStore() method, get a Folder from that store using Store.getFolder("FOLDER_NAME") method, open that Folder, using Folder.open(Folder.READ) method, and get all messages, using something like Message[] messages = inboxFolder.getMessages();

Is that what you were looking for?

UPD2:

There is simply no way to write a generic program, which will work with any mail provider, using just server path, userID and password. Because different mail servers are configured differently. They talk differen protocols (imap/pop3/pop3 ssl) on different ports. There's always some guy, who has configured his mail server to talk imap over ssl on 31337 port only, all the other ports and protocols are banned. And this guy breaks your program. So, you'll have to specify all this properties in your properties object. Look here for properties, you'll have to specify.

UPD3:

On second thought, you actually have one option. Just try connecting to the server using different protocols. If that does not help, start iterating through ports. The one that fits is your configuration. If that's really what you want.

folone
A: 

There are two ways to do it:

1) Google provides API's to access mail you could use that library which provides more control over your mails. See here: http://code.google.com/apis/gmail/. In the same way try for other email providers.

2) Simple mail client(you could find it easily googling), but you need to look at headers to identify which mails are read/unread etc.

Phani
A: 

You need a registry where you can get the properties for a given mail service.

For instance, instead of specifying a pop3 host, you could specify the name of a .properties file that would contain the host, the port, the protocol, etc...

If your .properties file contains the protocol, for instance mail.store.protocol=pop3, you could use session.getStore() (with no argument), and the same code could be used for pop3, imap, pop3s, imaps.

Maurice Perry