Debugging JavaMail exceptions

Hi All,

I am not very good at Java programming, but back then I coded the litlle program below to read email messages from a Public Folder on my exchange server and it was working fine since then. Suddenly it stopped working and when I execute it, I get the below error stack.

create or replace and compile

java source named"Branchmonitoring"

as

import java.text.*;

import java.util.*;

import java.io.*;

import javax.mail.*;

import javax.mail.UIDFolder;

import javax.mail.FetchProfile;

import javax.mail.Folder;

import javax.mail.Message;

import javax.mail.Session;

import javax.mail.Store;

import javax.mail.event.*;

import javax.activation.*;

import oracle.sql.*;

import oracle.sqlj.runtime.Oracle;

publicclass Branchmonitoring{

publicstaticfinal Locale US =new Locale("en","US","");

publicstatic oracle.sql.NUMBER

monitoralarm(String email_host

,String email_usr

,String email_pwd

,String email_box

,longmsglastUID)throws MessagingException

{

int rc = 0;

int mcount = 0;

try{

Properties props = System.getProperties();

// Get a Session object

Session session = Session.getInstance(props,null);

// session.setDebug(true);

// Get a Store object

Store store = session.getStore("imap");

// Connect

store.connect(email_host, email_usr, email_pwd);

// Open the Folder

Folder folder = store.getFolder(email_box);

if (folder ==null || !folder.exists()){

System.out.println("Invalid folder!");

System.exit(1);

}

if(!(folderinstanceof UIDFolder)){

System.out.println(

"This provider or this folder does not support UIDs. The program will exit now.");

System.exit(1);

}

UIDFolder ufolder = (UIDFolder)folder;

folder.open(Folder.READ_ONLY);

int numofMessages = folder.getMessageCount();

if (numofMessages == 0){

System.out.println("Empty folder.");

folder.close(false);

store.close();

System.exit(1);

}

// Get emails with the UIDs

Message[] msgs = ufolder.getMessagesByUID(msglastUID + 1, UIDFolder.LASTUID);

String msg_subj ="";

String msg_sent ="";

String sender="";

SimpleDateFormat formatter;

long uidMsg = 0;

formatter =new SimpleDateFormat("MM.dd.yyyy H:mm:ss", US);

int k=0;

for (int i = 0; i < msgs.length; i++){

k=i;

// get the subject

try{

msg_subj = msgs[i].getSubject();

}catch(Exception e){

msg_subj ="UNKNOWN";

}

// get the sender

try{

sender = msgs[i].getFrom()[0].toString();

}catch(Exception e){

sender ="UNKNOWN";

}

try{

uidMsg = ((UIDFolder)folder).getUID(msgs[i]);

}catch(Exception e){

uidMsg = 0;

}

//insert into oracle Dw_alarms_tmp table

try{

#sql{INSERT INTO dw_alarms_tmp(

AL_UID

, AL_FROM

, AL_TEXT

, AL_DATE) VALUES(

:IN( uidMsg )

, :IN( sender )

, :IN( msg_subj )

, :IN( formatter.format(msgs[i].getSentDate()) )

)};

#sql{ COMMIT};

}catch(Exception e){

;

}

}

folder.close(false);

store.close();

}catch (Exception ex){

System.out.println("Unhandled exception : " + ex.getMessage());

ex.printStackTrace();

System.exit(1);

}

finally

{

returnnew oracle.sql.NUMBER(mcount);

}

}

}

/

Do not pay attention to "create or replace and compile... " part, it is something that I have to use to embed this java code into Oracle database.

The error message is:

Unhandled exception : null

java.lang.NullPointerException

at javax.mail.Flags.<init>(Flags.java:163)

at com.sun.mail.imap.protocol.MailboxInfo.<init>(MailboxInfo.java:80)

at com.sun.mail.imap.protocol.IMAPProtocol.examine(IMAPProtocol.java:351)

at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:752)

at Branchmonitoring.monitoralarm(Branchmonitoring:60)

I coded "Unhandled exception" because I don't know which error to trap.

*** My question is, how can show the real error message to troubleshoot the problem or is this the only error message I can get? Maybe some of you will figure out the what the problem is by just loooking at the error stack, but I could not. When I wanted to print the error message it gives me "null".

Any help would be greatly appreciated. Thanks for your help in advance.

Thanks,

Sinan Topuz

[8193 byte] By [Sin@na] at [2007-11-26 15:52:30]
# 1

I have found my answer in the JavaMail FAQ. Thanks to everybody who took time to even view the thread.

I even had the line commented in my code... Like I said, it has been a long time...

I just needed to uncomment the line below and I was able to see the reason on the console.

// session.setDebug(true);

Sinan Topuz

Sin@na at 2007-7-8 22:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...