I have decided not to use EWS, instead I am using Microsoft.Office.Outlook.Interop.
I have the following code working well on my development PC:
log = New cLog()
Dim AppKeys As IDictionary = ConfigurationManager.GetSection("AppKeys")
Dim app As New Microsoft.Office.Interop.Outlook.Application()
Dim box As String
log.Write("Starting to retrieve mail.")
Try
box = AppKeys("mailbox")
tempLoc = Environment.CurrentDirectory & "\" & ConfigurationManager.AppSettings("TempLoc")
Dim ns As Microsoft.Office.Interop.Outlook.NameSpace = app.Session
log.Write("Namespace is: " & ns.CurrentProfileName)
Dim recipient As Recipient = ns.CreateRecipient(box)
log.Write("recipient is: " & recipient.Name)
recipient.Resolve()
log.Write("recipient resolved")
Dim inbox As Folder = ns.GetSharedDefaultFolder(recipient, OlDefaultFolders.olFolderInbox)
log.Write("Got inbox")
Dim subject As String
Dim mailCount As Int32 = 0
log.Write("There are " + inbox.Items.Count.ToString() + " Emails in " + box)
For Each mm As MailItem In inbox.Items
If Not subject Is Nothing Then
For Each attach As Attachment In mm.Attachments
attach.SaveAsFile(tempLoc & filename & attach.FileName.Substring(attach.FileName.Length - 4, 4))
Next
filename = filename.Substring(0, filename.Length - 2) & "_" & count + 1
mm.SaveAs(tempLoc & filename & ".msg", OlSaveAsType.olMSG)
End If
Next
Catch ex As System.Exception
log.Write("Excpetion getting mail: " & ex.Message)
End Try
app.Quit()
However, when I move the compiled code to a production PC (with Office 2010 installed, same as my development PC) I now get the following error on this line, "ns.GetSharedDefaultFolder":
The operation failed because of a registry or installation problem. Restart Outlook and try again. If the problem persists, reinstall.
I have a seperate application running on the same production PC that uses Microsoft.Office.Outlook.Interop to convert .MSG files to .PDF and that runs without issue.
The only difference that I can think of is that my development PC is Windows XP and the production PC is Windows 7.
Any thoughts would be greatly appreciated!
Thanks!