Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Service Scan Emails

Status
Not open for further replies.

mkohl

Programmer
Feb 22, 2005
82
US
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top