logo

Why Might Notes Consider a MIME Field To Be Rich Text?

I hate the idea of turning this site in to my own forum, but have an "interesting" problem for you, which might help to have the answer on the internet, so...

I'm in the process of writing code to loop all messages in a Notes inbox, so I can examine their content. Now, consider this document and its Body field:

image

The Body field is obviously MIME, right?

As confirmed by ScanEz.

image

If you were to run the following code against the above document, what would you expect to see?

Item item = document.getFirstItem("Body");

if (item.getType()==Item.RICHTEXT){
    System.out.println("Item is Rich Text");
    
} else if (item.getType()==Item.MIME_PART){
    System.out.println("Item is MIME");
    
} else {
    System.out.println("Item is another type");
    
}

Now, I'd expect it to say it was a MIME field. Wouldn't you? But it doesn't. It says it's Rich Text!

I put this down to a quirk and decided to ignore what it was telling me and just go ahead and examine the MIME parts directly. Here's the code I used at first:

session.setConvertMIME(false);

MIMEEntity me = document.getMIMEEntity("Body");

if (null!=me){
    System.out.println("FOUND MIME ITEM!");
} else {
    System.out.println("CAN'T FIND MIME ITEM!");
}

session.setConvertMIME(true);
I'd expect it say it found a MIME item by that name, but this code says it can't find a MIME item. WTH?

Why on earth might Notes not be able to find a MIME field when it's obviously there and why might it consider it Rich Text instead? It's driving me spare.

UPDATE: Found the Solution

Ok, here's why it happens. If you combine the two code snippets above, like so:

Item item = document.getFirstItem("Body");

if (item.getType()==Item.MIME_PART){
    session.setConvertMIME(false);
        
    MIMEEntity me = item.getMIMEEntity();        
    if (null!=me){
        System.out.println("FOUND MIME ITEM!");
    } else {
        System.out.println("CAN'T FIND MIME ITEM!");
    }        
    session.setConvertMIME(true);
    
} else {
    System.out.println("Item is another type");
}

This won't work. The code will never get as far as the MIMEEntity bit as Item.getType() will never return MIME_PART.

However, this code will work:

session.setConvertMIME(false);

Item item = document.getFirstItem("Body");

if (item.getType()==Item.MIME_PART){
    MIMEEntity me = item.getMIMEEntity();        
    if (null!=me){
        System.out.println("FOUND MIME ITEM!");
    } else {
        System.out.println("CAN'T FIND MIME ITEM!");
    }        
    
} else {
    System.out.println("Item is another type");
}

session.setConvertMIME(true);

It all comes down to where you turn off MIME conversion for the session. It needs to happen before you look at the types of any field. Makes "sense". I guess. In a kind of it doesn't really kind of way.

Comments

  1. Hi,

    For Lotusscript, i think it's .Type like itemType% = notesItem.Type

    it's not .GetType() (JAVA)

    @+

  2. Meh. Welcome to my world.

    notesSession.ConvertMIME

    1. Hey, Dragon. Long time!

      Yeah, what's with convertmime? Do you need to toggle it off/on when reading fields as well as when setting them?

      Should the first code block above use it?

    2. Ah. I think your hint might have helped my discover the cause. I was turning off ConvertMIME in the wrong place.

      I'll update the above post with the solution in a mo.

      Damn you Notes!

      Hide the rest of this thread

      1. It is easiest to understand when you know what Notes is doing under the covers. MIME is converted to rich text when the note is "opened", or loaded into memory. Sometimes the document object is instantiated without having to open the note, since the Java class is a wrapper around the actual backend, but as soon as you want to read any value or get any item, the backend has to load that note. If the ConvertMime is false, it will load the note without the conversion. If it is true, it will convert.

        The part that makes it less than obvious is the separate stages of a) getting the document and b) opening it. While it might seem silly, it saves enormous amounts of processing and memory in some situations.

        As with Dragon, welcome to my world.

        1. Yes. I'm dealing with this sort of stuff day in, day out. The BES has to handle conversions all the time. More to the point, we have to handle the memory inside the 2gig limit imposed by the memory management of Domino (all hail 64 bit). So there are times when we have to do a juggling act. It's not nice. And there are times when it can all go horribly wrong.

          MIME vs RichText is one of my biggest headaches. Throw Notes Native Encryption into the mix and you can start to see why it gets to be a pain in the...

  3. Hi Jake. You should check (item != null) before you call getType().

    Even if you've got a hasItem call that you're not showing us, there is still an edge case where hasItem returns true but getFirstItem returns null. Your final code has actually worked around this edge case, but it's worth pointing out.

    The problem occurs when the Body item in the document is a MIME Part but there is no actual content in it. If you look at it, you will just see MIME headers and separators. If this is the case, and convertMIME = true is in effect, the result is that hasItem sees the item, but getFirstItem forces the conversion to rich text and an optimization kicks in that deletes the item from the in-memory note!

    Your fix does get around this problem by forcing convertMIME=false, but IMHO it's still best practice to check for null.

    1. Interesting. I'd noticed a "null item" issue in some other code I'd written even though I'd used hasItem() before it, then had to add a (null!=item) clause. Put it down to just Notes being Notes. Thanks for explaining the probable cause though!

    • avatar
    • Pierre
    • Thu 5 Apr 2012 11:54 AM

    Do you mean the following will always work when first I call: session.setConvertMIME(false);

    Item item = document.getFirstItem("Body");

    and whatever I want with the item,

    then call session.setConvertMIME(true);

      • avatar
      • Jake Howlett
      • Thu 5 Apr 2012 01:49 PM

      Yes, that should work.

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Tue 31 Jan 2012

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content