You are currently offline, waiting for your internet to reconnect

Impersonation of user in code may cause Outlook to stop responding or cause "Method Failed" error message

Support for Office 2003 has ended

Microsoft ended support for Office 2003 on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

This article has been archived. It is offered "as is" and will no longer be updated.
When you run code that impersonates a user to access a mailbox, Microsoft Office Outlook 2007, Outlook 2003, or Outlook 2002 may stop responding or you may receive an error message that is similar to the following:
Method '~' of object '~' failed
This problem may occur if new threads are created when you impersonate a user. MAPI and Collaboration Data Objects (CDO) create new threads as a normal function, and these new threads do not inherit full access to the impersonated users token.

Note Impersonation with MAPI is not a supported behavior.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure. However, they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

To work around this problem, split your code into two separate programs.

For example, the first program should do the impersonation by using LogonUser, and then start the second program as a separate process by using CreateProcessAsUser as in the following sample Visual Basic code:
'<-- Launcher programConst LOGON32_LOGON_INTERACTIVE = 2Const LOGON32_PROVIDER_DEFAULT = 0Const CREATE_DEFAULT_ERROR_MODE = &H4000000Private Type STARTUPINFO   cb As Long   lpReserved As Long ' !!! must be Long for Unicode string   lpDesktop As Long  ' !!! must be Long for Unicode string   lpTitle As Long    ' !!! must be Long for Unicode string   dwX As Long   dwY As Long   dwXSize As Long   dwYSize As Long   dwXCountChars As Long   dwYCountChars As Long   dwFillAttribute As Long   dwFlags As Long   wShowWindow As Integer   cbReserved2 As Integer   lpReserved2 As Long   hStdInput As Long   hStdOutput As Long   hStdError As LongEnd TypePrivate Type PROCESS_INFORMATION   hProcess As Long   hThread As Long   dwProcessId As Long   dwThreadId As LongEnd TypePrivate Declare Function CreateProcessAsUser Lib "advapi32.dll" _      Alias "CreateProcessAsUserA" _      (ByVal hToken As Long, _      ByVal lpApplicationName As Long, _      ByVal lpCommandLine As String, _      ByVal lpProcessAttributes As Long, _      ByVal lpThreadAttributes As Long, _      ByVal bInheritHandles As Long, _      ByVal dwCreationFlags As Long, _      ByVal lpEnvironment As Long, _      ByVal lpCurrentDirectory As String, _      lpStartupInfo As STARTUPINFO, _      lpProcessInformation As PROCESS_INFORMATION) As LongPrivate Declare Function LogonUser Lib "advapi32.dll" Alias "LogonUserA" ( _      ByVal lpszUsername As String, _      ByVal lpszDomain As String, _      ByVal lpszPassword As String, _      ByVal dwLogonType As Long, _      ByVal dwLogonProvider As Long, _      phToken As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" ( _      ByVal hObject As Long) As LongPrivate Sub Command1_Click()      Dim hToken As Long      Dim ulResult As Long      Dim startup As STARTUPINFO      Dim process As PROCESS_INFORMATION          ulResult = LogonUser("ImpersonatedUsed", "ImpersonatedDomain", _         "ImpersonatedUserPassword", LOGON32_LOGON_INTERACTIVE, _            LOGON32_PROVIDER_DEFAULT, hToken)      If ulResult = 0 Then         MsgBox "Error in LogonUser:  " & Err.LastDllError         Exit Sub      End If      startup.cb = Len(startup)      'TODO: Replace 'mail.exe' with the name of the program you wish to start      ulResult = CreateProcessAsUser(hToken, 0&, "mail.exe", 0&, 0&, _         False, CREATE_DEFAULT_ERROR_MODE, 0&, "path", startup, process)      If ulResult = 0 Then         MsgBox "Error in CreateProcessAsUser:  " & Err.LastDllError         Exit Sub      End If          CloseHandle hToken    End Sub'<-- End of first program '<-- Start of second process'<-- Mail.exe project:Private Sub Command1_Click()On Error GoTo ErrHandler   Dim oSession 'As MAPI.Session   Set oSession = CreateObject("MAPI.Session")   oSession.Logon , , , , , True, "ServerName" & vbLf & "MailboxName"   Dim oFolder 'As MAPI.Folder   Set oFolder = oSession.GetDefaultFolder(1) 'CdoDefaultFolderInbox   Set oMessages = oFolder.Messages   Set oMessage = oMessages.GetFirst   MsgBox oMessage.Subject   Set oMessage = Nothing   MsgBox "1"   Set oMessages = Nothing   MsgBox "2"   Set oFolder = Nothing   MsgBox "3"   oSession.Logoff  '   MsgBox "4"      Set oSession = Nothing   MsgBox "Done"   Exit Sub    ErrHandler:   MsgBox Err.Number & "   -->   " & Err.Description   Set oMessage = Nothing   Set oMessages = Nothing   Set oFolder = Nothing   oSession.Logoff   Set oSession = NothingEnd Sub
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

Article ID: 821470 - Last Review: 12/08/2015 02:50:37 - Revision: 4.0

Microsoft Outlook 2002 Standard Edition, Microsoft Office Outlook 2003, Microsoft Office Outlook 2007, Microsoft Outlook 2010, Microsoft Outlook 2013

  • kbarchive kbnosurvey kberrmsg kbbug kbprb KB821470