2
votes

In my project I am attempting to read lotus notes email in the Inbox. Right now I have two emails in my inbox, each email has an attachment, and I am trying to open each email and download the attachment from that email.

The issue I am having now is that my program is only reading the first email.

try
    {
        NotesThread.sinitThread();

        Session session = NotesFactory.createSession();
        Database database = session.getDatabase("servername", "mailbox link");
        View view = database.getView("$Inbox");
        Document document = view.getFirstDocument();

        while(document != null)
        {

            RichTextItem Body = (RichTextItem)document.getFirstItem("Body");
            Vector vector = Body.getEmbeddedObjects();
            Enumeration iterator = vector.elements();

            while (iterator.hasMoreElements())
            {
              EmbeddedObject eo = (EmbeddedObject)iterator.nextElement();
              if(eo.getType()==EmbeddedObject.EMBED_ATTACHMENT)
                {
                    attachment = eo.getName();
                    eo.extractFile("destination" + eo.getName());
                    eo.remove();
                }

            }//end of inner while


            logger.info("Received email and downloaded attachment");
            document = view.getNextDocument(document);

        } //end of outer while



    } 
    catch(NotesException nex)
    {
       logger.severe("Exception in INotes connection");
    }
    finally
    {
        NotesThread.stermThread();

    }

I thought the line -- document = view.getNextDocument(document)

would open the next email in the Inbox. But I am only getting the first email as the result.

Appreciate any pointers.

2

2 Answers

1
votes

This code works for me. Though written for Notes 8 it also works on Notes 9.

try
    {
        Session s = NotesFactory.createSession((String)null, (String)null, NotesAuth.getPassword());
        Database db = s.getDatabase(null, "C:\\notes\\data\\mymail.nsf");

        DocumentCollection collection = db.getAllDocuments();

        Document doc = collection.getFirstDocument();
        System.out.println("DB Doc Count = " + collection.getCount());

        while (doc != null)
        {
            String type = doc.getItemValueString("Form");

            if (type==null) type = "*none*";

            CountRec cr = map.get(type);

            int docAttCount = 0;
            int docAttSize = 0;

            Object body = doc.getFirstItem("Body");

            if (body!=null && body instanceof RichTextItem)
            {
                @SuppressWarnings("rawtypes")
                Vector att = ((RichTextItem)body).getEmbeddedObjects();

                if (att.size()>0)
                {
                    for (int i = 0; i < att.size(); i++)
                    {
                        EmbeddedObject a = (EmbeddedObject)att.get(i);
                        docAttSize += a.getFileSize();
                    }

                    docAttCount += att.size();
                }
            }

            if (cr==null)
            {
                cr = new CountRec(type);
                map.put(type, cr);
            }

            ++cr.count;
            cr.totalSize += doc.getSize();
            cr.docAttachCount += docAttCount;
            cr.docAttachSize += docAttSize;

            Document newDoc = collection.getNextDocument(doc);
            doc.recycle();      // need to do this to prevent "out of backend memory" error
            doc = newDoc;
        }

It reads everything in the mail DB but you can select on Form = 'Memo' or 'Reply' to deal with the only the actual email documents.

0
votes

You can use view.getFirstDocument, get the first mail, and use the view.getNextDocument (Document document) to read the next message. Or use view.getNthDocument (int index) to read mail.