Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading mails from Hmail Server

843834Apr 21 2009 — edited Apr 23 2009
hi all,

I am using an java mail api to read mails from a hmail server.I was successfull in reading mails and analysing it further but the problem that i face is my program could read only the recent mails ,all the remaining unread mails were not accessible from the java code.......i am attaching my code...

package mail.hmailserver;
import java.io.*;
import java.util.*;

import javax.mail.*;

public class ReadMail_frm_hmailserver {
int c=0;

@SuppressWarnings("unchecked")
public static void main(String args[]) throws Exception {

ReadMail_frm_hmailserver mailObj=new ReadMail_frm_hmailserver();
mailObj.readMails();

}

public Vector[] readMails() throws Exception
{
String host = "172.29.19.78";
String user = "user1@infodata.com";
String password = "testuser1";


// 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("pop3");

//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);
//folder.open(Folder.READ_WRITE);

Message[] message = folder.getMessages();
// String [] contents=new String[10];

Vector [] contents = new Vector[message.length];//creating array of vectors to store mails
// Vector contents1=new Vector();


System.out.println("No of mails : " + message.length);
System.out.println("subject 1 : "+message[0].getSentDate());
System.out.println("subject 2 : "+message[0].getSubject());
// System.out.println("subject 3 : "+message[2].getSubject());
// System.out.println("subject 4 : "+message[3].getSubject());
//System.out.println("-----------------");
// Display message.
for (int i = 0; i < message.length; i++) {
c=0;
contents[i] = new Vector(); //since vector array is an array of arrays we hav to initilize each of the content vectors
System.out.println("------------ Message " + (i + 1) + " ------------");

System.out.println("SentDate : " + message.getSentDate());
System.out.println("From : " + message[i].getFrom()[0]);
System.out.println("Subject : " + message[i].getSubject());
System.out.print("Message : ");


//System.out.println(message[i].getContent());
Multipart multipart = (Multipart) message[i].getContent();
//System.out.println(multipart.getCount());
String line = null;
for (int j = 0; j < multipart.getCount(); j++) {
//System.out.println(i);
//System.out.println(multipart.getContentType());
BodyPart bodyPart = multipart.getBodyPart(j);
InputStream stream = bodyPart.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(stream));

while (br.ready()) {
try {
//line.append(br.readLine());
//line=line+"\n"+br.readLine();

line=br.readLine();
if(line.equals("NOTICE")){
System.out.println("entered-----------------------------exit loop");
//exit();
c++;
break;
}
if(c==0)
{
contents[i].add(line); //adding the mails line by line to the array
System.out.println(line);
}
//System.out.println(contents[i].get(j));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Exception : "+e);
}

//contents[1].add(line);

}


System.out.println();
}
//System.out.println(line);
// contents[i].add(line); //adding the mails line by line to the array
System.out.println();
}



// folder.close(true);
//store.close();



//System.out.println(contents.length);
//System.out.println(contents[0].get(0));
//System.out.println(contents[0].);
return contents;
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 21 2009
Added on Apr 21 2009
6 comments
1,348 views