|
|
|
date: Tue, 8 Jul 2008 13:19:00 -0700,
group: microsoft.public.win32.programmer.messaging
back
Re: Avoid MAPI Authentication Dialogs
On 8 jul, 22:19, jwu wrote:
> 1. CredUI on calling IMsgServiceAdmin::ConfigureMsgService when creating a
> profile, I have the username and password, is there a way to avoid this
> dialog?
>
> 2. CredUI on calling IMAPISession::OpenMsgStore with store type set to
> "\x54\x94\xA1\xC0\x29\x7F\x10\x1B\xA5\x87\x08\x00\x2B\x2A\x25\x17", I am
> using the profile just created to get back the IMAPISession, is there a way
> to avoid this dialog?
>
> Thanks.
Hi jwu,
I'm using this code to create a profile (code is not mine, but can't
remember where I got it). It works perfectly, without asking anything.
It's in Delphi, but you'll easily translate it to C++.
function TProfileCreator.CreateProfileAndLogOn(const
ProfileName,UserName,
ServerName:PChar): bool;
var
hr:HRESULT;
ulFlags:ULONG;
lppProfAdmin:IPROFADMIN;
lpServiceAdmin:IMsgServiceAdmin;
lppTable:IMAPITABLE;
pRows:PSRowSet;
lpProp : PSPropValueArray;
I:byte;
MsgStoreUID :PMAPIUID;
lppMAPIError:pMAPIError;
MAPIINIT:TMAPIINIT;
label cleanup;
begin
pRows:=nil;
ulFlags := MAPI_NEW_SESSION or
MAPI_EXTENDED or
MAPI_NO_MAIL or // We will not send or receive mails
MAPI_NT_SERVICE or //
MAPI_BEST_ACCESS;
MapiInit.ulVersion := MAPI_INIT_VERSION;
MapiInit.ulFlags := MAPI_NT_SERVICE;
hr:=mapiInitialize(@MAPIINIT);
if failed(hr) then
LogMessage('mapiInitialize HR: '+IntToStr(HR));
MAPIINITOK:=hr=S_OK;
szProfileName:=ProfileName;
{
GetMem(szProfileName,MAX_PATH+1);
// Create a unique profile name
hr:=HrCreateProfileName('Test', MAX_PATH, szProfileName);
if failed(hr) then
LogMessage('HrCreateProfileName HR: '+IntToStr(HR)) ;
else
LogMessage('HrCreateProfileName : '+szProfileName,
EVENTLOG_INFORMATION_TYPE); }
if MAPIINITOK then
begin
// Get pointer to administration object - IPROFADMIN interface
hr:=MAPIAdminProfiles(0,lppProfAdmin);
if failed(hr) then
begin
LogMessage('MAPIAdminProfiles HR: '+IntToStr(HR));
goto Cleanup;
end;
// We first try to delete profile (if exist)
lppProfAdmin.DeleteProfile(szProfileName, 0); //HR CAN BE<>S_OK !!!
//Creates a new MAPI profile for service
hr:=lppProfAdmin.CreateProfile(szProfileName, nil, 0, 0);
if failed(hr) then
begin
LogMessage('lppProfAdmin.CreateProfile HR: '+IntToStr(HR));
goto cleanup;
end;
hr:=lppProfAdmin.AdminServices(szProfileName,nil,
0,0,lpServiceAdmin);
if failed(hr) then
begin
LogMessage('lppProfAdmin.AdminServices HR: '+IntToStr(HR));
goto cleanup;
end;
hr:=lpServiceAdmin.CreateMsgService('MSEMS','MS Exchange',0,0);
if failed(hr) then
begin
LogMessage('lpServiceAdmin.CreateMsgService HR: '+IntToStr(HR));
goto cleanup;
end;
hr:=lpServiceAdmin.GetMsgServiceTable(0,lppTable);
if(FAILED(hr)) then
begin
LogMessage('lpServiceAdmin.GetMsgServiceTable HR: '+IntToStr(HR));
goto cleanup;
end;
hr:=lppTable.QueryRows(1,0,pRows);
if(FAILED(hr)) then
begin
LogMessage('lppTable.QueryRows HR: '+IntToStr(HR));
goto cleanup;
end;
for I:=0 to pRows.aRow[0].cValues-1 do
if (pRows.aRow[0].lpProps[I].ulPropTag = PR_SERVICE_UID) then
begin
MsgStoreUID := PMAPIUID(pRows.aRow[0].lpProps[I].Value.bin.lpb);
break;
end;
I:=0;
MAPIAllocateBuffer(sizeof(TSPropValueArray)
+sizeof(TSPropValue)*2,Pointer(lpProp));
MAPIAllocateMore(StrLen(UserName)
+1,Pointer(lpProp),Pointer(lpProp[0].Value.lpszA));
MAPIAllocateMore(StrLen(ServerName)
+1,Pointer(lpProp),Pointer(lpProp[I+1].Value.lpszA));
MAPIAllocateMore(StrLen(UserName)+1,Pointer(lpProp),Pointer(lpProp[I
+2].Value.lpszA));
lpProp[0].ulPropTag := PR_PROFILE_UNRESOLVED_NAME;
StrCopy(lpProp[0].Value.lpszA,UserName);
lpProp[I+1].ulPropTag := PR_PROFILE_UNRESOLVED_SERVER;
StrCopy(lpProp[I+1].Value.lpszA,ServerName);
lpProp[I+2].ulPropTag := PR_PROFILE_MAILBOX;
StrCopy(lpProp[I+2].Value.lpszA,UserName);
hr:=lpServiceAdmin.ConfigureMsgService(
MsgStoreUID,
0, 0,3, PSPropValue(lpProp));
if(FAILED(hr)) then
begin .....
cleanup:
if assigned(pRows) then FreePRows(pRows);
lppTable:=nil;
if assigned(lppMAPIError) then lppMAPIError:=nil;
lpServiceAdmin:=nil;
lppProfAdmin:=nil;
Result:=hr=S_OK;
date: Wed, 9 Jul 2008 03:00:05 -0700 (PDT)
author: patochem
Re: Avoid MAPI Authentication Dialogs
On 8 jul, 23:23, jwu wrote:
> I thought about that, but running the application process under mailbox user
> account is not an option.
>
> "SvenC" wrote:
> > Hi jwu,
>
> > > 1. CredUI on calling IMsgServiceAdmin::ConfigureMsgService when
> > > creating a profile, I have the username and password, is there a way
> > > to avoid this dialog?
>
> > Run your application in a user account with full access to the mailbox
> > you connect to.
>
> > > 2. CredUI on calling IMAPISession::OpenMsgStore with store type set to
> > > "\x54\x94\xA1\xC0\x29\x7F\x10\x1B\xA5\x87\x08\x00\x2B\x2A\x25\x17", I
> > > am using the profile just created to get back the IMAPISession, is
> > > there a way to avoid this dialog?
>
> > Same as 1)
>
> > --
> > SvenC
Jwu,
Here is the Delphi snipet I use to create profiles. In my case, it
doesn't ask for any credentials. The code is not mind, and I can't
remember from who I took it.
Hope it can helps.
function TProfileCreator.CreateProfileAndLogOn(const
ProfileName,UserName,
ServerName:PChar): bool;
var
hr:HRESULT;
ulFlags:ULONG;
lppProfAdmin:IPROFADMIN;
lpServiceAdmin:IMsgServiceAdmin;
lppTable:IMAPITABLE;
pRows:PSRowSet;
lpProp : PSPropValueArray;
I:byte;
MsgStoreUID :PMAPIUID;
lppMAPIError:pMAPIError;
label cleanup;
begin
szProfileName:=ProfileName;
if MAPIINITOK then // Mapi initialization routine here as
MAPI_NT_SERVICE
begin
// Get pointer to administration object - IPROFADMIN interface
hr:=MAPIAdminProfiles(0,lppProfAdmin);
if failed(hr) then
begin
LogMessage('MAPIAdminProfiles HR: '+IntToStr(HR));
goto Cleanup;
end;
// We first try to delete profile (if exist)
lppProfAdmin.DeleteProfile(szProfileName, 0); //HR CAN BE<>S_OK !!!
//Creates a new MAPI profile for service
hr:=lppProfAdmin.CreateProfile(szProfileName, nil, 0, 0);
if failed(hr) then
begin
LogMessage('lppProfAdmin.CreateProfile HR: '+IntToStr(HR));
goto cleanup;
end;
hr:=lppProfAdmin.AdminServices(szProfileName,nil,
0,0,lpServiceAdmin);
if failed(hr) then
begin
LogMessage('lppProfAdmin.AdminServices HR: '+IntToStr(HR));
goto cleanup;
end;
hr:=lpServiceAdmin.CreateMsgService('MSEMS','MS Exchange',0,0);
if failed(hr) then
begin
LogMessage('lpServiceAdmin.CreateMsgService HR: '+IntToStr(HR));
goto cleanup;
end;
hr:=lpServiceAdmin.GetMsgServiceTable(0,lppTable);
if(FAILED(hr)) then
begin
LogMessage('lpServiceAdmin.GetMsgServiceTable HR: '+IntToStr(HR));
goto cleanup;
end;
hr:=lppTable.QueryRows(1,0,pRows);
if(FAILED(hr)) then
begin
LogMessage('lppTable.QueryRows HR: '+IntToStr(HR));
goto cleanup;
end;
for I:=0 to pRows.aRow[0].cValues-1 do
if (pRows.aRow[0].lpProps[I].ulPropTag = PR_SERVICE_UID) then
begin
MsgStoreUID := PMAPIUID(pRows.aRow[0].lpProps[I].Value.bin.lpb);
break;
end;
I:=0;
MAPIAllocateBuffer(sizeof(TSPropValueArray)
+sizeof(TSPropValue)*2,Pointer(lpProp));
MAPIAllocateMore(StrLen(UserName)
+1,Pointer(lpProp),Pointer(lpProp[0].Value.lpszA));
MAPIAllocateMore(StrLen(ServerName)
+1,Pointer(lpProp),Pointer(lpProp[I+1].Value.lpszA));
MAPIAllocateMore(StrLen(UserName)+1,Pointer(lpProp),Pointer(lpProp[I
+2].Value.lpszA));
lpProp[0].ulPropTag := PR_PROFILE_UNRESOLVED_NAME;
StrCopy(lpProp[0].Value.lpszA,UserName);
lpProp[I+1].ulPropTag := PR_PROFILE_UNRESOLVED_SERVER;
StrCopy(lpProp[I+1].Value.lpszA,ServerName);
lpProp[I+2].ulPropTag := PR_PROFILE_MAILBOX;
StrCopy(lpProp[I+2].Value.lpszA,UserName);
hr:=lpServiceAdmin.ConfigureMsgService(
MsgStoreUID,
0, 0,3, PSPropValue(lpProp));
if(FAILED(hr)) then
begin
lppMAPIError:=nil;
lpServiceAdmin.GetLastError(hr,0,lppMAPIError);
if assigned(lppMAPIError) then
if assigned(lppMAPIError.lpszError) then
LogMessage(lppMAPIError.lpszError) else
LogMessage('lpServiceAdmin.ConfigureMsgService Error')
else LogMessage('lpServiceAdmin.ConfigureMsgService Error');
goto cleanup;
end;
FMapiSession:=nil;
hr:=MAPILogonEx(0, szProfileName, nil,
ulFlags,
MapiX.IMAPISession(FMapiSession));
if(FAILED(hr)) then begin
LogMessage('Error MAPILogonEx HR: '+IntToStr(HR));
goto cleanup;
end;
end;
cleanup:
if assigned(pRows) then FreePRows(pRows);
lppTable:=nil;
if assigned(lppMAPIError) then lppMAPIError:=nil;
lpServiceAdmin:=nil;
lppProfAdmin:=nil;
Result:=hr=S_OK;
end;
PS: Sorry if double post (it looked like my first one didn't get
through)
date: Wed, 9 Jul 2008 03:32:57 -0700 (PDT)
author: patochem
|
|