1

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

UncleFester
  • 417
  • 1
  • 7
  • 19
  • 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 Sep 06 '16 at 08:38
  • Please take a look at [this](https://blogs.msdn.microsoft.com/emeamsgdev/2011/12/06/process-incoming-mail-using-an-outlook-addin). 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 Sep 07 '16 at 16:13

2 Answers2

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.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Hi Dmitry! Just a question on your first suggestion...I have read somewhere that the user can change the sent items folder. If the user change this folder, will I be able to get the sent message? Do I have to set the sent folder before itemAdd event? And if I change the sent folder location, will it overwrite the user's option? Thanks for your suggestions – UncleFester Sep 30 '14 at 13:43
  • You can track the Application.ItemSend event and check if MailItem.SaveSentMessageFolder property points to the expected Sent Items folder returned by Namespace.GetDefaultFolder (use Namespace..CompareEntryIds). You can then either track the ItemAdd event on that folder as well, or save the custom folder's entry id in a custom property (MailItem.UserProperties) and reset the SaveSentMessageFolder property to the default Sent Items folder. When you process ItemAdd on that folder, you can then use the custom property to open the custom folder and move the message there. – Dmitry Streblechenko Sep 30 '14 at 21:15
  • hi Dmitry can you explain how to "track the ItemAdd event on that folder as well". Would this mean to define something like "Public WithEvents justinreserve1 As Outlook.Items" and then to use this for that Folder? – Max Oct 01 '14 at 13:03
  • Instead of having a single variable that points to the Items object, you can have a list of Items objects - this way you can have an arbitrary number of Items all referenced to make sure they are not released. Are you using C# or VB? – Dmitry Streblechenko Oct 01 '14 at 16:42
  • I am using vb. Sounds interesting have you got a link where I can find further information? – Max Oct 03 '14 at 18:08
  • VBA? VB 6? VB.Net? Your original question was tagged C# and your had C# code, but your comment mentioned "Public WithEvents justinreserve1", which can be either of the VB flavors. – Dmitry Streblechenko Oct 03 '14 at 18:27
0

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?

Max
  • 744
  • 1
  • 7
  • 19
  • Hi Max, thank you for your suggestion. I will try that...Regarding your curiosity, I show another fields on the form, where the user will add notes, pick some data from some dropdowns and then when the user hits ok-button, it will save all the user selection and the message itself. I just did not write all the things happening on that form :) Thanks – UncleFester Sep 30 '14 at 13:34
  • hi, the "inspection (in ThisOutlookSession) of the "sent items" Folder" is the same thing as Dimitri's suggestion, although of course he had the better explanation. also here this will only work if the user has not changed the sent-items-Folder, or even does this case-by-case with vba. – Max Oct 01 '14 at 13:04