unit uMain; interface {$I IMI.INC} uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ExtendedMAPI, ComCtrls; type TfrmMain = class(TForm) Panel1: TPanel; btLogOff: TButton; MessageListView: TListView; StatusBar: TStatusBar; rgFolders: TRadioGroup; btCreateMsg: TButton; procedure btLogOffClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure MessageListViewData(Sender: TObject; Item: TListItem); procedure MessageListViewDblClick(Sender: TObject); procedure rgFoldersClick(Sender: TObject); procedure btCreateMsgClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } MAPISession: IMAPISession; MsgStore: IMsgStore; Inbox: IMAPIFolder; SentBox: IMAPIFolder; DraftBox: IMAPIFolder; procedure LoadInboxMessages; procedure LoadSentItemsMessages; procedure LoadDraftItems; procedure ReleaseAll; public { Public declarations } end; var frmMain: TfrmMain; implementation uses IMIEMTypes, MAPISessionUtils, MAPIFldUtils, MAPIMsgUtils, MessageFrm, NewMessageFrm; {$R *.dfm} const strLogOff: array [Boolean] of string = ('Log On', 'Log Off'); var MsgHeadList: TMsgHeadList = nil; MsgHeadListCount: Integer = 0; procedure TfrmMain.btCreateMsgClick(Sender: TObject); var MAPIMessage: IMessage; begin if rgFolders.ItemIndex <> 2 then rgFolders.ItemIndex := 2; if not Assigned(DraftBox) then Exit; // Create a new message in Draft items folder MAPIMessage := CreateMapiMessage(DraftBox); if not Assigned(MAPIMessage) then Exit; try with TfrmNewMsg.Create(Self) do begin SetMAPIObjects(MAPISession, MsgStore, DraftBox, MAPIMessage); ShowModal; Free; end; finally MAPIMessage := nil; end; // refresh a Draft Items LoadDraftItems; end; procedure TfrmMain.btLogOffClick(Sender: TObject); begin if btLogOff.Tag = 0 then // Log On begin // Get MAPI Session MAPISession := GetMAPISession(Self.Handle, '', MAPI_LOGON_UI); // Get default message store for current MAPI/Outlook profile MsgStore := GetDefaultStore(MAPISession); // Get InBox, SentBox or DraftBox rgFoldersClick(Self); end else // Log Off begin // Clear all MAPI Interfaces ReleaseAll; // Close and clear MAPI Session ReleaseMapiSession(MAPISession); end; btLogOff.Tag := Integer(Assigned(MAPISession)); btLogOff.Caption := strLogOff[Bool(btLogOff.Tag)]; end; procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction); begin // Clear all MAPI Interfaces ReleaseAll; // Close and clear MAPI Session ReleaseMapiSession(MAPISession); end; procedure TfrmMain.FormCreate(Sender: TObject); begin {$IF DEFINED (WIN64)} Self.Caption := Self.Caption + ' - WIN64'; {$ELSE} Self.Caption := Self.Caption + ' - WIN32'; {$IFEND} MessageListView.Items.Count := 0; end; procedure TfrmMain.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; procedure TfrmMain.LoadSentItemsMessages; begin // Get Sent items if not Assigned(SentBox) then SentBox := GetSentBox(MsgStore); // Get list of messages MsgHeadList := GetMessageList(SentBox); MsgHeadListCount := Length(MsgHeadList); StatusBar.SimpleText := 'Message Count: ' + IntToStr(MsgHeadListCount) + ' in Sent items'; // Force virtual ListView to render items MessageListView.Items.Count := MsgHeadListCount; MessageListView.Refresh; end; procedure TfrmMain.LoadDraftItems; begin // Get draft items if not Assigned(DraftBox) then DraftBox := GetDefaultFolder(MsgStore, oFolderDrafts, True, False); // Get list of messages MsgHeadList := GetMessageList(DraftBox); MsgHeadListCount := Length(MsgHeadList); StatusBar.SimpleText := 'Message Count: ' + IntToStr(MsgHeadListCount) + ' in Draft items'; // Force virtual ListView to render items MessageListView.Items.Count := MsgHeadListCount; MessageListView.Refresh; end; procedure TfrmMain.MessageListViewData(Sender: TObject; Item: TListItem); begin // Render virtual ListView if Item.Index > MsgHeadListCount - 1 then Exit; Item.Caption := BoolToStr(MsgHeadList[Item.Index].HasAttachment, True); Item.SubItems.Add(MsgHeadList[Item.Index].Subject); Item.SubItems.Add(MsgHeadList[Item.Index].Sender); Item.SubItems.Add(DateTimeToStr(MsgHeadList[Item.Index].SentTime)); Item.SubItems.Add(ShowCustomSize(MsgHeadList[Item.Index].Size)); end; procedure TfrmMain.MessageListViewDblClick(Sender: TObject); var MAPIMessage: IMessage; Index: Integer; isUnSent: Boolean; begin if not Assigned(MessageListView.Selected) then Exit; Index := MessageListView.Selected.Index; // Get MAPI Message from folder by EntryID case rgFolders.ItemIndex of 0: MAPIMessage := GetMapiMessage(Inbox, MsgHeadList[Index].ID); 1: MAPIMessage := GetMapiMessage(SentBox, MsgHeadList[Index].ID); 2: MAPIMessage := GetMapiMessage(DraftBox, MsgHeadList[Index].ID, False); end; if not Assigned(MAPIMessage) then Exit; // Whether the message is still a draft? isUnSent := Bool(MsgHeadList[Index].Flags and MSGFLAG_UNSENT); try if isUnSent and (rgFolders.ItemIndex = 2) then // Open a compose message with TfrmNewMsg.Create(Self) do begin SetMAPIObjects(MAPISession, MsgStore, DraftBox, MAPIMessage); ShowModal; Free; end else // open a received or sent message with TfrmMessage.Create(Self) do begin SetMessage(MAPIMessage); ShowModal; end; finally // clear interface MAPIMessage := nil; end; if (rgFolders.ItemIndex = 2) then // refresh a Draft Items LoadDraftItems; end; procedure TfrmMain.ReleaseAll; begin MessageListView.Items.Count := 0; MsgHeadList := nil; DraftBox := nil; SentBox := nil; Inbox := nil; // Close message store ReleaseMsgStore(MsgStore); MsgStore := nil; end; procedure TfrmMain.rgFoldersClick(Sender: TObject); begin if not Assigned(MAPISession) then Exit; case rgFolders.ItemIndex of 0: LoadInboxMessages; 1: LoadSentItemsMessages; 2: LoadDraftItems; end; end; end.
Copyright © 1999 - 2021 IMIBO |