Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java mail recieve 1

Status
Not open for further replies.

fuadhamidov

Programmer
Sep 22, 2003
98
0
0
TR
hi

i need an example code about how i can get mail from mail server. i have found a number examples for java send mail but no one for java get mail
 
This one is for POP3 :


but the pronciple is the same for IMAP. Remember though that for IMAP, you have to 'ping' the server to get new messages, so I expect you will want to implement the MessageCountListener and keep calling getMessageCount(), and then retrieving the new mails using the MessageCountEvent.getMessages() call.

You may want to look at also.
 
From "Java 2 Network Protocols. Black Book" (Al Williams) from Coriolis

POP3 :
Code:
// JavaMail Demo -- Williams
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class JMailDemo {
    public static void main(String args[]) throws Exception {
	String host = args[0];
	String username = args[1];
	String password = args[2];

	// Create empty properties
	Properties props = new Properties();
	
	// Get session
	Session session = Session.getDefaultInstance(props, null);

	// Get the store
	Store store = session.getStore("pop3");
	store.connect(host, username, password);

	// Get folder
	Folder folder = store.getFolder("INBOX");
	folder.open(Folder.READ_ONLY);

	// Get directory
	Message message[] = folder.getMessages();

	// print first sender, subject, and message
	for (int i=0, n=message.length; i<n; i++) {
	    System.out.println(i + ": " + message[i].getFrom()[0] 
			       + "\t" + message[i].getSubject());
	    System.out.println(message[i].getContent().toString());
	}

	// Close connection 
	folder.close(false);
	store.close();
    }
}
IMAP :
Code:
// JavaMail Demo -- Williams
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class IMapMailDemo {
    public static void main(String args[]) throws Exception {
	String host = args[0];
	String username = args[1];
	String password = args[2];

	// Create empty properties
	Properties props = new Properties();
	
	// Get session
	Session session = Session.getDefaultInstance(props, null);

	// Get the store
	Store store = session.getStore("imap");
	store.connect(host, username, password);

	// Get folder
	Folder folder = store.getFolder("INBOX");
	folder.open(Folder.READ_ONLY);
	// print sub folders
	Folder [] folders = folder.list();
	if (folders.length==0) System.out.println("No sub folders");
	for (int ij=0;ij<folders.length;ij++) {
	    System.out.println("Subfolder: " + folders[ij].getFullName());
	}


	// Get directory
	Message message[] = folder.getMessages();
	if (message.length==0) System.out.println("No mail");
	// print first sender, subject, and message
	for (int i=0, n=message.length; i<n; i++) {
	    System.out.println(i + ": " + message[i].getFrom()[0] 
			       + "\t" + message[i].getSubject());
	    System.out.println(message[i].getContent().toString());
	}

	// Close connection 
	folder.close(false);
	store.close();
    }
}
 
i have two problem

1.
Code:
System.out.println(message[i].getContent().toString());
gives
javax.mail.internet.MimeMultipart@196c1b0,
not content

2.i put "fetching mail" in a method which returns message, so Folder and Store are closed at the end of method. It is interesting if i disable
Code:
System.out.println(i + ": " + message[i].getFrom()[0] 
                   + "\t" + message[i].getSubject());
System.out.println(message[i].getContent().toString());
statement in the FetchMail() method, i get following message

java.lang.IllegalStateException: Folder is not Open


i can't understand why, but i have tried several times. is reason that Message not be invoked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top