Search code examples
c#emailoutlookoutlook-addin

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

  • 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.