JAVA MAIL API

JavaMail Projects

JavaMail Project 1

Fetching Mail Using JavaMailAPI
Previous Home Next

For Reading mail you get a session get and connect to your appropriate store for your mailbox, then pen your appropriate folder , and get your message. when totally operation perform then don't forget to close to connection.

package R4R;
import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.activation.*;
public class FetchMsg 
{
public static void main(String args[]) throws Exception
{
  String host="192.168.10.255";
  String username = "r4r@localhost";
  String password = "r4r@localhost"; 
  // 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);
    BufferedReader reader = new BufferedReader (
		  new InputStreamReader(System.in));
       // Get directory

 

Message message[] = folder.getMessages(); 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("Do you want to read message? " + "[YES to read/QUIT to end]"); String line = reader.readLine(); { if ("YES".equals(line)) message[i].writeTo(System.out); else if ("QUIT".equals(line)) break; } } }}
Previous Home Next