none
HRESULT Error when retrieving Global Address List using from a Windows service application

    Question

  • I've created a windows service application which basically retreives the names and  smtp address from the Exchange Gloabal Addess List into a  database table using the outlook MAPI NameSpace. This works fine when I interactively run the executable, but it always fail with the error "mscorlib HRESULT: 0x86240200" when I run the executible from Windows Task Scheduler. The windows task was created to run with my login profile.

    By the way, I am using Outlook 2007 with the Outlook 3.0 runtime library.

    My Code:

            Dim strSQL As String = ""

            Dim Cn As New SqlClient.SqlConnection(strConnection)

            Cn.Open()

            Dim cmd As New SqlClient.SqlCommand

            cmd.Connection = Cn

           

            Dim currentDomain As AppDomain = AppDomain.CurrentDomain

            AddHandler currentDomain.UnhandledException, AddressOf OnUnhandledException

            'AddHandler Sytem.windows.forms.Application, AddressOf OnGuiUnhandedException

            Dim olA As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application

            Dim Ns As Microsoft.Office.Interop.Outlook.NameSpace = olA.GetNamespace("MAPI")

            Dim AddressEntry As Microsoft.Office.Interop.Outlook.AddressEntry = Nothing

            Dim objExuser As Microsoft.Office.Interop.Outlook.ExchangeUser = Nothing

            Dim olAddressList As Microsoft.Office.Interop.Outlook.AddressList = Nothing

            Dim Address As String = ""

            Dim strName As String = ""

            Dim AddressArray() As String = Nothing

            Try

                olAddressList = Ns.GetGlobalAddressList  '***Seems to be failing here from Task Manager**

          

                Dim m_dt As New DataTable

                m_dt.Columns.Add("ExUser_Name", GetType(String))

                m_dt.Columns.Add("Smtp_Address", GetType(String))

                If Not olAddressList Is Nothing Then

                    For Each Entry In olAddressList.AddressEntries

                        Dim objExGroup As Microsoft.Office.Interop.Outlook.ExchangeDistributionList = Nothing

                        objExuser = Nothing

                        AddressEntry = Entry

                        strName = AddressEntry.Name

                        objExuser = AddressEntry.GetExchangeUser

                        If objExuser Is Nothing Then objExGroup = AddressEntry.GetExchangeDistributionList

                        If Not objExuser Is Nothing Then

                            Address = objExuser.PrimarySmtpAddress.ToString

                            'Console.WriteLine("{0} -{1}", strName, LCase(Address))

                            m_dt.Rows.Add(strName, LCase(Address))

                        ElseIf Not objExGroup Is Nothing Then

                            Address = objExGroup.PrimarySmtpAddress.ToString

                            'Console.WriteLine("{0} -{1}", strName, LCase(Address))

                            m_dt.Rows.Add(strName, LCase(Address))

                        ElseIf AddressEntry.Address.Contains("/") Then

                            AddressArray = Split(AddressEntry.Address, "/")

                            Address = AddressArray(4).ToString

                            Address = LCase(Mid(Address, InStr(Address, "=") + 1, Len(Address)))

                            If Not UCase(Right(Address, 7)) = "CHP.ORG" Then Address = Address & "@chp.org"

                            m_dt.Rows.Add(strName, Trim(Address))

                            'Console.WriteLine("{0} -{1}", strName, LCase(Address))

                        ElseIf UCase(Right(AddressEntry.Address, 7)) = "CHP.ORG" Then

                            Address = Trim(AddressEntry.Address)

                            m_dt.Rows.Add(strName, LCase(Address))

                        End If

                    Next

                    If m_dt.Rows.Count > 1 Then

                        'Clear the DB table if the table object has data

                        cmd.CommandText = "Delete From tblCHPEmail"

                        cmd.ExecuteNonQuery()

                        With m_dt

                            For i As Integer = 0 To .Rows.Count - 1

                                strName = .Rows(i).Item("ExUser_Name").ToString

                                Address = .Rows(i).Item("Smtp_Address").ToString

                                strSQL = "Exec dbo.usp_ins_tblCHPEmail @Display_Name = '" & Trim(strName) & "'"

                                strSQL = strSQL & ", @SMTP = '" & Trim(LCase(Address)) & "'"

                                cmd.CommandText = strSQL

                                cmd.ExecuteNonQuery()

                            Next

                        End With

                    End If

                    olA = Nothing

                    Ns = Nothing

                    olAddressList = Nothing

                    objExuser = Nothing

                    AddressEntry = Nothing

                    Cn.Close()

                    cmd.Dispose()

                End If

            Catch ex As Exception

                EventLog.WriteEntry(My.Application.Info.AssemblyName & " " & ex.Source, ex.Message & vbCrLf & ex.StackTrace.ToString)

                'Console.WriteLine("{0} -{1}", ex.Source, ex.Message)

                Err.Clear()

                olA = Nothing

                Ns = Nothing

                olAddressList = Nothing

                objExuser = Nothing

                AddressEntry = Nothing

                Cn.Close()

                cmd.Dispose()

            End Try

        End Sub


    VB.Net Dev

    Wednesday, December 12, 2012 3:56 PM

Answers

  • No matter what, you cannot run Outlook object model code from a service, scheduler, or background thread. It's not supported and won't run correctly if it runs at all.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "FredFerguson" <=?utf-8?B?RnJlZEZlcmd1c29u?=> wrote in message news:d6a700b4-3629-495d-bafc-4af2ed99546b...

    The task was created with the current user that is the same as the mailbox profile.

    I have attempted to use NameSpace.Logon() Method. before the GetGloabalAddressList method. but that didn't work either.


    VB.Net Dev


    Ken Slovak MVP - Outlook
    • Marked as answer by FredFerguson Wednesday, December 12, 2012 7:01 PM
    Wednesday, December 12, 2012 6:50 PM

All replies

  • Scheduler is a service, but no Office app (Outlook inckluded) can run from a service.

    If the current user the same as the domain user who owns the primary mailbox in the profile?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Wednesday, December 12, 2012 4:36 PM
  • The task was created with the current user that is the same as the mailbox profile.

    I have attempted to use NameSpace.Logon() Method. before the GetGloabalAddressList method. but that didn't work either.


    VB.Net Dev

    Wednesday, December 12, 2012 6:23 PM
  • No matter what, you cannot run Outlook object model code from a service, scheduler, or background thread. It's not supported and won't run correctly if it runs at all.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "FredFerguson" <=?utf-8?B?RnJlZEZlcmd1c29u?=> wrote in message news:d6a700b4-3629-495d-bafc-4af2ed99546b...

    The task was created with the current user that is the same as the mailbox profile.

    I have attempted to use NameSpace.Logon() Method. before the GetGloabalAddressList method. but that didn't work either.


    VB.Net Dev


    Ken Slovak MVP - Outlook
    • Marked as answer by FredFerguson Wednesday, December 12, 2012 7:01 PM
    Wednesday, December 12, 2012 6:50 PM
  • You can of course switch to Extended MAPI (C++ or Delphi) or Redemption (any language) - they can run in a service.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Wednesday, December 12, 2012 7:55 PM
  • I have redemption 5.3 I am attempting that at the moment. Ill let you know how it works out.

    VB.Net Dev

    Wednesday, December 12, 2012 8:03 PM
  • Dmitry,

    Redemtion RDO worked. Thank you for your help and input.


    VB.Net Dev

    Thursday, December 13, 2012 4:48 PM