?
Solved

Using CDO to read, send and delete email

Posted on 2005-03-07
10
Medium Priority
?
3,956 Views
Last Modified: 2012-06-27
Hi,

I have to write code in VB6 (my last because we're moving to .NET) that checks for the existence of an .eml file in an Exchange public folder, if it's present it reads the contents of the email and then deletes it. There is no Outlook installed and the machine will be logged off (so it's all running as a service). I've never done this stuff before and a cursory glance on the web indicates that I might put a reference to 'Microsoft CDO for Exchange library 2000' and instantiate as follows:

Dim MyMsg as CDO.Message
MyMsg.DataSource.Open SomeStringContainingServerandFolderNameandEMLfilename

.....and related stuff.....my hunch is that this will probably only be about 20 lines of code or so, has been done before yet most of the code I found was getting pretty heavy duty and irrelevant to my cause.

I'm developing at home (broken my hand playing football!) and can't get to work for a month, where there is an MS Exchange server. I can't access the Exchange remotely and don't have Excahnge at home, so I'm kinda writing it blind and would like to get close first time around before sending it in to be tested, as such any simple tutorial code would be much appreciated.....hence 500 points

Ta in advance
Iain
0
Comment
Question by:iaing1000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 
LVL 12

Accepted Solution

by:
gbzhhu earned 1600 total points
ID: 13488112
I am not into CDO but I found this code that I was playing with sometime ago

    Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
    Const cdoSendUsingPort = 2
    Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
    Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
    Const cdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
    Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
    Const cdoBasic = 1
    Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
    Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
   
    Dim objConfig   As CDO.Configuration
    Dim objMessage  As CDO.Message
    Dim Fields      As ADODB.Fields
   
    ' Get a handle on the config object and it's fields
    Set objConfig = CreateObject("CDO.Configuration")
    Set Fields = objConfig.Fields
   
    ' Set config fields we care about
    With Fields
        .Item(cdoSendUsingMethod) = cdoSendUsingPort
        .Item(cdoSMTPServer) = "111.11.11.11"       'IP address
        .Item(cdoSMTPServerPort) = 25
        .Item(cdoSMTPConnectionTimeout) = 10
        .Item(cdoSMTPAuthenticate) = cdoBasic
        .Item(cdoSendUserName) = ""
        .Item(cdoSendPassword) = ""
   
        .Update
    End With
   
    Set objMessage = CreateObject("CDO.Message")
   
    Set objMessage.Configuration = objConfig
   
    With objMessage
        .To = "Ben <ben.buck@uk.beer.com>"
        .From = "Hassan <hassan.hussein@uk.wine.com>"
        .Subject = "CDO email test"
        .TextBody = "CDO Email Test Sent @ " & Now()
        .Send
    End With
   
    Set Fields = Nothing
    Set objMessage = Nothing
    Set objConfig = Nothing
0
 

Author Comment

by:iaing1000
ID: 13705132
Hi,

Sorry I've not been in touch regarding this question! I've decided I shan't be able to attempt to fix the problem until I can get on site, which is when my doctor gives me permission to drive again (wednesday 13th April) because of my recovering broken hand. In the meantime I've been doing other stuff.

Sorry for the delay, hopefully I can pick up this thread when I get on site again and finish it off quickly then

Hope that's OK
Iain

 
0
 

Author Comment

by:iaing1000
ID: 13789304
Hi,

Had a chance to get back to this one eventually. It's a bit of a minefield though. It complains that 'The Transport failed to connect to the server' -2147220973. It threw that one once it had got to the .send

Incidentally, the problem I'm trying to solve involves a remote client who would like to inform the s/ware I'm writing to perform a certain function. We looked at ftp, email etc and decided that they were better off sending an email, because the software may be busy at the time an ftp request comes in and it's also conventional to do this. The program runs as a service because the machine needs to be logged out most of the time, hence the attempt to use Exchange directly rather than Outlook which apparently needs a user (but would be much easier since there's a plethora of different ways via VB to do Outlook stuff). Any other ideas (even thought of using a web service!) that circumvent the need for using Exchange would be welcome, otherwise I'll pursue the CDO route

Thanks
Iain
0
Industry Leaders: 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!

 
LVL 12

Expert Comment

by:gbzhhu
ID: 13789462
To be honest with you the only 2 ways I ever used to send from a client are
CDO
Third party tool (IDSMail)  'bit better, you can see log file for activities but it means you will have to install all cliant PCs plus cost

Perhaps the error is a permission problem.  Exchange server not recognising the PC sending mail.  Again i never used Exchange, we use Lotus Notes and if i want to send email from my PC then my PC must be added to the SMTP server list of PC given access


0
 

Author Comment

by:iaing1000
ID: 13789534
Hi,

I've got it sending emails now! Amazing how quick it can be on site when you've got all the tools you need. The sending email account didn't have permission to send on the server and securing those rights fixed the problem. Now the only thing to do is to have the code recognise incoming emails sent to a specific account.....any ideas?

Thanks
Iain
0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 13789579
This maybe helpful.  It shows how to read email via ASP the code is the same for VB

http://www.cst-group.com/articles/technical/2000/aspemailhandling.asp
0
 
LVL 14

Assisted Solution

by:Shiju Sasidharan
Shiju Sasidharan earned 400 total points
ID: 13789812
0
 

Author Comment

by:iaing1000
ID: 13793562
Hi,

I tried the CodeProject stuff but it really didn't like it, kept finding page 404 if there was space character in the path (/Public Folders/etc) and declined access even with the correct username/password otherwise.

Any CDONTS stuff I'd like to do with CDO since I already use that and have that working for sending stuff and would like to be consistent. Although CDO doesn't provide a session object from which to iterate through a collection of emails I'm not especially after the inbox (see next paragraph....)  

What is slightly different in my case is that I'm not using the typical path http://<servername>/exchange/username/InBox/emailname.eml but instead http://<servername>/Public Folders/FolderName/emailname.eml   where FolderName is a 'mail enabled folder' (so the system admin guy keeps telling me) in other words the .eml file I must read the contents of and then delete resides not in a user's mail folder but in an area of the Public Folders. Dunno if that makes any difference but may be useful info. Should I use File://./<Servername>/etc instead?

Thanks again
Iain
0
 

Author Comment

by:iaing1000
ID: 13800886
Hi,

99% of everything I've found on the internet to do Exchange stuff relates to how to send an email, and I've done this successfully using CDO objects. It's the reading of emails that is proving fiendishly difficult (and their subsequent deletion), some stuff I've found suggests using outlook objects or MAPI objects with a mapi.logon(strProfile) call, which I don't believe can work as a service since it prompts and I'd like to be consistent and continue to use CDO objects.

So, I'll award the points to you guys because the CDO was great for sending emails when logged off, and the links are bookmarked and useful, but I shall re-post with a more specific title relating to Exchange Public Folders in the vain hope that someone knows!    

Ta
Iain
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question