LazyMAPI

MAPIFldUtils

unit: MAPIFldUtils.pas
file path: ..\Library\
version: 2014.хх
uses Windows, ComCtrls, SysUtils, ExtendedMAPI, MapiUtils, IMIEMTypes

Available functions:

function GetDefaultFolder - Returns the default folder of the specified type for the message store of the current session.

function GetInbox - Returns a Folder representing the current users Inbox folder.

function GetTrashBox - Returns the current users Deleted folder for the message store of the current session.

function GetOutbox - Returns the current users Outbox folder for the message store of the current session.

function GetSentBox - Returns the current users Sent Items folder for the message store of the current session.

function GetTopFolder - Returns the IPM (interpersonal message) subtree folder for the message store of the current session

function GetIMPSubtreeEntryID - Returns the IPM (interpersonal message) subtree folder Unique EntryID (PR_ENTRYID)

function GetFoldersRoot - Returns the IPM (interpersonal message) subtree folder for the message store of the current session. This folder is invisible, and it is a parent for all other folders.


function GetMAPIFolderType - Returns type of folder (TMAPIFldType), such as Contacts, Calendar, Notes, Journal, Tasks.

function GetMAPIFolderTypeEx - Returns type of folder (TMAPIFldType), such as Inbox, Sent Items, Deleted, Outbox, Junk, Contacts, Calendar, Notes, Journal, Tasks.

function GetMAPIFolderKnowType - Returns type of folder, converting FolderClass (PR_CONTAINER_CLASS) to TMAPIFldType.


function GetMAPIFolder - Returns IMAPIFolder based on its Unique EntryID (PR_ENTRYID).

function GetMAPIFolderList - Returns an array of TFolderHead

function GetMAPIFolderHead - Returns TFolderHead of MAPI Folder


function GetMessageList - Returns TMsgHead array of all messages inside a folder.

function GetMsgTable - Returns the folder contents table (MAPI table with all visible messages).

function GetTableRowCount - Returns the total number of rows in the table.


function GetMsgHead - Returns a message head (TMsgHead) by its table index.


function GetMapiMessage - Returns (open) a message  (IMessage) by its table index, or message Unique Id (PR_ENTRYID).

function CreateMapiMessage - Returns (create) a new empty MAPI message (IMessage)

procedure DeleteMapiMessages - Deletes one or more messages from the current folder

procedure CopyOrMoveMapiMessages - Copy or move one or more messages from the current folder to another folder


procedure EmptyMapiFolder - Deletes all messages and subfolders from a folder without deleting the folder itself

procedure DeleteMapiSubFolder - Deletes subfolder

function CreateMapiSubFolder - Creates a new subfolder.

procedure CopyOrMoveMapiSubFolder - Copies/moves the folder specified thru SubFldEntryId to the destination folder.


function GetFldIconIndex - Returns a number (integer) that indicates which icon to use when you display a folder

function GetMsgIconIndex - Returns a number (integer) that indicates which icon to use when you display a message

function CalculateMsgIconIndex - For internal use


function IsSpecialFolder - Returns True if folder is a special (system) folder as Inbox, Sent Item, Deleted Items, Outbox, Calendar, etc.

function ShowCustomSize - For internal use


function GetMAPIFoldersTable - Returns MAPI Table (IMAPITable) with subfolders information


function GetMAPISpecialFoldersList - Returns an array of the type of the folder (TMAPIFldType) and its unique ID (PR_ENTRYID). The array can contain information about the Inbox, Outbox, Sent items, Drafts, Contacts, Calendar and other system folders.


procedure ClearMAPISpecialFoldersList - Clears and releases the used memory of TSpecialFoldersList


function GetDefaultFolder(const MsgStore: IMsgStore; FolderType: TMAPIFldType; const OnLine: Boolean = True; ReadOnly: Boolean = True): IMAPIFolder;

description
 
The GetDefaultFolder returns the default folder of the specified type for the message store of the current session.

parameters

MsgStore - IMsgStore
FolderType - TMAPIFldType of target folder (as Inbox, Outbox, Sent, Calendar, Contacts, etc.).
   Accepted values are
   oFolderInbox
   oFolderDeletedItems
   oFolderOutbox
   oFolderSentMail
   oFolderCalendar
   oFolderContacts
   oFolderDrafts
   oFolderJournal
   oFolderNotes
   oFolderTasks 
OnLine - if true, forces the live, on-line IMAPIFolder. Otherwise local, cached copy can be used. applicable only if IMsgStore reside on remote server, as Microsoft Exchange Server.
ReadOnly - if True, do not requests read/write permission

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get Calendar folder
 fMsgFolder := GetDefaultFolder(fMsgStore, oFolderCalendar);

Top


function GetInbox(const MsgStore: IMsgStore; const OnLine: Boolean = True): IMAPIFolder;

description
 The GetInbox returns the current user Inbox folder for the message store of the current session.

parameters

MsgStore - IMsgStore
OnLine - if true, forces the live, on-line IMAPIFolder. Otherwise local, cached copy can be used. applicable only if IMsgStore reside on remote server, as Microsoft Exchange Server.

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get Inbox  folder
 fMsgFolder := GetInbox(fMsgStore);

Top


function GetTrashBox(const MsgStore: IMsgStore; const OnLine: Boolean = True): IMAPIFolder;

description
 The GetTrashBox returns the current users Deleted folder for the message store of the current session.

parameters

MsgStore - IMsgStore
OnLine - if true, forces the live, on-line IMAPIFolder. Otherwise local, cached copy can be used. applicable only if IMsgStore reside on remote server, as Microsoft Exchange Server.

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get Deleted  folder
 fMsgFolder := GetTrashBox(fMsgStore);

Top


function GetOutbox(const MsgStore: IMsgStore; const OnLine: Boolean = True): IMAPIFolder;

description
 The GetOutbox returns the current users Outbox folder for the message store of the current session.

parameters

MsgStore - IMsgStore
OnLine - if true, forces the live, on-line IMAPIFolder. Otherwise local, cached copy can be used. applicable only if IMsgStore reside on remote server, as Microsoft Exchange Server.

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get Outbox  folder
 fMsgFolder := GetOutbox(fMsgStore);

Top


function GetSentBox(const MsgStore: IMsgStore; const OnLine: Boolean = True): IMAPIFolder;

description
The GetSentBox returns the current users Sent Items folder for the message store of the current session.

parameters

MsgStore - IMsgStore
OnLine - if true, forces the live, on-line IMAPIFolder. Otherwise local, cached copy can be used. applicable only if IMsgStore reside on remote server, as Microsoft Exchange Server.

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get Sent Items  folder
 fMsgFolder := GetSentBox(fMsgStore);

Top


function GetTopFolder(const MsgStore: IMsgStore; const OnLine: Boolean = True): IMAPIFolder;

description
The GetTopFolder returns the IPM (interpersonal message) subtree folder for the message store of the current session. This folder is invisible, and it is a parent for all other folders as Inbox, Calendar, Notes, etc..

parameters

MsgStore - IMsgStore. Can be private store only.
OnLine - if true, forces the live, on-line IMAPIFolder. Otherwise local, cached copy can be used. applicable only if IMsgStore reside on remote server, as Microsoft Exchange Server.

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get Root folder
 fMsgFolder := GetTopFolder(fMsgStore);

Top


function GetIMPSubtreeEntryID(const MsgStore: IMsgStore): TBytes;

description
The GetIMPSubtreeEntryID returns the IPM (interpersonal message) subtree folder Unique EntryID (PR_ENTRYID).

parameters

MsgStore - IMsgStore. Can be private store only.

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get Root folder EntryID
 fFolderID := GetIMPSubtreeEntryID(fMsgStore);

Top


function GetFoldersRoot(const MsgStore: IMsgStore; const OnLine: Boolean = True): IMAPIFolder;

description
The GetFoldersRoot returns the IPM (interpersonal message) subtree folder for the message store of the current session. This folder is invisible, and it is a parent for all other folders.

parameters

MsgStore - IMsgStore. Can be private or public store.
OnLine - if true, forces the live, on-line IMAPIFolder. Otherwise local, cached copy can be used. applicable only if IMsgStore reside on remote server, as Microsoft Exchange Server.

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get Root folder
 fMsgFolder := GetFoldersRoot(fMsgStore);

Top


function GetMAPIFolderType(const MAPIFolder: IMAPIFolder): TMAPIFldType;

description
The GetMAPIFolderType returns type of folder, such as Contacts, Calendar, Notes, Journal, Tasks.
For retrieving also Inbox, Sent Items, Deleted, Outbox, Junk - use function GetMAPIFolderTypeEx.  

parameters

MAPIFolder - IMAPIFolder.

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get Calendar folder
 fMsgFolder := GetDefaultFolder(fMsgStore, oFolderCalendar);
 // Check for folder type
 if GetMAPIFolderType(fMsgFolder)<>oFolderCalendar then ShowMessage('Something is wrong here!');

Top


function GetMAPIFolderTypeEx(const MsgStore: IMsgStore; const MAPIFolder: IMAPIFolder): TMAPIFldType;

description
The GetMAPIFolderTypeEx returns type of folder, such as Inbox, Sent Items, Deleted, Outbox, Junk, Contacts, Calendar, Notes, Journal, Tasks.

parameters

MsgStore - IMsgStore where inspected Folder reside
MAPIFolder - IMAPIFolder.

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get Inbox folder
 fMsgFolder := GetInbox(fMsgStore);
 // Check for folder type
 if GetMAPIFolderTypeEx(fMsgStore, fMsgFolder)<>oFolderInbox then ShowMessage('Something is wrong here!');

Top


function GetMAPIFolderKnowType(const FolderClass: string): TMAPIFldType;

description
The GetMAPIFolderKnowType returns type of folder, converting FolderClass (PR_CONTAINER_CLASS) to TMAPIFldType.

parameters

FolderClass - string as 'IPF.Appointment'

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get Calendar folder
 fMsgFolder := GetDefaultFolder(fMsgStore, oFolderCalendar);
 // Get folder class
  fFolderClass := GetPropString(fMsgFolder, PR_CONTAINER_CLASS);
 // Check for folder type
 if GetMAPIFolderKnowType (fFolderClass)<>oFolderCalendar then ShowMessage('Something is wrong here!');

Top


function GetMAPIFolder(const MAPISession: IMAPISession; const FldEntryId: TBytes; const OnLine: Boolean = True; const ReadWrite: Boolean = False): IMAPIFolder;overload;
function GetMAPIFolder(const MsgStore: IMsgStore; const FldEntryId: TBytes; const OnLine: Boolean = True; const ReadWrite: Boolean = False): IMAPIFolder;overload;

description
The GetMAPIFolder returns IMAPIFolder based on its Unique EntryID (PR_ENTRYID).

parameters

MAPISession or MsgStore - Interface which will be used for opening MAPI Folder
FldEntryId - Target folder Unique EntryID  (PR_ENTRYID)
OnLine - if true, forces the live, on-line IMAPIFolder. Otherwise local, cached copy can be used. applicable only if IMsgStore reside on remote server, as Microsoft Exchange Server.
ReadWrite - if True, do not requests read/write permission

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get all visible folders @ level 1
 
fFolderList:=GetMAPIFolderList(fMsgStore);
 // Get the second folder
 fMsgFolder :=  GetMAPIFolder(fMsgStore, fMsgFolder[1].ID);
 // Get folder class
 fFolderClass := GetPropString(fMsgFolder, PR_CONTAINER_CLASS);
 // Show folder class
 ShowMessage(fFolderClass);

Top


function GetMAPIFolderList(const MsgStore: IMsgStore): TFoldersHeadList; overload;
function GetMAPIFolderList(const MAPIFolder: IMAPIFolder): TFoldersHeadList; overload;

description
The GetMAPIFolderList returns an array of TFolderHead

parameters
MAPIFolder or MsgStore - Parent Folder/Store

usage

 fMAPISession := GetMapiSession(Self.Handle, '', MAPI_LOGON_UI);
 // Get Default Store
 fMsgStore := GetDefaultStore(fMAPISession);
 // Get all visible folders @ level 1
 
fFolderList:=GetMAPIFolderList(fMsgStore);
 // Get the second folder
 fMsgFolder :=  GetMAPIFolder(fMsgStore, fMsgFolder[1].ID);

Top


function GetMAPIFolderHead(const MAPIFolder: IMAPIFolder): TFolderHead; overload;
function GetMAPIFolderHead(const MAPISession: IMAPISession; const FldEntryId: TBytes): TFolderHead; overload;

description
The GetMAPIFolderHead returns TFolderHead of MAPI Folder

parameters

MAPIFolder - Folder interface
MAPISession - current MAPI session
FldEntryId - Folder Unique ID  (PR_ENTRYID)

Top


function GetMessageList(const MAPIFolder: IMAPIFolder; const ProgressBar: TProgressBar = nil; const SortPropTag: ULONG = PR_CLIENT_SUBMIT_TIME; const Ascending: Boolean = False): TMsgHeadList; overload;

description
Returns TMsgHead array of all messages inside a folder. Returned array can be sorted thru SortPropTag. Default value for SortPropTag is PR_CLIENT_SUBMIT_TIME (date and time when the message sender submitted a message) 

function GetMessageList(const MAPIFolder: IMAPIFolder; const StartIndex, EndIndex: Cardinal; const ProgressBar:TProgressBar = nil; const SortPropTag: ULONG = PR_CLIENT_SUBMIT_TIME; const Ascending: Boolean = False): TMsgHeadList; overload;

description
Returns TMsgHead array of messages inside a folder, within StartIndex/EndIndex. For example when StartIndex=40 / EndIndex = 120, the result will includes all messages where position is >=40 and position is =< 120. Returned array can be sorted thru SortPropTag. Default value for SortPropTag is PR_CLIENT_SUBMIT_TIME (date and time when the message sender submitted a message) 


function GetMessageList(const MAPITable: IMAPITable; const ProgressBar: TProgressBar = nil; const SortPropTag: ULONG = PR_CLIENT_SUBMIT_TIME; const Ascending: Boolean = False): TMsgHeadList; overload;

description
Returns TMsgHead array of all messages inside a folder. Returned array can be sorted thru SortPropTag. Default value for SortPropTag is PR_CLIENT_SUBMIT_TIME (date and time when the message sender submitted a message) 


function GetMessageList(const MAPITable: IMAPITable; const StartIndex, EndIndex: Cardinal; const ProgressBar: TProgressBar = nil): TMsgHeadList; overload;

description
 
Returns TMsgHead array of messages inside a folder, within StartIndex/EndIndex. For example when StartIndex=40 / EndIndex = 120, the result will includes all messages where position is >=40 and position is =< 120.  

usage

procedure LoadInboxMessages;
begin
// Get Inbox
    if not Assigned(Inbox) then
    Inbox := GetInbox(MsgStore);
// Get list of messages
    MsgHeadList := GetMessageList(Inbox);

    MsgHeadListCount := Length(MsgHeadList);
    StatusBar.SimpleText := 'Message Count: ' + IntToStr(MsgHeadListCount) + ' in Inbox';

// Force virtual ListView to render items
    MessageListView.Items.Count := MsgHeadListCount;
    MessageListView.Refresh;
end;

// Render messages header inside virtual ListView
procedure
MessageListViewData(Sender: TObject; Item: TListItem);
begin
    if MsgHeadList[Item.Index].HasAttachment then
    Item.SubItemImages[0] := 0
    else
    Item.SubItemImages[0] := -1;
   
    Item.SubItems.Add(MsgHeadList[Item.Index].Sender);

    Item.SubItems.Add(MsgHeadList[Item.Index].Subject);

    Item.SubItems.Add(DateTimeToStr(MsgHeadList[Item.Index].SentTime));
    Item.SubItems.Add(IntToStr(MsgHeadList[Item.Index].Size));
end;

Top


function GetMsgTable(const MAPIFolder: IMAPIFolder; const SortPropTag: ULONG = PR_CLIENT_SUBMIT_TIME; const Ascending: Boolean = False): IMAPITable;

description
Returns the folder contents table (MAPI table with all visible messages). Returned MAPI table can be sorted thru SortPropTag. Default value for SortPropTag is PR_CLIENT_SUBMIT_TIME (date and time when the message sender submitted a message) 

parameters

MAPIFolder - IMAPIFolder

usage

var
    MAPITable:IMAPITable;
    Count:Integer;
begin
    MAPITable := GetMsgTable(MAPIFolder);
// Get message count
    Count := GetTableRowCount(MAPITable);

note:
  The output table contains the following columns:
    PR_INSTANCE_KEY
    PR_ENTRYID
    PR_LONGTERM_ENTRYID_FROM_TABLE
    PR_MESSAGE_CLASS
    PR_HASATTACH
    PR_SUBJECT
    PR_NORMALIZED_SUBJECT
    PR_SENDER_NAME
    PR_SENDER_EMAIL_ADDRESS
    PR_CLIENT_SUBMIT_TIME
    PR_MESSAGE_DELIVERY_TIME
    PR_MESSAGE_SIZE
    PR_MESSAGE_FLAGS
    PR_ICON_INDEX

Top


function GetTableRowCount(const MAPITable: IMAPITable): Cardinal;

description
Returns the total number of rows in the table

parameters

MAPITable - IMAPITable

usage

var
    MAPITable:IMAPITable;
    Count:Integer;
begin
    MAPITable := GetMsgTable(MAPIFolder);
// Get message count
    Count := GetTableRowCount(MAPITable);

Top


function GetMsgHead(const MAPITable: IMAPITable; const MsgIndex: Cardinal; const MsgCount: Cardinal = 0): TMsgHead;

description
Returns a message head regarding its table index.

parameters

MAPITable - IMAPITable
MsgIndex - Target message index inside MAPI content table. For retrieving second message, specify MsgIndex=1
MsgCount - Total number of messages inside content table

Top


function GetMapiMessage(const MAPIFolder: IMAPIFolder; MsgIndex: Cardinal; ReadOnly: Boolean = True): IMessage; overload;
function GetMapiMessage(const MAPIFolder: IMAPIFolder; const MsgEntryId: String; ReadOnly: Boolean = True): IMessage; overload;
function GetMapiMessage(const MAPIFolder: IMAPIFolder; const MsgEntryId: TBytes; ReadOnly: Boolean = True): IMessage; overload;
function GetMapiMessage(const MAPIStore: IMsgStore; const MsgEntryId: TBytes; ReadOnly: Boolean = True): IMessage; overload;

description
Returns (open) a message (IMessage) by its table index, or message Unique Id (PR_ENTRYID).

parameters

MAPIFolder - IMAPIFolder where reside message / MAPIStore - IMsgStore where reside message
MsgIndex - Target message index inside MAPI content table. For retrieving second message, specify MsgIndex=1
MsgEntryId - Message Unique Id (PR_ENTRYID), as string or bytes
ReadOnly - if True, do not requests read/write permission

usage

procedure MessageListViewDblClick(Sender: TObject);
var
    MAPIMessage: IMessage;
begin
    if not Assigned(MessageListView.Selected) then
Exit;

    MAPIMessage := GetMAPIMessage(MAPIFolder, MessageListView.Selected.Index);

// Show IMessage inside Delphi custom form

    with TfrmMessage.Create(Self) do
    try
        SetMessage(MAPIMessage);
        ShowModal;
    finally
        Free;
    end;
end;

Top


function CreateMapiMessage(const MAPIFolder: IMAPIFolder; const MsgType: TMsgType = oUnknown): IMessage;

description
Returns (create) a new empty MAPI message (IMessage).

parameters

MAPIFolder - IMAPIFolderwhere will reside message
MsgType - Target message type as Appointment, Contact, Task, etc..

usage

 // Create a new message in Draft items folder
MAPIMessage := CreateMapiMessage(DraftBox);

// Show IMessage inside Delphi custom form
try
    with TfrmNewMsg.Create(Self) do
    begin
        SetMAPIObjects(MAPISession, MsgStore, DraftBox, MAPIMessage);
        ShowModal;
        Free;
    end;
finally
    MAPIMessage := nil;
end;

Top


procedure DeleteMapiMessages(const MAPIFolder: IMAPIFolder; const MsgEntryIdList: array of TBytes; const TrashBox: IMAPIFolder = nil; const Progress: IMAPIProgress = nil; const DialogHandle: UINT_PTR = 0);

description
The DeleteMapiMessages method deletes one or more messages from the current folder

parameters

MAPIFolder - IMAPIFolder where resides messages
MsgEntryIdList - Array of messages EntryID to delete (unique message ID - PR_ENTRYID)
TrashBox - IMAPIFolder. If it is not nil, deleted messages will be moved to this folder
Progress: IMAPIProgress. A progress object that displays a progress indicator
DialogHandle - A handle to the parent window of the progress indicator 

usage

var
  EntryIDList: array of TBytes;
begin

    // Get number of selected messages for deleting
   
Count := MessageListView.SelCount;
    if Count = 0 then
    Exit;

    Indx:=0;

    SetLength(EntryIDList, Count);
   
    // Get first selected ListView item

    Item := MessageListView.Selected;

    while Item <> nil do
    begin
        ItemIndex := Item.Index;
        MsgHead := ShellItem(ItemIndex);
       
         SetLength(EntryIDList[Indx], Length(MsgHead^.ID));
         Move(MsgHead^.ID[0], EntryIDList[Indx][0], Length(MsgHead^.ID));
        
        Item := MessageListView.GetNextItem(Item, sdAll, [isSelected]);
        Inc(Indx);
    end;

    Assert(Indx = Length(EntryIDList));

    MAPIProgrs := TMAPIProgress.Create(Self, ' Deleting messages ');
    Try
        DeleteMapiMessages(MAPIFolder, EntryIDList, nil, MAPIProgrs, Self.Handle);
    Finally
        MAPIProgrs := nil;
    End;
End;

Top


procedure CopyOrMoveMapiMessages(const SourceFolder: IMAPIFolder; const TargetFolder: IMAPIFolder; const MsgEntryIdList: array of TBytes; const ActionMove: Boolean = False; const Progress: IMAPIProgress = nil; const DialogHandle: UINT_PTR = 0);

description
The CopyOrMoveMapiMessages method copy or move one or more messages from the current folder to another folder

parameters

SourceFolder - IMAPIFolder where resides messages
TargetFolder - IMAPIFolder - destination folder
MsgEntryIdList - Array of messages EntryID for copy/move (unique message ID - PR_ENTRYID)
ActionMove - Boolean. If it is True, messages will be moved.
Progress: IMAPIProgress. A rogress object that displays a progress indicator
DialogHandle - A handle to the parent window of the progress indicator 

usage

var
  EntryIDList: array of TBytes;
begin

    // Get number of selected messages
   
Count := MessageListView.SelCount;
    if Count = 0 then
    Exit;

    Indx:=0;

    SetLength(EntryIDList, Count);
   
    // Get first selected ListView item

    Item := MessageListView.Selected;

    while Item <> nil do
    begin
        ItemIndex := Item.Index;
        MsgHead := ShellItem(ItemIndex);
       
         SetLength(EntryIDList[Indx], Length(MsgHead^.ID));
         Move(MsgHead^.ID[0], EntryIDList[Indx][0], Length(MsgHead^.ID));
        
        Item := MessageListView.GetNextItem(Item, sdAll, [isSelected]);
        Inc(Indx);
    end;

    Assert(Indx = Length(EntryIDList));

    MAPIProgrs := TMAPIProgress.Create(Self, ' Deleting messages ');
    Try
       
// Move selected
        CopyOrMoveMapiMessages(SourceFolder, DestinationFolder, EntryIDList, True, MAPIProgrs, Self.Handle);
    Finally
        MAPIProgrs := nil;
    End;
End;

Top


procedure EmptyMapiFolder(const MAPIFolder: IMAPIFolder; const Progress: IMAPIProgress = nil; const DialogHandle: UINT_PTR = 0);

description
The EmptyMapiFolder method deletes all messages and subfolders from a folder without deleting the folder itself

parameters

MAPIFolder - IMAPIFolder
Progress: IMAPIProgress. A progress object that displays a progress indicator
DialogHandle - A handle to the parent window of the progress indicator 

usage

 EmptyMapiFolder(MAPIFolder);

Top


procedure DeleteMapiSubFolder(const MAPIFolder: IMAPIFolder; const SubFldEntryId: TBytes; const Progress: IMAPIProgress = nil; const DialogHandle: UINT_PTR = 0);

description
The DeleteMapiSubFolder method deletes subfolder from a folder

parameters

MAPIFolder - IMAPIFolder. Parent folder
SubFldEntryId - Unique ID of subfolder for deleting (PR_ENTRYID)
Progress: IMAPIProgress. A progress object that displays a progress indicator
DialogHandle - A handle to the parent window of the progress indicator 

usage

 DeleteMapiSubFolder (ParentFolder, SubFolderEntryID);

Top


function CreateMapiSubFolder(const MAPIFolder: IMAPIFolder; const FldType: TMAPIFldType; const FolderName: String; const FolderComment: String = ''; const ContainerClass: String = ''): IMAPIFolder;

description
The CreateMapiSubFolder creates a new subfolder.

parameters

MAPIFolder - IMAPIFolder. Parent folder
FldType - TMAPIFldType - valid values: oFolderCalendar .. oFolderUndefined
FolderName - String that contains the name for the new folder
FolderComment - string that contains a comment associated with the new folder
ContainerClass - string that contains a custom container class as "IPF.Note.IMI.ReportToMicro". Valid only if FldType is oFolderUndefined 

usage

with TFormNewFolder.Create(Self) do
begin
    if ShowModal = mrOk then
    begin
        FldName := FolderName;
        FldComment := FolderComment;
        FldStrType := FolderType;
    end;
    Free;
end;

NewFolder := nil;

if (FldName <> '') then
begin
    FldType := GetMAPIFolderKnowType(FldStrType);
    NewFolder := CreateMapiSubFolder(Folder, FldType, FldName, FldComment);
end;

Top


procedure CopyOrMoveMapiSubFolder(const MAPIFolder: IMAPIFolder; const SubFldEntryId: TBytes; const DestFolder: IMAPIFolder; const ActionMove: Boolean = False; const NewFolderName: string = ''; const SubFoldersToo: Boolean = True; const Progress: IMAPIProgress = nil; const DialogHandle: UINT_PTR = 0);

description
Copies/moves the folder specified thru SubFldEntryId to the destination folder.

parameters

MAPIFolder - IMAPIFolder. Parent folder
SubFldEntryId - Unique folder ID for copying / moving  (PR_ENTRYID)
DestFolder -IMAPIFolder. The destination folder for the copied/moved folder
ActionMove - Boolean. If it is True, subfolder defined by the folder SubFldEntryId will be moved
SubFoldersToo -Boolean. If it is True, all subfolders will be copied/moved
Progress: IMAPIProgress. A progress object that displays a progress indicator
DialogHandle - A handle to the parent window of the progress indicator 

usage

procedure CopyOrMoveFolder(const ActionMove: Boolean);
var
    MAPIProgrs: IMAPIProgress;
    Node: TTreeNode;
    mr: Integer;
    SourceParentFolder, DestFolder: IMAPIFolder;
    DestinationEntryID, DestinationStoreID: TBytes;
    DestStore: IMsgStore;
begin
    Node := MailboxTreeView.Selected;
    if not Assigned(Node) or (Node.Level < 1) then
    Exit;

    SourceParentFolder := nil;
    DestFolder := nil;
    DestStore := nil;
    MAPIProgrs := nil;

    with TFolderCopyMove.Create(Self) do
    begin
        try
            SetSession(MAPISession);
            Caption := ' Copy folder';
            lbAction.Caption := 'Copy to folder:';

            mr := ShowModal;
            if (mr = mrOk) and (Length(TargetEntryID) > 0) then
            begin
                SetLength(DestinationEntryID, Length(TargetEntryID));
                Move(TargetEntryID[0], DestinationEntryID[0], Length(TargetEntryID));
                SetLength(DestinationStoreID, Length(TargetStoreID));
                Move(TargetStoreID[0], DestinationStoreID[0], Length(TargetStoreID));
            end
            else
                mr := mrCancel;
        finally
            Free;
        end;
    end;

    if mr <> mrOk then
    Exit;

    // We need a parent folder
    SourceParentFolder := GetMAPIFolder(MAPIStore, PFolderHead(Node.Data)^.ParentID);

    if IsSameTBytes(DestinationStoreID, PFolderHead(Node.Data)^.StoreID) then
    begin
        DestFolder := GetMAPIFolder(MAPIStore, DestinationEntryID);
    end
    else
    begin
        DestStore := GetMAPIStore(MAPISession, DestinationStoreID);
        DestFolder := GetMAPIFolder(DestStore, DestinationEntryID);
    end;

    MAPIProgrs := TMAPIProgress.Create(Self, ' Copying folder ' + Node.Text);
    Try
        CopyOrMoveMapiSubFolder(SourceParentFolder, PFolderHead(Node.Data)^.ID, DestFolder, ActionMove, '', True, MAPIProgrs, Self.Handle);
    Finally
        MAPIProgrs := nil;
        if Assigned(DestStore) then
            ReleaseMsgStore(DestStore);
    End;

end;

Top


function GetFldIconIndex(const FldType: TMAPIFldType): Integer;

description
Returns a number that indicates which icon to use when you display a folder

parameters

FldType - TMAPIFldType

usage

for iCount := 0 to Length(FolderList) - 1 do
begin
    New(FolderHead);
    CopyFolderHead(@FolderList[iCount], FolderHead);

    TreeNode := MailboxTreeView.Items.AddChildObject(StoreNode, FolderHead^.DisplayName, FolderHead);

    TreeNode.ImageIndex := GetFldIconIndex(FolderHead.FolderType);
    TreeNode.SelectedIndex := TreeNode.ImageIndex;

    // Add Dummy Node for [+] Icon
    if FolderHead.HasSubFolders then
        MailboxTreeView.Items.AddChildObject(TreeNode, '', nil);
end;

Top


function GetMsgIconIndex(const MsgHead: TMsgHead): Integer;

description
Returns a number that indicates which icon to use when you display a message

parameters

MsgHead - TMsgHead

usage

// Render messages inside ListView
procedure
MessageListViewData(Sender: TObject; Item: TListItem);
begin

    if Item.Index > MsgHeadListCount - 1 then Exit;

    Item.ImageIndex := GetMsgIconIndex(MsgHeadList[Item.Index]);
    Item.SubItems.Add('');
    if MsgHeadList[Item.Index].HasAttachment then
        Item.SubItemImages[0] := 0
    else
        Item.SubItemImages[0] := -1;

    Item.SubItems.Add(MsgHeadList[Item.Index].Sender);
    Item.SubItems.Add(MsgHeadList[Item.Index].Subject);
    Item.SubItems.Add(DateTimeToStr(MsgHeadList[Item.Index].SentTime));
    Item.SubItems.Add(ShowCustomSize(MsgHeadList[Item.Index].Size));
end;

Top


function CalculateMsgIconIndex(const IconIndex: Integer; MsgClass: WideString; MsgFlags: ULONG): Integer;

description
For internal use

Top


function IsSpecialFolder(const MsgStore: IMsgStore; const MAPIFolder: IMAPIFolder): Boolean; overload;
function IsSpecialFolder(const MsgStore: IMsgStore; const FldEntryId: TBytes): Boolean; overload;

description
Returns True if folder is a special (system) folder as Inbox, Sent Item, Deleted Items, Outbox, Calendar, etc

usage

Node := MailboxTreeView.Selected;

if IsSpecialFolder(MAPIStore, PFolderHead(Node.Data)^.ID) then
begin
    MessageDlg('Warning!!! '#13#10#13#10'You cannot delete a system folder!', mtError, [mbOK], 0);
    Exit;
end;

Top


function ShowCustomSize(Value: Cardinal): string;

description
For internal use

Top


function GetMAPIFoldersTable(const MsgStore: IMsgStore): IMAPITable; overload;
function GetMAPIFoldersTable(const MAPIFolder: IMAPIFolder): IMAPITable; overload;

description
Returns MAPI Table with subfolders information.

The output table contains the following columns:
  PR_INSTANCE_KEY
  PR_ENTRYID
  PR_LONGTERM_ENTRYID_FROM_TABLE
  PR_PARENT_ENTRYID
  PR_STORE_ENTRYID
  PR_MDB_PROVIDER
  PR_MAPPING_SIGNATURE
  PR_STORE_SUPPORT_MASK
  PR_FOLDER_TYPE
  PR_CONTAINER_CLASS
  PR_DISPLAY_NAME
  PR_COMMENT
  PR_CONTENT_COUNT
  PR_CONTENT_UNREAD
  PR_SUBFOLDERS
  PR_ATTR_HIDDEN
  PR_ACCESS

Top


function GetMAPISpecialFoldersList(const MsgStore: IMsgStore): TSpecialFoldersList

description
Returns an array of the type of the folder (TMAPIFldType) and its unique ID (PR_ENTRYID).
The array can contain information about the Inbox, Outbox, Sent items, Drafts, Contacts, Calendar and other system folders.

Top


procedure ClearMAPISpecialFoldersList(var SpecialFoldersList: TSpecialFoldersList);

description
Clears and releases the used memory of TSpecialFoldersList

Top


Copyright 2017 IMIBO
Privacy Statement