[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2303
  • Last Modified:

Reading email hen deleting it from Exchange/Public Folders/MyFolder

Hi,

I'm writing a program to automatically read then delete .eml files in an Exchange public folder, lets call it Public Folders/MyFolder. If a .eml file is present it reads the contents of the email and then deletes it, then checks if there's another one etc. There is no Outlook installed and the machine will be logged off (so it's all running as a service), hence mapi.logon may not work as well. I've got the sending of emails working well by using CDO objects and would like to be consistent and so use these for the reading/deletion too. 99% of code samples I've found on the web relate to sending emails and not reading them.

I'm using VB6 but frankly it could be any language, it's the manipulation of the CDO objects (or whatever stuff I end up using that's important). I'm tearing my hair out on this one.

Ta
Iain
0
iaing1000
Asked:
iaing1000
  • 8
  • 8
1 Solution
 
ADSaundersCommented:
Hi,
I have had no experience of reading messages from public folders, but have had a fair bit on reading from an inbox. You will need to log on to an account that has write (delete) access to the folder you wish to manipulate. I provide a bit of code that should work (it's an extract from a service that watches a mailbox for certain incoming messages). This function (CheckAutomail) checks the subject for the colon (:) character included in most auto reply messages (Out of Office Autoreply: <subject>, RE:<Subject>, FWD:<Subject> etc. Etc.), or empty subjects, and deletes them. If you need to manipulate the body of the message, it's in ObjMessage.Text.

.. Hope it helps
Regards .. Alan

Dim oSession As MAPI.Session
Dim oInbox As Object
Dim oMessages As MAPI.Messages
Dim oMessage As MAPI.Message

Private Sub CheckMail()
    oSession.Logon "", Encode1.DeCrypt(MPassword), False, True, 0, False, MServer & Chr(10) & MUser
    LogMessage 3, "Logged on OK."
    Set oInbox = oSession.Inbox
    Set oMessages = oInbox.Messages
    lIsFirst = True
    Set oMessage = oMessages.GetFirst
    While Not oMessage Is Nothing ' loop through all messages
        If Not CheckAutoMail(oMessage) Then
            '...... Other Code
        End If
        Set oMessage = oMessages.GetNext
    Wend
    Set oMessage = Nothing
    Set oMessages = Nothing
    Set oInbox = Notjing
    oSession.Logoff
    set oSession = Nothing
End Sub

Private Function CheckAutoMail(ObjMessage As MAPI.Message) As Boolean
    If Trim(oMessage.Subject) = "" _
    Or InStr(oMessage.Subject, ":") <> 0 Then
        If InStr(oMessage.Subject, ":") <> 0 Then
            LogMessage 2, "Auto Mail style Subject Line, Deleting: " & oMessage.Subject
        Else
            LogMessage 2, "Message has empty subject, Deleting"
        End If
        ObjMessage.Delete
        CheckAutoMail = True
    Else
        CheckAutoMail = False
    End If
End Function
0
 
ADSaundersCommented:
Sorry,
>   oSession.Logon "", Encode1.DeCrypt(MPassword), False, True, 0, False, MServer & Chr(10) & MUser
Should be:
   oSession.Logon "", <Password>, False, True, 0, False, <Server> & Chr(10) & <Account>
Where <Account> is the Exchange Server account with the required access
           <Password> is the account password, and
           <Server> is the name of the Exchange Server.

The logmessage sub is simply a logging routine:

Private Sub LogMessage(lLevel As Long, smsg As String)
Dim f As Integer, p As Process
    If lLevel > LogDetail Then Exit Sub ' We're not logging at this importance level
    f = FreeFile
    Open LogPath For Append As #f
    Print #f, Format(Now); " - ("; CStr(lLevel) & ") "; smsg
    Close #f
End Sub

.. Alan
0
 
iaing1000Author Commented:
Hi Alan

Ta for that. I shan't get a chance to use it until tomorrow. btw, can <server> be the IP addressm, or if it is the name should it be written "\\ServerName" just so I know in advance.

Cheers
Iain
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ADSaundersCommented:
Hi,
No, Server is just ServerName without any decoration. I don't think you can use an IP address.

.. Alan
0
 
iaing1000Author Commented:
Hi,

I'm using the MS MAPI controls 6.0 as a component but it only recognises MAPISession and MAPIMessages as objects, not MAPIMessage in the singular. Note there are also no full stops (periods in US) between objects, e.g. MAPI.Session, so I suspect I'm using the wrong reference or component. Which do you use?

Thanks
Iain
0
 
ADSaundersCommented:
Hi,
Don't use the MAPI controls! They are too limited. Instead, add a reference to the Microsoft CDO 1.21 library. If you don't have this installed, it is on the MSOffice installation CD, and can be installed from there.

.. Alan
0
 
iaing1000Author Commented:
Hi,

I've made some progress. I installed MS Office 2003 to get the CDO.dll file and so allow me to use the MAPI objects from within VB using CDO 1.21 as a reference. The following code...

Dim oSession as MAPI.Session
Set oSession= New MAPI.Session
oSession.Logon "", "MyPass", False, True, 0, False, "ServerName" & chr(10) & "MyUser"

yields the following error:
-2147221231: The information store could not be opened. [MAPI 1.0 - [MAPI_E_LOGON_FAILED(80040111)]]

I'm not sure if this relates to passwords etc. so I'll investigate further but I'm wondering if it sounds like I'm making a big mistake.

Cheers
Iain
0
 
iaing1000Author Commented:
And..

-2147221231:  [Collaboration Data Objects - [MAPI_E_LOGON_FAILED(80040111)]]

This when I tried

oSession.Logon

with no parameters, so that I would hopefully get a prompt

Iain
0
 
iaing1000Author Commented:
Latest update....

It's not the usernames/passwords that are the problem, I've double checked that stuff again. But I'm wondering whether the client machine on which I'm running my compiled code also needs to have the CDO.dll registered in <%Win%>\System32. I tried installing the MS Office CD on that but it nevewr seems to put CDO.dll on the m/c, which it did on mine....bizarre. I copied it to the <%Win%>\system32 folder, tried to register with regsvr32 and it complained about the load library for it. Mind you would it come up with the errors it has if that were the problem?

Thanks again
Iain
0
 
ADSaundersCommented:
Hi,
Not seen this problem, All machines that my code runs on have office installed, and the CDO.dll
Don't know what to suggest...
Hang on, Are you using your user name, or the Exchange account name? That might make a difference. For automated mail handling on our site, the appropriate accounts are the same as the user name, but it could be different on your system.

.. Alan

p.s. Going home now,  will pick this up again tomorrow.
0
 
ADSaundersCommented:
Hi,
The Information store could not be opened error is detailed here http://support.microsoft.com/default.aspx?scid=kb;en-us;181739

oSession.Logon "", "", True, True, 0, False, "" should give you a dialog (the first boolean parameter defines this.

For full logon docs see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdo/html/_olemsg_logon_method_session.asp

.. Alan
0
 
iaing1000Author Commented:
Hi Alan,

I've not had a chance to look at it today, although yesterday I did try playing with the parameters and tried to get a dialog box. Unfortunately it came up with -2147221231:  [Collaboration Data Objects - [MAPI_E_LOGON_FAILED(80040111)]], which is the same internal MAPI error code 80040111 as the Information Store Not Found error but with a different start to it. I'd looked at those links too and explored everything in the KB, all the users etc are OK and visible.

I don't know what to make of it, although I'm beginning to think the target machine's copy of CDO.dll is dodgy. When I installed the Office CD on that m/c (to get CDO.dll registered onto it) it didn't put it there for some reason (it worked on the build m/c) so I just copied my own version onto it and using regsvr32 it moaned about the Load Library. So that's my latest approach. Will try again tomorrow.

Ta
Iain
0
 
iaing1000Author Commented:
Hi,

Sorry about the enormous delay....I haven't been back on this site cos the project suddenly ground to a halt.....anyway it's started again now. Where was I? Oh, yes, I put a new CDO.dll installed from elsewhere and the oSession.logon with or without prompts went in fine, which is the battle won really. All I want to do is read the oMessage body and subject, but oMessage (of type MAPI.Message and non null) assigned via the appropriate inbox.oMessages etc does not expose a .Subject method. Maybe I'll try oMessage.Item(x) where x might give me what I want.

Any ideas? Nearly there though I think....

Ta
Iain
0
 
iaing1000Author Commented:
Hi Alan,

Got it sorted. The last bit was a typo! I'd used oMessages not oMessage when reading their content.

Thanks for your help, that was really getting to me that problem

Iain
0
 
ADSaundersCommented:
Hi,
>>oMessage (of type MAPI.Message and non null) assigned via the appropriate inbox.oMessages etc does not expose a .Subject method
???!!
Are you sure you're referncing the correct DLL?
CDO 1.2 does provide a subject property of a MAPI.Message.

.. Alan
0
 
ADSaundersCommented:
... Timing ...
But thanks

.. Alan
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now