none
Outlook MailItem object and Extended MAPI IMessage interface RRS feed

  • General discussion

  • I had to move my VSTO outlook addin  (Office 2007/2010) to unmanaged C++.

    The code gets the MailItem object on Send event, inspects attachments, and saves them to the temporary location, if the attachments are of certain type. The business logic requires replacing the processed attachments, so, the code deletes the original attachments and attaches the processed files. This is done by calling Extended MAPI functions. IMessage interface pointer is extracted via MAPIOBJECT property of MailItem.

    Everything works fine (I'm using HTML messages body for testing). 

    Here's where the code fails: If I attach 2 files with the identical filenames:

    On the original processing, both files are replaced with the copies from the disk, and going out without issue. But, if I forward the previously processed message I get an error in MailItem.SaveAsFile() call. The COM exception error is "Member not found".

    To summarize, I can repeatedly forward the same email with the uniquely named attachments, the code processes messages without any issue. But the forwarding will cause an error for the messages with 2 or more identically named attachment files.

    It would be great if anyone can offer a clue or suggestion.

    Thanks,

    Michael

    Tuesday, July 17, 2012 9:31 PM

All replies

  • Do you mean you get the error from Attachment.SaveAsFile?

    If oyu are using Extended MAPI, why not use it to save the contents of the PR_ATTACH_DAT_BIN property?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Wednesday, July 18, 2012 12:31 AM
  • correct, SaveAsFile() on the attachment object.

    regarding your question on PR_ATTACH_DAT_BIN:

    My original plan was to just re-write the addin in C++ to see if that would fix the issues we were having (lately). Basically to get rid of .Net interop and do everything through COM. But then I realized that I had the same issues with this approach, so I added the Extended MAPI calls to the code.

    And I'm considering right now, to give up on Outlook's MailItem and to do everthing using IMessage interface.

    The thing that bothers me is why I'm only experiencing this issue when dealing with identically named attached files. After further testing today, I've discovered that forwarded email will result in error (on SaveAsFile call). But it only errors on identically name Word or Excel binary files (.doc and .xls) not on 2007 file format (.docx and .xlsx). If I attach any mix of any uniquely named word or excel files, I can forward/process email without any issues.

    My suspicion was that there was something out of sync between Outlook's MailItem and IMessage interface pointer, but looks like I might have another issue on my hands. BTW, I inspected the resulted emails with Outlook Spy, and didn't see anything wrong with them.

    Thank you

    Michael

    Wednesday, July 18, 2012 4:09 AM
  • What does your code look like right now that adds the attachments?

    Thursday, July 19, 2012 3:19 PM
    Answerer
  • I already replaced the code to just swap the data in PR_ATTACH_DATA_BIN, testing right now.

    So, I'm not positive if everything will work

    But before I had something like this:

    1 Call CreateAttach on IMessage pointer

    2 set PR_ATTACH_METHOD prop to ATTACH_BY_VALLUE

    3 Open PR_ATTACH_DATA_BIN

    4 copy the stream from the disk file to attachment stream, call Commit()

    5 Set PR_DISPLAY_NAME_W, PR_ATTACH_LONG_FILENAME_W, PR_ATTACH_FILENAME_W, 

    PR_ATTACHMENT_FLAGS = 0, PR_ATTACH_FLAGS = 0, PR_ATTACHMENT_LINKID = 0

    6 Set Custom attachment property (my app specific)

    7 Save attachment

    8 Save Message

    9 Call Release on the stream pointers and lpAttach.

    Thank you

    Michael

    Friday, July 20, 2012 9:17 PM