1
votes

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

2
Please , I have to implement something like this can you provide me the source code , or point me where I can find the solution , I need to store some emails to our database . how do you do that ?Zack ISSOIR
Please take a look at this. In this example it shows how to get an incoming email and do something with it. If you need to get the item sent, add a handler like this: this.itemsendhndler = new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend); this.Application.ItemSend += itemsendhndler;. Let me know if you need any other thing. :)UncleFester

2 Answers

1
votes

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 - call RDOSession.GetMessageFromMsgFile / RDOMail.Sent = true / RDOMail.Save. Keep in mind that the sender information might not yet be set.

0
votes

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?