Not able to get items from Outlook Inbox from Windows Service using VB.NET

B

Brian

I have a Windows Service which is setup and running
properly, well, at least it execute my RunThrougInbox
method when it is supposed to be. The problem I am having
though is that when my RunThroughInbox method is
executing, it seems to stop when it gets to the "PROBLEM
ON THIS NEXT LINE:" line. I am writing each step of the
way to the event viewer window and my logs are
mysteriously stopping. Does anyone have any suggestions?
Thanks

Here is a portion of my code to help with any resolutions
or ideas.



Protected Overrides Sub OnStart(ByVal args() As String)
Dim cbTimer As New Timer(New TimerCallback(AddressOf
RunThroughInbox), Nothing, 1000, 60000)
End Sub

Sub RunThroughInbox(ByVal state As Object)


WriteToEventLog("In the RunThroughInbox Method")

Try
'----------------------------------------------
-----
' Instantiate Outlook and access the MAPI
Namespace
' (Messaging Application Programming Interface)
'----------------------------------------------
-----
Dim olApp As Outlook.Application = New
Outlook.Application
WriteToEventLog("passed olApp")

Dim olNS As Outlook.NameSpace =
olApp.GetNamespace("MAPI")
WriteToEventLog("passed olNS")
'----------------------------------------------
-----
' Once we have the MAPI namespace, we can now
access
' the Inbox, which we will grab the email
messages
' from and do further processing.
'----------------------------------------------
-----

*************************************
PROBLEM ON THIS NEXT LINE:
*************************************

Dim olInbox As Outlook.MAPIFolder =
olNS.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox)

WriteToEventLog("Passed olInbox: " &
olInbox.UnReadItemCount.ToString & " :: FolderPath is " &
olInbox.FolderPath.ToString)


'----------------------------------------------
-----
' Before performing any actions, we must
provide
' default credentials to access the MAPI.
'----------------------------------------------
-----
olNS.Logon("Brian Gaines", "password", False,
True)

WriteToEventLog("Logged in under Brian Gaines
profile.")

'----------------------------------------------
-----
' Get all items in the Inbox to be processed.
We
' also obtain the number of items in the inbox
folder
'----------------------------------------------
-----
Dim olItems As Outlook.Items = olInbox.Items
Dim boxCount As Integer = olItems.Count
Dim i As Integer
Dim cnt As Integer = 0

WriteToEventLog("Number of items in Brian
Gaines inbox are " & boxCount.ToString)

Try

'------------------------------------------
---------
' We step through our inbox backwards in
order to
' reference the correct index of the mail
item. If
' not, then we face the probability of
deleting
' incorrect emails and stepping out of
bounds of our
' items collection.
'------------------------------------------
---------
For i = boxCount To 1 Step -1

WriteToEventLog("Inbox Item #" &
i.ToString)

If TypeOf (olItems.Item(i)) Is
Outlook.ReportItem Then

cnt += 1

WriteToEventLog("Report Inbox Item
#" & cnt.ToString)

Dim emailSubject As String =
olItems.Item(i).Subject
Dim receiptBody As String =
olItems.Item(i).Body

Dim report As Outlook.ReportItem =
CType(olItems.Item(i), Outlook.ReportItem)
Dim returnedEmail As
Outlook.Attachment = report.Attachments.Item(1)

Dim path As String
If _path = "" Then
path = GetAppSetting
("TempInboxMsgAttchPath") & TEMP_ATTACHMENT_PREFIX
Else
path = _path
End If

returnedEmail.SaveAsFile(path & i
& TEMP_ATTACHMENT_EXT)

Dim err As String = ""
Dim emailBody = GetMessageBody
(path & i & TEMP_ATTACHMENT_EXT, err)

If err = "" Then

'------------------------------
--------------------------
' Regular expression returns
the invalid email from
' the invalid mail message and
the mail message guid
'------------------------------
--------------------------
Dim invalidEmail As String =
RegExValue(emailBody, GetAppSetting("EmailRegExChecker"))
Dim matchGuid As String =
RegExValue(emailBody, GetAppSetting("GuidRegExChecker"))
Dim writeGuidMessage As String
= ""
If matchGuid <> "" Then
writeGuidMessage = " for
GUID " & matchGuid & " "
End If
Console.Write(FormatDateTime
(Now) & ": Email to " & invalidEmail & writeGuidMessage
& " was processed.")

File.Delete(path & i &
TEMP_ATTACHMENT_EXT)

'------------------------------
--------------------------
' We remove this undeliverable
mail message from our
' Inbox folder and move on to
the next email item.
'------------------------------
--------------------------
olItems.Item(i).Delete()
Else
WriteToEventLog(err.ToString)
Exit Sub
End If
End If

Next i

WriteToEventLog("Done with Inbox")

Catch ex As Exception
WriteToEventLog(ex.ToString)
Finally

'------------------------------------------
--------------
' We now close our session with MAPI as
well as Outlook
'------------------------------------------
--------------
olNS.Logoff()
olApp.Quit()

WriteToEventLog("Logged and quit Outlook")

'------------------------------------------
--------------
' We de-reference any memory allocation to
these objects
'------------------------------------------
--------------
olApp = Nothing
olNS = Nothing
olItems = Nothing
olInbox = Nothing
WriteToEventLog(cnt & " Emails were
processed.")
End Try

Catch outEx As Exception
WriteToEventLog(outEx.ToString)
End Try

End Sub
 
K

Ken Slovak - [MVP - Outlook]

Outlook is not suitable for running in a Windows service. You will
have problems if you attempt to do so.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top