Windows Service and MailItems

  • Thread starter Thread starter Dima Protchenko
  • Start date Start date
D

Dima Protchenko

Hi,
I am building a windows service, which monitors a mailbox and parses email messages into the db. When I wrote the SAME code in windows forms application, everything worked fine, but I am having a problem in the Windows Service project and I can't figure it out.

For i As Integer = objFolder.Items.Count To 1 Step -1
If TypeOf (objFolder.Items.Item(i)) Is Outlook.MailItem Then
'Me.EventLog.WriteEntry("TechLocatorSRVC", "Sender :" & objMailMessage.SenderName.ToString & ", Message: " & objMailMessage.Body.ToString)
objMailMessage = objFolder.Items.Item(i)
'UpdateLocation(objMailMessage.SenderName.ToString, objMailMessage.Body.ToString)
objMailMessage.UnRead = False
objMailMessage.Move(objArchiveFolder)
End If
Next

Except for the two commented out lines, everything is happening. But as soon as I try to access the MailItem PROPERTIES (like SenderName or Body, commented lines) it seems to abort any kind of execution and doesn't even keep looping. I am doing TRY-CATCH, but it doesn't post any exceptions. I am doing all this on the box with windows XP and office so my project reference is Microsoft Outlook 10.0 Object Library.

Any help is greatly appreciated.
 
Have you tried setting the system like SQLServer, have the service run under
the same login as a mail profile that has been configured. ie log on to the
machine and configure a mail account with the service account user.

GW
Hi,
I am building a windows service, which monitors a mailbox and parses email
messages into the db. When I wrote the SAME code in windows forms
application, everything worked fine, but I am having a problem in the
Windows Service project and I can't figure it out.

For i As Integer = objFolder.Items.Count To 1 Step -1
If TypeOf (objFolder.Items.Item(i)) Is Outlook.MailItem Then
'Me.EventLog.WriteEntry("TechLocatorSRVC", "Sender :" &
objMailMessage.SenderName.ToString & ", Message: " &
objMailMessage.Body.ToString)
objMailMessage = objFolder.Items.Item(i)
'UpdateLocation(objMailMessage.SenderName.ToString,
objMailMessage.Body.ToString)
objMailMessage.UnRead = False
objMailMessage.Move(objArchiveFolder)
End If
Next

Except for the two commented out lines, everything is happening. But as soon
as I try to access the MailItem PROPERTIES (like SenderName or Body,
commented lines) it seems to abort any kind of execution and doesn't even
keep looping. I am doing TRY-CATCH, but it doesn't post any exceptions. I am
doing all this on the box with windows XP and office so my project reference
is Microsoft Outlook 10.0 Object Library.

Any help is greatly appreciated.
 
Dima,
As Glenn stated, you need to run your service under an account that has a
MAPI profile.

Also, as soon as you attempt to access properties such as SenderName & Body,
Outlook XP & 2003 will display a security prompt asking you if you want to
allow a potential VIRUS from accessing this information.

Seeing as a Service is not normally able to interact with the desktop, the
access is denied and you get the exception...

If you do not have it, the following site provides a plethora of information
on using Outlook with .NET.

http://www.microeye.com/resources/res_outlookvsnet.htm

Hope this helps
Jay

Hi,
I am building a windows service, which monitors a mailbox and parses email
messages into the db. When I wrote the SAME code in windows forms
application, everything worked fine, but I am having a problem in the
Windows Service project and I can't figure it out.

For i As Integer = objFolder.Items.Count To 1 Step -1
If TypeOf (objFolder.Items.Item(i)) Is Outlook.MailItem Then
'Me.EventLog.WriteEntry("TechLocatorSRVC", "Sender :" &
objMailMessage.SenderName.ToString & ", Message: " &
objMailMessage.Body.ToString)
objMailMessage = objFolder.Items.Item(i)
'UpdateLocation(objMailMessage.SenderName.ToString,
objMailMessage.Body.ToString)
objMailMessage.UnRead = False
objMailMessage.Move(objArchiveFolder)
End If
Next

Except for the two commented out lines, everything is happening. But as soon
as I try to access the MailItem PROPERTIES (like SenderName or Body,
commented lines) it seems to abort any kind of execution and doesn't even
keep looping. I am doing TRY-CATCH, but it doesn't post any exceptions. I am
doing all this on the box with windows XP and office so my project reference
is Microsoft Outlook 10.0 Object Library.

Any help is greatly appreciated.
 
Back
Top