Sponsered Links
Categories
Sponsered Links

Mail Reader Servlet

 

This example shows how to read the incomming mail which is in POPUP inbox by using servlet.

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

public class SimpleMailReader{
    public static void main(String args[]){
        try{
            String strServer=args[0];
            String strUser=args[1];
            String strPassword=args[2];
            read(strServer, strUser, strPassword);
        } catch (Exception ex) {
            System.out.println("Usage: java  SimpleMailReader"+" strServer strUser strPassword");
        }
        System.exit(0);
    }
   
    public static void read(String strServer, String strUser, String strPassword){
        Store mystore=null;
        Folder myfolder=null;
        try{
            Properties p = System.getProperties();
            Session session = Session.getDefaultInstance(p, null);
            mystore = session.getStore("pop3");
            mystore.connect(strServer, strUser, strPassword);
            myfolder = mystore.getDefaultFolder();
            if (myfolder == null) throw new Exception("No default folder");
            myfolder = myfolder.getFolder("INBOX");
            if (myfolder == null) throw new Exception("No POP3 INBOX");
            myfolder.open(Folder.READ_ONLY);
            Message[] msg = myfolder.getMessages();
            for (int msgNumber = 0; msgNumber < msg.length; msgNumber++){
                printMessage(msg[msgNumber]);       
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (myfolder!=null) myfolder.close(false);
                if (mystore!=null) mystore.close();
            } catch (Exception ex2) {ex2.printStackTrace();}
        }
    }
   
    public static void printMessage(Message message){
        try{
            String strfrom=((InternetAddress)message.getFrom()[0]).getPersonal();
            if (strfrom==null) strfrom=((InternetAddress)message.getFrom()[0]).getAddress();
            System.out.println("FROM: "+strfrom);
            String strsubject=message.getSubject();
            System.out.println("SUBJECT: "+strsubject);
            Part msgPart=message;
            Object content=msgPart.getContent();
            if (content instanceof Multipart){
                msgPart=((Multipart)content).getBodyPart(0);
                System.out.println("[ Multipart Message ]");
            }
            String contentType=msgPart.getContentType();
            System.out.println("CONTENT:"+contentType);

            if (contentType.startsWith("text/plain") || contentType.startsWith("text/html")){
                InputStream is = msgPart.getInputStream();
                BufferedReader reader=new BufferedReader(new InputStreamReader(is));
                String thisLine=reader.readLine();
                while (thisLine!=null){
                    System.out.println(thisLine);
                    thisLine=reader.readLine();
                }
            }
            System.out.println("-----------------------------");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } 
}

 
 
Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved