Solved

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

Posted on 2004-03-25
6
573 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

773 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