import java.util.*;
import lotus.domino.*;

public class getMyDomMail{
	public static void main(String argv[]){
		try{
			// test and get args
			if (argv.length < 4){
				// Not all arguments were entered. give user the variables
				System.out.println("You need to enter 4 arguments for this application...");
				System.out.println("   server, Domino directory, user, password");
				System.out.println("");
				System.out.println("Note: Most company's Domino Directory is names.nsf");
				System.exit(0);
			}			
			String server = argv[0];
			String database = argv[1];
			String user = argv[2];
			String pass = argv[3];

			// start bubblegum wrapper and start session
			NotesThread.sinitThread();
			Session s = NotesFactory.createSession(server, user, pass);

			//get server, and Domino Directory database
			String servername=s.getServerName();
			if(servername==""){
				System.out.println("Could not attach to server");
				System.exit(0);
				}
			System.out.println("Attached to Server: " + servername);
			Database db = s.getDatabase(servername, database);
			if(db.isOpen()==false){
				System.out.println("Could not open database: " + database);
				System.exit(0);
			}
			System.out.println("Attached to Domino Directory: " + db.getTitle());

			//get the Name of this user so we can convert to abbreviated format and get the user from the directory
			Name uName = s.getUserNameObject();
      			String uAbbr = uName.getAbbreviated();

			// get this user's person document
			View dV = db.getView("($VIMPeople)");
			if (dV == null){
				System.out.println("Sorry. I am unable to get your person document to get to your mailfile.");
				System.exit(0);
			} 
			Document dP = dV.getDocumentByKey(uAbbr);
			if (dP == null){
				System.out.println("Sorry. I am unable to get your person document to get to your mailfile.");
				System.exit(0);
			} 
			// we have the person document, now we need the server and mailfile fields for this person
			String mailserver = dP.getItemValueString("MailServer");
			String mailfile = dP.getItemValueString("MailFile");
			//System.out.println("Your mail server is: " + mailserver + ", and your mail file location is: " + mailfile);
			
			// now lets test to make sure that mail file is on this server
			if(servername.equalsIgnoreCase(mailserver)==false){
				System.out.println("Sorry, you mail server is: " + mailserver + ". Please connect to that server instead to read your mail.");
				System.exit(0);
			}
			
			
			// proceed to get mail file and inbox
			Database mailDb = s.getDatabase(servername, mailfile);
			if(mailDb.isOpen()==false){
				System.out.println("Could not open your mail database: " + mailfile);
				System.exit(0);
			}
			System.out.println("Attached to Your mail file: " + mailDb.getTitle());
			View inbox = mailDb.getView("($Inbox)");
			if(inbox == null){
				System.out.println("Sorry. I am unable to get in your inbox.");
				System.exit(0);
			} 

			
			// get collection of entries/docs that are new mail
			ViewEntryCollection vec = inbox.getAllEntries();
			ViewEntry entry = vec.getLastEntry();

			// get today's date to compare with inbox document
			DateTime testDay = s.createDateTime("Today 12:00:00 AM");
			//roll now date back one day
			//testDay.adjustDay(-1, true); // subtracts 24 hrs, change for yesterday or change today above to yesterday


			// dim compare date from entry
			DateTime compareDay;

			
      			while (entry != null) {
				// now compare the entry to the test date
				//entry.getColumnValues().elementAt(2) is the date column
				
				if(entry.getColumnValues().elementAt(2).getClass().getName().endsWith("DateTime")) {
            				compareDay = (DateTime) entry.getColumnValues().elementAt(2);
				
					if( testDay.timeDifference(compareDay)<0) {
						// present this email to the user
						System.out.println("");
						System.out.println("___________________________________________");
						System.out.println("");
						System.out.println("Received: " + compareDay.getLocalTime());	
						System.out.println("");
						System.out.println("Mail from: " + entry.getColumnValues().elementAt(1));
						System.out.println("Subject: " + entry.getColumnValues().elementAt(5));
						System.out.println("Body:");
						RichTextItem body = (RichTextItem)entry.getDocument().getFirstItem("Body");
        					System.out.println(body.getFormattedText(false, 0, 0) + "\n");
						System.out.println("___________________________________________");
					}
				}
        			entry = vec.getPrevEntry();
			}
			

		}
		catch (Exception e){
		if (e instanceof NotesException){
			NotesException ne=(NotesException)e;
			System.out.println("Domino-side error:\n" + ne.text + "\n");
		}
		e.printStackTrace();
		}
		finally {
		NotesThread.stermThread();
		}
	}
}	