0
votes

I have a Outlook 2013 addin that has a ItemSend Eventhandler that I can't get to work right.

What it does is:

  1. Loops all recipients (to, cc, bcc) and creates a separate copy for each with only that email as recipient and sends them. Then deletes them from sent mail folder. This works fine.

  2. Creates one more copy, that has the original to, cc and bcc information. Uses the move-method to move it to the Sent items folder, because Save would put it in Outbox. It must not be actually sent, it should be just your personal copy as if it would have been sent normally.

  3. Sets cancel = true and closes the inspector window with discard, so the original is never sent either. This works too.

The problem is the saved copy. When I open it, it's in compose mode. Basically it is same as it would be a draft. I want to see it as it would have been sent, in read mode.

I read that the Sent-property determines what mode to show it in, but that property is readonly, as are SentOn which is null and that would be a problem too even if the mode would be right. Is there any way around this?

I even tried to take one of the other mails that where actually sent, from Sent Items folder, edit the content and save it. But this results in the same behaviour. Also that MailItem has Sent = False and SentOn = null.

Could this be because it hasn't actually been sent yet even if Send-was called, as we are still running the EventHandler and I don't think Outlook actually sends it in another thread?

Anyway this doesn't seem to be a working workaround.

Any ideas how to implement this kind of functionality?

1

1 Answers

0
votes

On the low (Extended MAPI) level, the MSGFLAG_UNSENT bit can be removed from the PR_MESSAGE_FLAGS property only before the item is saved for the very first time (MAPI limitation).

The only OOM item ever created in the sent state is the post item. Create a post item, change its MessageClass property back to IPM.Note, save it, remember the item's entry id, release the post item using Marshal.ReleaseComObject (in case of .Net), then reopen it using Namespace.GetItemfromId - you will have MailItem object in the sent state. You will still need to update/delete the PR_ICON_INDEX property to make sure the icon is right.

If using Redemption is an option, it allows to set the Sent property (before it is saved) as well as SentOn / ReceivedTime / Sender / SentOnBehalfOf properties.

Off the top of my head:

RDOSession rdoSession = new RDOSession();
rdoSession.MAPIOBJECT = Globals.ThisAddIn.Application.Session.MAPIOBJECT;
RDOFolder rdoFolder = rdoSession.GetDefaultFolder(rdoDefaultFolders.olFolderSentMail);
RDOMail rdoItem = rdoInbox.Items.Add("IPM.Note");
rdoItem.Sent = true;
rdoItem.Recipients.AddEx("Joe The User", "[email protected]", "SMTP");
rdoItem.Subject = "test";
rdoItem.Body = "test body";
rdoItem.UnRead = false;
rdoItem.SentOn = rdoItem.ReceivedTime = new DateTime(2016, 10, 6, 8, 44, 0);
rdoItem.Sender = rdoItem.SentOnBehalfOf = rdoSession.CurrentUser;
rdoItem.Save();