Copyright © 2024 IMIBO. Privacy Statement
Request # 14
Outlook Folders (Inbox, Outbox, Sent Items, Deleted Items, Calendar, Contacts, Journal, Notes, etc…)
This example (By Request #14) will show you how to get direct access to Microsoft Outlook specific folders as Calendar, Contacts, Notes, etc..
Example includes functions as:
- GetInbox
- GetOutbox
- GetSentBox
- GetTrashBox
- GetDefaultFolder – direct access to Microsoft Outlook specific folders as Calendar, Contacts, Drafts, Journal, Notes, Tasks…
Code snip:
procedure TMainFrm.RadioGroupFoldersClick(Sender: TObject); begin if GetDefaultFolder(TMAPIFldType(RadioGroupFolders.ItemIndex)) then EnumFolderMessages end; function TMainFrm.GetDefaultFolder(FolderType: TMAPIFldType): boolean; var rgPropTag: TSPropTagArray; ObjType: ULONG; cValues: ULONG; PropArray: PSPropValue; begin Result := False; PropArray := nil; FMAPIFolder := nil; if (FolderType in [oFolderCalendar, oFolderContacts, oFolderDrafts, oFolderJournal, oFolderNotes, oFolderTasks]) then begin rgPropTag.cValues := 1; case FolderType of oFolderCalendar: rgPropTag.aulPropTag[0] := PR_IPM_APPOINTMENT_ENTRYID; oFolderContacts: rgPropTag.aulPropTag[0] := PR_IPM_CONTACT_ENTRYID; oFolderDrafts: rgPropTag.aulPropTag[0] := PR_IPM_DRAFTS_ENTRYID; oFolderJournal: rgPropTag.aulPropTag[0] := PR_IPM_JOURNAL_ENTRYID; oFolderNotes: rgPropTag.aulPropTag[0] := PR_IPM_NOTE_ENTRYID; oFolderTasks: rgPropTag.aulPropTag[0] := PR_IPM_TASK_ENTRYID; end; try hr := FMDB.OpenEntry(0, nil, nil, ExtendedMAPI.MAPI_BEST_ACCESS, ObjType, IUnknown(FMAPIFolder)); if Failed(hr) or not Assigned(FMAPIFolder) then exit; hr := FMAPIFolder.GetProps(@rgPropTag, fMapiUnicode, cValues, PropArray); FMAPIFolder := nil; if Failed(hr) or not Assigned(PropArray) or (PropArray.ulPropTag <> rgPropTag.aulPropTag[0]) then begin FMAPIFolder := GetInbox; if not Assigned(FMAPIFolder) then exit; hr := FMAPIFolder.GetProps(@rgPropTag, fMapiUnicode, cValues, PropArray); FMAPIFolder := nil; if Failed(hr) or not Assigned(PropArray) or (PropArray.ulPropTag <> rgPropTag.aulPropTag[0]) then exit; end; hr := FMDB.OpenEntry(PropArray.Value.bin.cb, PENTRYID(PropArray.Value.bin.lpb), @IID_IMAPIFolder, ExtendedMAPI.MAPI_BEST_ACCESS, ObjType, IUnknown(FMAPIFolder)); if Assigned(PropArray) then begin MAPIFreeBuffer(PropArray); PropArray := nil; end; if Failed(hr) or not Assigned(FMAPIFolder) then exit; finally if Failed(hr) then raise Exception.Create(GetMAPIError(FMDB, hr)); end; end else begin case FolderType of oFolderInbox: FMAPIFolder := GetInbox; oFolderDeletedItems: FMAPIFolder := GetTrashBox; oFolderOutbox: FMAPIFolder := GetOutbox; oFolderSentMail: FMAPIFolder := GetSentBox; end; end; Result := Assigned(FMAPIFolder); end; function TMainFrm.GetInbox: IMAPIFolder; var cbeid: ULONG; // count of bytes in entry ID eid: PENTRYID; ObjType: ULONG; begin Result := nil; if not Assigned(FMDB) then exit; FMAPIFolder := nil; eid := nil; try hr := HrMAPIFindInbox(FMDB, cbeid, eid); if Failed(hr) then raise Exception.Create(GetMAPIError(FMDB, hr)); hr := FMDB.OpenEntry(cbeid, eid, @IID_IMAPIFolder, ExtendedMAPI.MAPI_BEST_ACCESS, ObjType, IUnknown(FMAPIFolder)); if Failed(hr) or not Assigned(FMAPIFolder) then raise Exception.Create(GetMAPIError(FMDB, hr)); Result := FMAPIFolder; finally if Assigned(eid) then MAPIFreeBuffer(eid); end; end; function TMainFrm.GetSentBox: IMAPIFolder; var cbeid: ULONG; eid: PENTRYID; ObjType: ULONG; begin Result := nil; if not Assigned(FMDB) then exit; FMAPIFolder := nil; eid := nil; try hr := HrMAPIFindSentBox(FMDB, cbeid, eid); if Failed(hr) then raise Exception.Create(GetMAPIError(FMDB, hr)); hr := FMDB.OpenEntry(cbeid, eid, @IID_IMAPIFolder, ExtendedMAPI.MAPI_BEST_ACCESS, ObjType, IUnknown(FMAPIFolder)); if Failed(hr) or not Assigned(FMAPIFolder) then raise Exception.Create(GetMAPIError(FMDB, hr)); Result := FMAPIFolder; finally if Assigned(eid) then MAPIFreeBuffer(eid); end; end; function TMainFrm.GetTrashBox: IMAPIFolder; var cbeid: ULONG; // count of bytes in entry ID eid: PENTRYID; ObjType: ULONG; begin Result := nil; if not Assigned(FMDB) then exit; eid := nil; FMAPIFolder := nil; try hr := HrMAPIFindTrashBox(FMDB, cbeid, eid); if Failed(hr) then raise Exception.Create(GetMAPIError(FMDB, hr)); hr := FMDB.OpenEntry(cbeid, eid, @IID_IMAPIFolder, ExtendedMAPI.MAPI_BEST_ACCESS, ObjType, IUnknown(FMAPIFolder)); if Failed(hr) or not Assigned(FMAPIFolder) then raise Exception.Create(GetMAPIError(FMDB, hr)); Result := FMAPIFolder; finally if Assigned(eid) then MAPIFreeBuffer(eid); end; end; function TMainFrm.GetOutbox: IMAPIFolder; var cbeid: ULONG; // count of bytes in entry ID eid: PENTRYID; ObjType: ULONG; begin Result := nil; if not Assigned(FMDB) then exit; eid := nil; FMAPIFolder := nil; try hr := HrMAPIFindOutbox(FMDB, cbeid, eid); if Failed(hr) or not Assigned(eid) then exit; hr := FMDB.OpenEntry(cbeid, eid, @IID_IMAPIFolder, ExtendedMAPI.MAPI_BEST_ACCESS, ObjType, IUnknown(FMAPIFolder)); if Failed(hr) or not Assigned(FMAPIFolder) then raise Exception.Create(GetMAPIError(FMDB, hr)); Result := FMAPIFolder; finally if Assigned(eid) then MAPIFreeBuffer(eid); end; end;
Download Request #14 as Compiled Application
Download Project (DELPHI 10.4) ZIP file
Source Code: In package