Copyright © 2024 IMIBO. Privacy Statement
Request # 21
How to Send an e-mail message on behalf of someone else
Send on Behalf will allow a user to send as another user while showing the recipient that it was sent from a specific user on behalf of another user.
How to:
- Displays the Outlook address book dialog box.
- Performs name resolution, assigning entry identifiers to recipients in a recipient list.
- use PR_SENT_REPRESENTING_XXXX properties
Code snip:
procedure TfrmMain.btSendClick(Sender: TObject); var OutboxEntryID: TSBinary; Outbox: IMAPIFolder; ObjType: ULONG; MAPIMessage: IMessage; TempString: string; Prop: PSPropValue; OwnProp: PSPropValue; begin OwnProp := nil; try // Get Entry ID of Outbox folder hr := HrMAPIFindOutbox(FMsgStore, OutboxEntryID.cb, PENTRYID(OutboxEntryID.lpb)); if failed(hr) then raise Exception.Create(GetMAPIError(FMsgStore, hr)); // Get Interface to Outbox folder hr := FMsgStore.OpenEntry(OutboxEntryID.cb, PENTRYID(OutboxEntryID.lpb), @IID_IMAPIFolder, MAPI_MODIFY or MAPI_NO_CACHE, ObjType, IUnknown(Outbox)); if (MAPI_E_UNKNOWN_FLAGS = hr) or (MAPI_E_FAILONEPROVIDER=hr) then // maybe Outlook Version < 2003 hr := FMsgStore.OpenEntry(OutboxEntryID.cb, PENTRYID(OutboxEntryID.lpb), @IID_IMAPIFolder, MAPI_MODIFY, ObjType, IUnknown(Outbox)); if failed(hr) then raise Exception.Create(GetMAPIError(FMsgStore, hr)); // Create a new empty message hr := Outbox.CreateMessage(nil, 0, MAPIMessage); if failed(hr) then raise Exception.Create(GetMAPIError(Outbox, hr)); // Set Message Class hr := HrMAPISetPropString(MAPIMessage, PR_MESSAGE_CLASS, Pointer(PChar('IPM.Note'))); if failed(hr) then raise Exception.Create(GetMAPIError(MAPIMessage, hr)); // Set Message Subject TempString := Trim(ebSubject.Text); if TempString = '' then TempString := 'Test Message'; hr := HrMAPISetPropString(MAPIMessage, PR_SUBJECT, Pointer(PChar(TempString))); if failed(hr) then raise Exception.Create(GetMAPIError(MAPIMessage, hr)); TempString := Trim(BodyMemo.Text); hr := HrMAPISetPropString(MAPIMessage, PR_BODY, Pointer(PChar(TempString))); if failed(hr) then raise Exception.Create(GetMAPIError(MAPIMessage, hr)); Resolve(MAPIMessage); if not Assigned(FSentOnBehalfProp) and (ebSentOnBehalfOf.ReadOnly = False) then ResolveOneOf; // Set SentOnBehalfProp if Assigned(FSentOnBehalfProp) then begin // PR_SENT_REPRESENTING_ADDRTYPE Prop := PPropFindProp(FSentOnBehalfProp, FSentOnBehalfPropCb, CHANGE_PROP_TYPE(PR_ADDRTYPE, PT_UNSPECIFIED)); if Assigned(Prop) then begin Prop.ulPropTag := CHANGE_PROP_TYPE(PR_SENT_REPRESENTING_ADDRTYPE, PROP_TYPE(Prop.ulPropTag)); hr := MAPIMessage.SetProps(1, Prop, PSPropProblemArray(nil^)); if failed(hr) then raise Exception.Create(GetMAPIError(MAPIMessage, hr)); end; // PR_SENT_REPRESENTING_EMAIL_ADDRESS Prop := PPropFindProp(FSentOnBehalfProp, FSentOnBehalfPropCb, CHANGE_PROP_TYPE(PR_EMAIL_ADDRESS, PT_UNSPECIFIED)); if Assigned(Prop) then begin Prop.ulPropTag := CHANGE_PROP_TYPE(PR_SENT_REPRESENTING_EMAIL_ADDRESS, PROP_TYPE(Prop.ulPropTag)); hr := MAPIMessage.SetProps(1, Prop, PSPropProblemArray(nil^)); if failed(hr) then raise Exception.Create(GetMAPIError(MAPIMessage, hr)); end; // PR_SENT_REPRESENTING_NAME Prop := PPropFindProp(FSentOnBehalfProp, FSentOnBehalfPropCb, CHANGE_PROP_TYPE(PR_DISPLAY_NAME, PT_UNSPECIFIED)); if Assigned(Prop) then begin Prop.ulPropTag := CHANGE_PROP_TYPE(PR_SENT_REPRESENTING_NAME, PROP_TYPE(Prop.ulPropTag)); hr := MAPIMessage.SetProps(1, Prop, PSPropProblemArray(nil^)); if failed(hr) then raise Exception.Create(GetMAPIError(MAPIMessage, hr)); end; // PR_SENT_REPRESENTING_ENTRYID Prop := PPropFindProp(FSentOnBehalfProp, FSentOnBehalfPropCb, PR_ENTRYID); if Assigned(Prop) then begin Prop.ulPropTag := PR_SENT_REPRESENTING_ENTRYID; hr := MAPIMessage.SetProps(1, Prop, PSPropProblemArray(nil^)); if failed(hr) then raise Exception.Create(GetMAPIError(MAPIMessage, hr)); end; // PR_SENT_REPRESENTING_SEARCH_KEY Prop := PPropFindProp(FSentOnBehalfProp, FSentOnBehalfPropCb, PR_SEARCH_KEY); if Assigned(Prop) then begin Prop.ulPropTag := PR_SENT_REPRESENTING_SEARCH_KEY; hr := MAPIMessage.SetProps(1, Prop, PSPropProblemArray(nil^)); if failed(hr) then raise Exception.Create(GetMAPIError(MAPIMessage, hr)); end; end; // Save all changes hr := MAPIMessage.SaveChanges(KEEP_OPEN_READWRITE); if failed(hr) then raise Exception.Create(GetMAPIError(MAPIMessage, hr)); // Send Message hr := MAPIMessage.SubmitMessage(0); if failed(hr) then raise Exception.Create(GetMAPIError(MAPIMessage, hr)); ShowMessage('The message is going out...'); finally // Clear Outbox EntryID if Assigned(OutboxEntryID.lpb) then MAPIFreeBuffer(OutboxEntryID.lpb); if Assigned(OwnProp) then MAPIFreeBuffer(OwnProp); // Clear Outbox Interface Outbox := nil; // Clear Message Interface MAPIMessage := nil; end; end;
Download Request #21 as Compiled Application
Download Project (DELPHI 10.4) ZIP file
Source Code: In package