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:
The Body field is obviously MIME, right?
As confirmed by ScanEz.
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.
Hi,
For Lotusscript, i think it's .Type like itemType% = notesItem.Type
it's not .GetType() (JAVA)
@+
Reply
Meh. Welcome to my world.
notesSession.ConvertMIME
Reply
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?
Reply
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!
Reply
Show the rest of this thread
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.
Reply
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!
Reply
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);
Reply
Yes, that should work.
Reply