MAPI.Kit for Delphi – Examples
MAPI.Kit Example #29
Send on behalf of and send as another person
Needs Delphi 10.4 Sydney or later; demonstration project.
Compose a message from me, on behalf of another person (Send on Behalf) or as that person (Send As), the person picked in the address book; save it in Drafts or send it. The Inbox, Sent Items and Drafts show “X on behalf of Y”, and the sender properties of the selected message as they are stored.
In this example
TMAPIMessage.SetSentRepresenting(the represented sender) andSetSendAs(the actual sender too);IsSentOnBehalf,TMAPIMessageRow.SentRepresentingNameIsPublicDelegateOf: may I send on behalf of that person (PidTagAddressBookPublicDelegates)- Exchange decides by the rights: without them the send fails with
MAPI_E_NO_ACCESS(online; in Cached Exchange Mode a non-delivery report comes later) - Grant and remove “Send on Behalf” through Exchange Web Services (EWS.Kit
AddDelegate/RemoveDelegate) in the other person’s mailbox, with that person’s credentials typed by the user; “Send As” is an Active Directory permission an administrator gives
Works with
An Exchange mailbox (Microsoft Exchange Server or Microsoft 365) for “on behalf of” and “as”; the default profile or --profile=Name.
Run: SendOnBehalf.exe, SendOnBehalf.exe --profile=Name.
Download MAPI.Kit Example #29, compiled and signed (Win32 and Win64)
Download Source code of the example (Delphi 10.4 Sydney – Delphi 13 Florence)
MAPI.Kit library: free DCU packages
Level: GUI application, VCL application
Delphi: 10.4 Sydney and later
Class reference: “Messages”
Needs: an Exchange mailbox
Extended MAPI lessons: Request #21
What it shows
The window:
How it works
1. Compose and send. A new message in Drafts; one call sets the represented sender (on behalf of) or both senders (as). Exchange checks the rights when the message is submitted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Msg := AStore.IPMFolder[oFolderDrafts].CreateMessage; // the caller owns it try Msg.Subject := Subject; Msg.Body.Plain := Body; if Recips <> nil then Msg.Recipients.AddResolved(Recips); case Mode of FromOnBehalf: Msg.SetSentRepresenting(Other); // the represented sender only FromAs: Msg.SetSendAs(Other); // the actual sender as well end; Msg.Save; if ASend then try Msg.Send; // Exchange checks the rights here except on E: EMAPIError do if E.ErrorCode = MAPI_E_NO_ACCESS then Outcome := 'Exchange refused it (MAPI_E_NO_ACCESS). The draft stays in Drafts.' else raise; end; finally Msg.Free; end;
2. May I? The other person’s address book entry lists the users who may send on their behalf.
1
IsDelegate := IsPublicDelegateOf(ASession, EntryID); // PidTagAddressBookPublicDelegates of that person
Every MAPI call runs on one worker thread (ExampleWorker from the examples’ Common folder): the window posts a job that gets the session and the store, and the result comes back on the main thread. The window never waits for MAPI.
Units
MAPIKit, MAPIKit.Exchange.Delegates (IsPublicDelegateOf), MAPIKit.Exchange.Autodiscover (the EWS address), MAPIKit.Common.Errors, EWS.Kit (AddDelegate / RemoveDelegate) and ExampleWorker, ExampleUtils and TestStore from the examples’ Common folder.

