使用Java从Lotus Notes Email中提取附件

时间:2014-08-26 03:50:16

标签: java lotus-notes

在我的项目中,我试图在收件箱中阅读Lotus notes电子邮件。现在我的收件箱里有两封电子邮件,每封电子邮件都有一个附件,我正在尝试打开每封电子邮件并从该电子邮件下载附件。

我现在遇到的问题是我的程序只是阅读第一封电子邮件。

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();

    }
我认为这条线 -       document = view.getNextDocument(document)

会打开收件箱中的下一封电子邮件。但我只收到了第一封电子邮件。

欣赏任何指示。

2 个答案:

答案 0 :(得分:1)

此代码适用于我。虽然是为Notes 8编写的,但它也适用于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;
        }

它会读取邮件数据库中的所有内容,但您可以选择Form =&#39; Memo&#39;或者&#39;回复&#39;处理唯一的实际电子邮件文档。

答案 1 :(得分:0)

您可以使用view.getFirstDocument,获取第一封邮件,然后使用view.getNextDocument(文档文档)阅读下一封邮件。 或者使用view.getNthDocument(int index)来阅读邮件。

相关问题