Search code examples
javaemailxpageslotus-noteslotus

Copy MIME parts from mail to another document - converting MIME <-> Richtext


Hello Domino programmers!

I work on a lotus database + XPages and i ran into a following problematic situation:

I receive mails in form of "Memo" documents directly to my database. I would like to separate attachments and body of mail (with embedded images) into two richtext fields. To achieve this I made a java agent. Idea seems fine for my solution, but every time I try to copy a Richtextitem "Body" from mail to another document, a created item contains Richtext elements instead of MIME parts. It would be fine, but through this a whole message seems to lose the formatting.. I know there exists a "switch" that should prevent this from happening - session.setConvertMime(false). I used it before accessing a source document but without any effect - my Richtext fields are blank instead.

So I have a bunch of questions:

  • Is there a way to copy body of a Memo document to store a content of email formatted? I'm interested in possibility to review email from XPages.
  • When I use a FileDownload, FileUpload and RichTextControl - is it recommendable to keep content of a message and attachments in the same field on back-end document?

Any help will be appreciated.

// I use 9.0 designer and 9.0 development server.

// Here is a code i currently use

          Session session = getSession();
          String TMPSAVE_PATH = System.getProperty("java.io.tmpdir");
          Database db = session.getCurrentDatabase();
          View v = db.getView("Inbox");
          ViewEntryCollection vec = v.getAllEntries(); 
          ViewEntry entry = vec.getFirstEntry();
          while(entry!=null)
          {
              Document mailDoc = entry.getDocument();
              Document newDoc = db.createDocument();
              newDoc.replaceItemValue("Form", "Ticket");
              newDoc.replaceItemValue("Title","[MAIL] " + mailDoc.getItemValueString("Subject"));
              newDoc.replaceItemValue("Status", "0");
              newDoc.computeWithForm(true, true);
              newDoc.save(false,true);

              RichTextItem rtiOLD = (RichTextItem)mailDoc.getFirstItem("Body");
              newDoc.removeItem("Description");
              RichTextItem rtiDESC = newDoc.createRichTextItem("Description");
              rtiDESC.appendRTItem(rtiOLD);
              newDoc.removeItem("Attachment");
              RichTextItem rtiATT = newDoc.createRichTextItem("Attachment");
              newDoc.save(false,true);

              List attachmentList = new ArrayList();
              Vector vector = rtiDESC.getEmbeddedObjects();

              if(vector.size()>0)
              {                  
                  for (int i = 1; i <= vector.size(); i++)
                  {
                      String attachmentName = vector.get(i-1).toString();
                      EmbeddedObject obj = (EmbeddedObject)vector.get(i-1);                   
                      if(obj!=null)
                      {
                          attachmentList.add(attachmentName);
                          obj.extractFile(TMPSAVE_PATH + attachmentName);
                          obj.remove();
                      }
                  }
              }

              newDoc.save(false,true); 

              if(attachmentList.size()>0)
              { 
                  for (int i = 0; i < vector.size(); i++) 
                  {
                      rtiATT.embedObject(EmbeddedObject.EMBED_ATTACHMENT, "", TMPSAVE_PATH + attachmentList.get(i), (String)attachmentList.get(i));
                      File file = new File(TMPSAVE_PATH + attachmentList.get(i));
                      file.delete();
                  }
              }
              newDoc.save(false,true);
          }           
          entry = vec.getNextEntry(entry);

//Changed title to be suitable for the problem.


Solution

  • I solved problem by multiple using methods NotesDocument.getFirstItem and then NotusDocument.AppendItemValue so I could just simply copy whole message body.