'Get sent message in Outlook Addin

I am facing some problems trying to get the sent message from an Outlook plugin.

In onItemSend event, I open a dialog where it shows some fields, with message information such as recipients, subject, etc and a button that will save these information into our DB. Another requirement is to save a copy of the sent message and this is where I got stuck...

I could save the message using the SaveAs method but the problem is when I open the message, it shows:

This message has not been sent. This message will be sent via Microsoft Exchange

causing some problems with users, making them think that the message was not sent.

During my searches, I found this thread where another person had the same problem and the solution was to use the message as PostItem instead of MailItem, once the PostItem is created in sent state. Also, we should set the MessageClass property to IPM.Note and delete PR_ICON_INDEX

Here is the code that I am using to do the steps above. I found this code here and changed a little bit:

PostItem postItem = this._email.Application.CreateItem(OlItemType.olPostItem);
postItem.MessageClass = "IPM.Note";
PropertyAccessor pa = postItem.PropertyAccessor;
pa.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x10800003", -1);
postItem.Save();

NameSpace session = postItem.Session;
string postItemEntryID = postItem.EntryID;
Marshal.ReleaseComObject(postItem);
Marshal.ReleaseComObject(pa);

MailItem newMessage = session.GetItemFromID(postItemEntryID) as MailItem;
newMessage.BCC = this._email.BCC;
newMessage.Body = this._email.Body;
newMessage.BodyFormat = this._email.BodyFormat;
newMessage.CC = this._email.CC;
newMessage.HTMLBody = this._email.HTMLBody;

//Hard coded path just for illustration
newMessage.SaveAs("C:\\Temp\\MSG\test.msg", OlSaveAsType.olMSG); 

The code above creates a postitem object, set some of the properties and save to the path correctly, but it has the following problems:

  1. After executing postItem.save, to create the postitem message, it creates a read message in inbox folder

  2. After saving the messages, I have compared the files and the size where significant, the original message size was 580kb and the postitem saved message was 52kb. It seems it did not make a copy of the message

  3. It lost some of the of the images embedded in message, such as signature images, showing a red X in place.

How can I get/create a message, with the exact message content, recipients, attachments, properties, etc (clone kind) with sent state, without creating another message inside inbox folder?

Thank you



Solution 1:[1]

I would not do this to a message that Outlook is trying to send. You can

  1. Process the Items.ItemAdd event on the Sent Items folder. By that time the message is sent and all the sender related properties are set.

  2. You can "fix" the created MSG file by removing the unsent flag. You can do that using Redemption (I am its author) - call RDOSession.GetMessageFromMsgFile / RDOMail.Sent = true / RDOMail.Save. Keep in mind that the sender information might not yet be set.

Solution 2:[2]

i would not go that way with the "postitem" further, somehow it does not look the perfect way for me.

the Problem is that you are copying the item bevor it is sent. Therefore the copy says it has not been sent. If you do not need the "normal" copy which is saved in the "sent items"-Folder, you could just Change the Folder where the item is saved with

Set mailitem.SaveSentMessageFolder = someother Folder '(which is defined as Outlook.folder)

if that is not possible, then I would make an inspection (in ThisOutlookSession) of the "sent items" Folder and make the copy-action for every new item in there. If you don't know how to let me know, then I copy you some code to bring you on the way.

another question just because Iam curious: why are you opening the form and waiting for someone to hit the ok-button, instead of saving the data into your db straight away?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Max