Solved

Need to resolve names using CDOEX to send e-mails in a VB6 program

Posted on 2004-03-25
6
567 Views
Last Modified: 2013-12-25
Hi.

I'm amending a VB6 program I inherited that used to use CDO 1.21 to either print letters or to send attachments via e-mail.  Since we've moved to Windows 2000 I need to upgrade this to CODEX.  (It's form, not Web based).

The program works OK if I give it a full e-mail address (john.hyde@whatever.net, for example).  However, the old program included code to resolve the address if it was entered as a name in the users (Outlook 2000) address book.  So if the user had an address book entry for john hyde pointing to john.hyde@whatever.net, the user could enter an address of "john hyde", Jo Hyd" or anything else that Outlook would normally resolve.  This was accomplished with a .Recipients.Resolve" as follows:

    Dim objMessage              As MAPI.Message
        .
        .
        .
            Set objRecipient = objMessage.Recipients.Add(txtEmail.Text)
            ' If the txtCopiesTo text box is not empty then add this recipient too
            If txtCopiesTo.Text <> "" Then
                Set objRecipient = objMessage.Recipients.Add(txtCopiesTo.Text)
                objRecipient.Type = olCC
            End If
           
            ' On error - go to the CDO error handling which traps errors pertaining
            ' to email recipient resolution
            strLabel = "Send11"
            On Error GoTo CDO_Error
            ' Resolve the email addresses
            objMessage.Recipients.Resolve
            ' Reset back to Normal error handling
            On Error GoTo Error_Label
           
            ' Cycle through the recipients (currently just two) to extract the
            ' resolved email addresses and place them in the Correspondence form
            strLabel = "Send12"
            For Each objRecipient In objMessage.Recipients
                Select Case objRecipient.Type
                    Case olTo
                        Me.txtEmail.Text = objRecipient.AddressEntry
                    Case olCC
                        Me.txtCopiesTo.Text = objRecipient.AddressEntry
                    Case Else
                        ' Do nothing
                End Select
               
            Next objRecipient
           
            ' Ask the user if they want to send the email to the resolved recipients.
            ' If so, the correspondence is then saved to history, and sent via email.
            strLabel = "Send13"
            If vbYes = MessageConfirm(503, txtEmail.Text, txtCopiesTo.Text) Then
                Call SaveToHistory
                objMessage.Send
                MessageConfirm (504)
            End If

There is no ".Recipients" property available using CDOEX, so the question is - can I emulate this code using CDOEX for users who have Outlook loaded on their machines, or is CDOEX so removed from the Outlook interface that this is no longer possible/practical?  (If it is possible, would the same code work for people with Outlook Express?).

As a tack-on question, I'll also need to send Fax messages using the same program.  Can anyone point me to some code to do this using CDOEX?  (We have Zetafax set up on site - can we simply send a file to the Zetafax printer?)

A speedy answer would be appreciated - hence the 500 point allocation.

With thanks in anticipation,

John Hyde.
0
Comment
Question by:johnhyde
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:JohnK813
ID: 10678639
Do you use an Active Directory?  If so, you can use an iAddressee (information can be found on http://msdn.microsoft.com, search for "CDOEX iAddressee".)  I have some code I can show you if this is that case.

As for using CDOEX to fax, I don't think I can help you there.
0
 

Author Comment

by:johnhyde
ID: 10686239
Thanks for this.  Yes, we do use Active directory.  However, we're on Windows 2000 and Exchange Server 2000, and looking up "iAddressee" on MSDN only brings up references to Exchange Server 2003.  Is that going to rule this solution out?

If you think it doesn't, I'd appeciate seeing your code.

Thanks again,

John.
0
 
LVL 14

Accepted Solution

by:
JohnK813 earned 500 total points
ID: 10687019
We use Win2000 and Exchange 2000 also.  I think MSDN just put everything under Exchange 2003 to save space.

(Don't forget to reference CDOEX - I always forget to do this at first.)

Here's your setup:

Dim Info 'Active Directory Info
Dim iAddr
Dim strEmail 'email or name entered
Dim strGoodEmail 'valid email to use
Dim oMessage as CDO.Message
Set Info = CreateObject("ADSystemInfo")
Set iAddr = CreateObject("CDO.Addressee")

The five basic iAddress commands you will need are:
iAddr.EmailAddress - holds email address
iAddr.DisplayName - holds corresponding name
iAddr.CheckName - retrieves email/name
iAddr.ResolvedStatus - returns CheckName results
iAddr.AmbiguousNames - collection of matches from CheckName, if more than one returned.  This will come in to play, for instance, when you type in "John H" and there are multiple Johns with last names that start with H.

DisplayName doesn't require an entire name, so you can use "John H" or "J Hyde".
CheckName basically "fills in the gaps."  If you give it an EmailAddress, it'll give you the DisplayName, and vice-versa.

So, what you can do is first check whether the string entered was an email address or just a name:
If instr(strEmail,"@") Then
  iAddr.EmailAddress = strEmail
Else
  iAddr.DisplayName = strEmail
EndIf
iAddr.CheckName("LDAP://" & Info.DomainDNSName) 'this checks the Active Directory
Select Case iAddr.ResolvedStatus
  Case 1 'valid email found
    strGoodEmail = iAddr.EmailAddress
  Case 2 'multiple matches found
    'what I normally do here is set up a For...Each
    'statement to loop through
    'iAddr.AmbigousNames, then display them in a
    'MsgBox, Exit Sub, and say "Try Again".
    'There's really not much else you can do.
  Case 3 'no matches
    'tell user to try again
End Select

Now, it's time to send the email

With oMessage
  .To = strGoodEmail
  .From = [your email]
  .TextBody = "body of email"
  .Subject = "Subject"
  'I use SMTP port 25 to send emails through
  'Exchange.  MSDN explains other methods.
  .Configuration.Fields(cdoSendUsingMethod) = cdoSendUsingPort
  .Configuration.Fields(cdoSMTPServer) = "ExchangeServerNameHere"
  .Configuration.Fields(cdoSMTPServerPort) = 25
  .Configuration.Fields.Update
  .AddAttachment("c:\path\file.txt")
  .Send
End With

Hope this helps.  I'll be happy to answer any questions you have.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:johnhyde
ID: 10688092
Many thanks - I'll have a play with it and get back to you - hopefully Monday at the latest.

John.
0
 

Author Comment

by:johnhyde
ID: 10688520
Wow!  Didn't expect that to be so painless.  I virtually dropped the code in and it works fine.  Great code, clear as a bell.  

Many thanks for your help - it's well worth the points, and it's save me days of hassle.

John.
0
 
LVL 14

Expert Comment

by:JohnK813
ID: 10688872
Glad to see everything worked smoothly.

I guess we're both lucky that I just happened to be working on something similar.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now