I've fighting this all weekend. I'm trying to write a windows service that accesses microsoft outlook. But for some reason, my window service is crashing. I've tried the same code in a windows application and it works fine, my question is. Can I access Microsoft Outlook using a windows service or am I just waisting my time? The error message I receive is:"System.UnauthorizedAccessException: Access is denied.
at mikesEmailScanner.EmailScanner.OnStart(String[] args) in C:\mikesEmailScanner\EmailScanner.vb:line 73"
Here is my test code that I have been working with.
Dim myString As String
Dim myLocation As String
myLocation = "c:/test.txt"
Dim writer As StreamWriter
Try
Dim fname As String 'folder name
fname = "inbox"
Dim oOutLook As Outlook.Application
'create outlook application
oOutLook = New Outlook.Application'<-- line 73
'get mapi and login
Dim oNS As Outlook.NameSpace
oNS = oOutLook.GetNamespace("mapi")
oNS.Logon("outlook", Missing.Value, False, True)
Dim oInbox As Outlook.MAPIFolder
oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim oFolders As Outlook.Folders
oFolders = oInbox.Folders
Dim oFolder As Outlook.MAPIFolder
'oFolder = oFolders.Item(fname) ' folder name
Dim oMessage As Outlook.MailItem
Dim x As Object
x = oInbox.Items
For Each oMessage In x
writer = New StreamWriter(myLocation)
myString = oMessage.Body
writer.Write(myString)
writer.Flush()
writer.Close()
Next
Catch ex As Exception
writer = New StreamWriter(myLocation)
myString = ex.ToString
writer.Write(myString)
writer.Flush()
writer.Close()
End Try
any help would greatly be appriciated.
at mikesEmailScanner.EmailScanner.OnStart(String[] args) in C:\mikesEmailScanner\EmailScanner.vb:line 73"
Here is my test code that I have been working with.
Dim myString As String
Dim myLocation As String
myLocation = "c:/test.txt"
Dim writer As StreamWriter
Try
Dim fname As String 'folder name
fname = "inbox"
Dim oOutLook As Outlook.Application
'create outlook application
oOutLook = New Outlook.Application'<-- line 73
'get mapi and login
Dim oNS As Outlook.NameSpace
oNS = oOutLook.GetNamespace("mapi")
oNS.Logon("outlook", Missing.Value, False, True)
Dim oInbox As Outlook.MAPIFolder
oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim oFolders As Outlook.Folders
oFolders = oInbox.Folders
Dim oFolder As Outlook.MAPIFolder
'oFolder = oFolders.Item(fname) ' folder name
Dim oMessage As Outlook.MailItem
Dim x As Object
x = oInbox.Items
For Each oMessage In x
writer = New StreamWriter(myLocation)
myString = oMessage.Body
writer.Write(myString)
writer.Flush()
writer.Close()
Next
Catch ex As Exception
writer = New StreamWriter(myLocation)
myString = ex.ToString
writer.Write(myString)
writer.Flush()
writer.Close()
End Try
any help would greatly be appriciated.