Extended MAPI in DELPHI
LazyMAPI # 3
Test Microsoft Office Outlook Account Management (read-only)
A “test project” – how to use the IOlkAccountManger to get email accounts configured in Outlook ( read-only mode).
Code snip:
procedure TFrmAccounts.GetAccountManager;
var
iCount, cCount: Integer;
begin
oMAPIAccountManager := TMAPIAccountManager.Create(MAPISession);
cCount := oMAPIAccountManager.Count;
StatusBar.SimpleText := ' Accounts count: ' + IntToStr(cCount);
if cCount < 1 then
Exit;
for iCount := 0 to cCount - 1 do
cbAccounts.Items.Add(oMAPIAccountManager.Account[iCount].AccountName);
btDisplayAccounts.Enabled := True;
btNewAccount.Enabled := True;
end;
procedure TFrmAccounts.btDisplayAccountsClick(Sender: TObject);
begin
oMAPIAccountManager.DisplayAccountList(Self.Handle);
end;
procedure TFrmAccounts.btLogOnClick(Sender: TObject);
begin
if btLogOn.Tag = 0 then
// Log On
begin
// Get MAPI Session
MAPISession := GetMAPISession(Self.Handle, '', MAPI_LOGON_UI);
if Assigned(MAPISession) then
GetAccountManager;
end
else
// Log Off
begin
btDisplayAccounts.Enabled := False;
btNewAccount.Enabled := False;
ClearAll;
if Assigned(oMAPIAccountManager) then
FreeAndNil(oMAPIAccountManager);
// Close and clear MAPI Session
ReleaseMapiSession(MAPISession);
end;
btLogOn.Tag := Integer(Assigned(MAPISession));
btLogOn.Caption := strLogOff[Bool(btLogOn.Tag)];
end;
procedure TFrmAccounts.btNewAccountClick(Sender: TObject);
begin
oMAPIAccountManager.DisplayAddNewAccountWizard(Self.Handle);
end;
procedure TFrmAccounts.cbAccountsChange(Sender: TObject);
var
ProfileAccount: TMAPIProfileAccount;
begin
if cbAccounts.ItemIndex < 0 then
Exit;
ProfileAccount := oMAPIAccountManager.Account[cbAccounts.ItemIndex];
ebUserName.Text := ProfileAccount.UserDisplayName;
ebUserEmailAddress.Text := ProfileAccount.EmailID;
cbAccountType.ItemIndex := Integer(ProfileAccount.AccountType);
ebIncomingServer.Text := ProfileAccount.IncomingServer;
ebOutgoingServer.Text := ProfileAccount.OutgoingServer;
ebLogonUserName.Text := ProfileAccount.IncomingLoginName;
cbSPA.Checked := ProfileAccount.UseIncomingSPA;
ebMailAccount.Text := ProfileAccount.AccountName;
ebOrganization.Text := ProfileAccount.Organization;
ebReplayEmail.Text := ProfileAccount.POP3ReplayAddress;
cbSMTPReqAuth.Checked := ProfileAccount.UseOutgoingUseAuthentication;
ebUserNameSMTP.Text := ProfileAccount.OutgoingLoginName;
cbOutgoingSPA.Checked := ProfileAccount.UseOutgoingSPA;
ebPOP3Port.Text := IntToStr(ProfileAccount.IncomingPort);
cbIncomingSSL.Checked := ProfileAccount.UseIncomingSSL;
ebSMTPPort.Text := IntToStr(ProfileAccount.OutgoingPort);
cbEncrType.ItemIndex := ProfileAccount.SMTPEncryptConnType;
ebServerTimeOuts.Text := IntToStr(ProfileAccount.TimeOut);
ebUserNameSMTP.Text := ProfileAccount.OutgoingLoginName;
rbUseSameSettings.Checked :=
ProfileAccount.OutgoingServerAuthenticationType = 0;
rbSMTPLogOnUsing.Checked :=
ProfileAccount.OutgoingServerAuthenticationType = 1;
rbLogonBefore.Checked := ProfileAccount.OutgoingServerAuthenticationType = 2;
cbRememberSMTPPassword.Checked := ProfileAccount.OutgoingRememberPassword;
seDays.Value := HiWORD(ProfileAccount.AccountFlags and $FFFF0000);
cbLeaveCopy.Checked := Bool(LoWORD(ProfileAccount.AccountFlags and $0000FFFF) and 1);
cbRemoveFromServer.Checked := Bool(LoWORD(ProfileAccount.AccountFlags and $0000FFFF) and 2);
cbRemoveDeleted.Checked := Bool(LoWORD(ProfileAccount.AccountFlags and $0000FFFF) and 4);
end;
procedure TFrmAccounts.ClearAll;
procedure WalkChildren(Parent: TWinControl);
var
i: Integer;
Child: TControl;
begin
for i := 0 to Parent.ControlCount-1 do
begin
Child := Parent.Controls[i];
if Child is TEdit then
TEdit(Child).Text:='';
if Child is TComboBox then
TComboBox(Child).ItemIndex:=-1;
if Child is TWinControl then
WalkChildren(TWinControl(Child));
end;
end;
begin
cbAccounts.Items.Clear;
WalkChildren(PageControl1);
StatusBar.SimpleText := '';
end;
procedure TFrmAccounts.FormClose(Sender: TObject; var Action: TCloseAction);
begin
btDisplayAccounts.Enabled := False;
btNewAccount.Enabled := False;
cbAccounts.Items.Clear;
if Assigned(oMAPIAccountManager) then
FreeAndNil(oMAPIAccountManager);
ReleaseMapiSession(MAPISession);
end;
procedure TFrmAccounts.FormCreate(Sender: TObject);
begin
{$IF DEFINED (WIN64)}
Self.Caption := Self.Caption + ' - WIN64';
{$ELSE}
Self.Caption := Self.Caption + ' - WIN32';
{$IFEND}
oMAPIAccountManager := nil;
MAPISession := nil;
end;

