Unable to set MAPI Header on draft mail when it is a reply

Add-in Express™ Support Service
That's what is more important than anything else

Unable to set MAPI Header on draft mail when it is a reply
 
Niels Ziegler


Guest


I am trying to set a classification marker on emails as a MAPI attribute, which a gateway appliance will evaluate (sensitivity=10,20,30,...). It works in most cases, using the code shown here on this https://www.add-in-express.com/forum/read.php?FID=5&TID=1020.

When the user creates a new mail, he can select the classification using buttons. I then set a MAPI header using the code mentioned above. I am basically doing this:

 object itemMAPI = OLLateBoundPropertyGetHelper(currentItem, "MAPIOBJECT");
switch (classificationMarker)
                    {
                        case Classification.Restricted:
                            mapi.SetHeader(itemMAPI, "x-classificationmarker", "10");                           
                            break;

                    }

mapi is a helper class instance.

When the mail is sent, I can grab the header in the item_Send event.

 MailItem mailItem = e.Item as MailItem;
       classificationHeader = GetClassificationHeader(mapi, mailItem.MAPIOBJECT);


But reading the header returns an empty string, if the mail is a REPLY to a received mail, that already contained this information. Writing it again during item_send also does not help. Just for testing it out, I tried this:

    classificationHeader = GetClassificationHeader(mapi, mailItem.MAPIOBJECT);

                    mapi.SetHeader(mailItem.MAPIOBJECT, Constants.CLASSIFICATIONHEADER, "20");

                    classificationHeader = GetClassificationHeader(mapi, mailItem.MAPIOBJECT);


Is there maybe another approach when I have to overwrite a setting that was part of the received header?
Posted 27 Jun, 2017 05:07:43 Top
Andrei Smolin


Add-in Express team


Posts: 18827
Joined: 2006-05-11
Hello Niels,

Niels Ziegler writes:
mapi.SetHeader(itemMAPI, "x-classificationmarker", "10");


Try to call MailItem.Save after you set the header.


Andrei Smolin
Add-in Express Team Leader
Posted 27 Jun, 2017 06:22:58 Top
Niels Ziegler


Guest


I am saving, but I added some more saves just to be sure.

Here's a fun fact: the problem only arises when I answer from the inline editor, not from an inspector window. Could it be I am accessing a different mail object than the one I am about to send?

When a classification button is pressed from either view, I look for the current object like this and end up in the explorer/selection branch when in an inline editor:

     Inspector currInspector = (Inspector)inspectorObj;
            Explorer currExplorer = (Explorer)explorerObj;

            object currItem = null;

            if (currInspector != null)
            {
                currItem = currInspector.CurrentItem;
            }
            else if (currExplorer != null)
            {
                if (currExplorer.Selection.Count > 0)
                {
                    currItem = currExplorer.Selection[1];
                }
            }
            return currItem;

I then pass the item object to another method, where I set the header:



object itemMAPI = OLLateBoundPropertyGetHelper(currentItem, "MAPIOBJECT");

      mapi.SetHeader(itemMAPI, Constants.CLASSIFICATIONHEADER, "10");
 


It never shows any error and works "sometimes" (new mails in inline editor and replies in inspector)
Posted 27 Jun, 2017 09:10:54 Top
Niels Ziegler


Guest


Oh, and every first time I set the header attribute on an e-mail, there is one empty "ghost" mail generated in the outbox folder.
Posted 27 Jun, 2017 09:12:15 Top
Andrei Smolin


Add-in Express team


Posts: 18827
Joined: 2006-05-11
Niels,

I've created a simple VBA macro setting a custom header on an email sent from an inline response. This works as expected: whenever I send an email (new or reply) a new value is set. Note that a save isn't required:

Sub MyTestMacro()
Dim anItem As Outlook.MailItem
Set anItem = Application.ActiveExplorer.ActiveInlineResponse
anItem.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-MyVal", "12345"
End Sub


Niels Ziegler writes:
Oh, and every first time I set the header attribute on an e-mail, there is one empty "ghost" mail generated in the outbox folder.


If all other COM add-ins are turned off, this may be an indication of an unreleased COM object in the code of your add-in.


Andrei Smolin
Add-in Express Team Leader
Posted 27 Jun, 2017 10:00:49 Top
Niels Ziegler


Guest


I wasn't aware, that there's an item object in ActiveInlineResponse. Using this helped setting the property and reading it back again when the email is sent. The "ghost" mails in the outbox are gone as well. So this seems to work now. Thank you!

But then there must be something wrong with the "ExplorerObj.Selection[1]" I was using. I did check the subject text while debugging and it was always the one from the email I was currently editing. But it must have been another object, since the property could not be found after retrieving that "same" email in the ItemSend event.
Posted 28 Jun, 2017 03:59:11 Top
Andrei Smolin


Add-in Express team


Posts: 18827
Joined: 2006-05-11
Hello Niels,

Niels Ziegler writes:
But then there must be something wrong with the "ExplorerObj.Selection[1]" I was using.


Selection[1] returns the item selected; the item is stored in the current folder. ActiveInlineResponse returns an item being created/edited; the item is seemingly stored in Drafts.


Andrei Smolin
Add-in Express Team Leader
Posted 28 Jun, 2017 04:39:24 Top
Niels Ziegler


Guest


I am still working on the same Add-In, and I managed to set headers on mails a while ago and now I am stuck on meetings, which have a lot of special actions going on. Roughly said, I store an x-header in outgoing mails, and on the receiving side, I show the classification selected by the sender, then use that setting as default selection on replies.

But with meetings, the information get's lost somehow and the header never arrives. I see no reason to believe the server would strip it somehow, the server will even bounce the message back if the header is not available when it is processed.

Based on your suggestion I am setting the header like this:
acc.SetProperty(http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/ + "x-sensitivity", value);
In the send-event, I can see the value in OutlookSpy and I can get the value by checking the appointment item

 OLInterop.AppointmentItem appointment = meetingItem.Item.GetAssociatedAppointment(false);
                            object appMapi = null;

                            if (appointment != null)
                            {
                                appMapi = appointment.MAPIOBJECT;
                                classificationHeader = mapi.GetHeader(appMapi, Constants.CLASSIFICATIONHEADER);
                            }
                            Marshal.ReleaseComObject(appointment);
                            Marshal.ReleaseComObject(appMapi);


But once it arrives in the recipient inbox, the header is gone. I can not find it with OutlookSpy. Then again replies to meetings are working as expected. I can not find the difference.
Posted 14 Jul, 2017 09:20:26 Top
Andrei Smolin


Add-in Express team


Posts: 18827
Joined: 2006-05-11
Hello Niels,

When you create a meeting in the Outlook UI, you use an AppointmentItem. When you send a meeting request, a MeetingItem is used; you retrieve the AppointmentItem it contains using MeetingItem.GetAssociatedAppointment(true/false). Check if that AppointmentItem contains the header. If not, try setting it on the MeetingItem instead.


Andrei Smolin
Add-in Express Team Leader
Posted 14 Jul, 2017 09:48:10 Top
Niels Ziegler


Guest


While in edit/draft mode, I receive a item of type appointment, I set the header. In the itemSend event, it's a meeting and I can get the header value from the appointment. I now tried to set it again during sending, on the meeting item, but no change.
Posted 14 Jul, 2017 10:26:20 Top