Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Verifying address in automated outlook mail 1

Status
Not open for further replies.

johnnygeo

Programmer
Apr 23, 2003
125
US
I am using Access to create and send an Outlook mail item. I have a table containing email addresses to use for the recipients (in user@domain.com format). I need to make sure before I send the email that the addresses have been matched to the Exchange address list.

In other words, if I were to do this manually, I would create a new mail item in Outlook. I would type in jgeo@domain.com and hit the check address button. If the address is an employee, Outlook changes it to "Geo, Johnny" and underlines it. That validation is what I am trying to capture in my code.

Does Outlook validate as soon as the recipient is added? Do I have to save the message first, etc.?

Here's my code so far:
Code:
Sub sendEmail()
  Dim myOutlook As Object
  Dim myEmail As Object
  Set myOutlook = CreateObject("Outlook.Application")
  Set myEmail = myOutlook.CreateItem(olMailItem)
  Set myRecipient = myEmail.Recipients.Add("jgeo@domain.com")
  myEmail.Subject = "Subject Line"
  myEmail.Body = "Body Text" & Chr(10)
then depending on whether "jgeo@domain.com" was recognized or not, I would either save or send
Code:
  if ........ then 
    myEmail.Send
  else
    myEmail.Save
  end if
  Set myOutlook = Nothing
End Sub

Thanks,
Johnny Geo
 
It is better to create explicit objects, rather than just Object. So your mailitem is better to BE a MailItem. It also makes it faster to see the properties and methods. There is a Boolean property named Resolved.
Code:
Sub sendEmail()
Dim myOutlook As Outlook.Application
Dim myNamespace As Outlook.NameSpace
Dim myEmail As MailItem
Dim myRecipient As Recipient
  
  Set myOutlook = CreateObject("Outlook.Application")
  Set myNamespace = myOutlook.GetNamespace("MAPI")
  Set myEmail = myOutlook.CreateItem(olMailItem)
  Set myRecipient = myEmail.Recipients.Add("jgeo@domain.com")
  myEmail.Subject = "Subject Line"
  myEmail.Body = "Body Text" & Chr(10)
If myRecipient.Resolved Then
    msgbox "yes" [COLOR=red]' or whatever......[/color red]
Else
    msgbox "no" [COLOR=red]' or whatever......[/color red]
End If

  Set myRecipient = Nothing
  Set myEmail = Nothing
  Set myNamespace = Nothing
  Set myOutlook = Nothing
End Sub

Gerry
See my Paintings and Sculpture
 
Thanks for the tip on objects.

My problem is that I am creating the recipient using an explicit email address, rather than a user name. Outlook resolves these whether or not they match an address in the exchange address list.

I may have solved the problem by examining the AddressEntry.Type. If the email address matches an exchange address, this type is set to "EX", otherwise it is left as "SMTP". Or at least, that's what seems it seems to be doing. So now my code looks like:
Code:
allAddressesOK = True
Set myRecipient = myEmail.Recipients.Add("jgeo@domain.com")
myRecipient.Type = olTo
Set myAddressEntry = myRecipient.AddressEntry
If Not myAddressEntry.Type = "EX" Then allAddressesOK = False

...

If myEmail.Recipients.Count > 0 And allAddressesOK Then
  myEmail.Send
Else
  myEmail.Save
End If
Can anyone verify that the "EX" type means what I think it does; i.e., that the address has definitely been matched to an entry in the address list?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top