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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Exchange address???

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
Hi All,
I have an HTA app that allows the entry of a persons name or Employee ID number, looks that information up on the exchange server and if valid sends an email to that ID.

Apparently though what it does is translates the ID number if that was used into a name, or takes the name directly and checks to see if it exists on the exchange server.
Most times this is fine but here is the problem.

If we have two people with the same name, the script is only validating that the name exists, it is not returning a reference to the actual person I am trying to look up.

For instance. User 1 named John Doe, ID# AA12345 with a Display Name of Doe, John.
User 2 named John Doe, ID# BB54321 with a display name of Doe, John K.

Both people have different email addresses and display names but the first and last names are the same.
When the script validates is DOES find someone with the first/last name but when it tries to send it puts the last, first into the TO line and that will not resolve because there are multiple options for that name.

How can I get it to resolve in this case?
Or even just use the ID# to return the email address and insert that in the TO line since that will not have to resolve.

Help?

Here is my code.
Code:
<script language="vbscript">
Function SendEmail(name)
  Dim ToAddress
  Dim MessageSubject
  Dim MessageBody
  Dim MessageAttachment
  Dim ol, ns, newMail
  Dim thisdate
  thisdate = Date
  Dim Pos
  Dim SenderFullName
  Dim SenderLastName
  Dim SenderFirstName
  Dim RecipientFullName
  Dim RecipientLastName
  Dim RecipientFirstName

  ToAddress = name
  MessageSubject = "my subject"
  MessageBody = "Message body"
  MessageBody = MessageBody & chr(13) & chr(13) & "Thank you for your attention to this matter." & chr(13)

  On Error Resume Next

  ' Assume success
  fSuccess = True

  Set ol = GetObject("", "Outlook.application")
  ' If Outlook is NOT Open, then there will be an error. 
  ' Attempt to open Outlook
  If Err.Number > 0 Then
    Err.clear
    Set ol = CreateObject("Outlook.application")
    If Err.Number > 0 Then
      MsgBox "Could not create Outlook object", vbCritical
      fSuccess = False
      Exit Function
    End If
  End If

  ' If we've made it this far, we have an Outlook App Object 
  ' Now, set the NameSpace object to MAPI Namespace
  Set ns = ol.GetNamespace("MAPI")
  ns.logon "","",true,false
    
  If Err.Number > 0 Then
    MsgBox "Could not create NameSpace object", vbCritical
    fSuccess = False
    Exit Function
  End If

  If fsuccess = True Then
    ' Create the mail item and populate it with data
    Set newMail = ol.CreateItem(olMailItem)
    newMail.Subject = MessageSubject
    SenderFullName = ns.CurrentUser.Name
    Pos = InStr(1, SenderFullName, ",", vbTextCompare)
    SenderLastName = Trim(Left(SenderFullName, Pos - 1))
    SenderFirstName = Right(SenderFullName, Len(SenderFullName) - (Pos + 1))

    ' validate the recipient.
    Set myRecipient = ns.CreateRecipient(ToAddress)
    myRecipient.Resolve
    If Not myRecipient.Resolved Then
      MsgBox "Invalid recipient ID or name, please re-enter."
    Else
      resetid()
      newMail.Recipients.Add(myRecipient)
      RecipientFullName = myRecipient
      Pos = InStr(1, RecipientFullName, ",", vbTextCompare)
      RecipientLastName = Trim(Left(RecipientFullName, Pos - 1))
      RecipientFirstName = Right(RecipientFullName, Len(RecipientFullName) - (Pos + 1))
      Message = FormatDateTime(Now(), 1) & "<br>" & "<br>" & RecipientFirstName & " " & RecipientLastName & "<br>" & "<br>" & MessageBody & "<br>" & "<br>"
      Message = Message & SenderFirstName & " " & SenderLastName & "<br>" 
      newMail.HTMLBody = Message & "<br>"
      newMail.Display 'Un-comment this line and comment out the newMail.Send line if you want to review the message before sending.
'      newMail.Send  
      MsgBox "Your message has been sent."
    End If
  End If
  ' Close out our objects.
  Set ol = Nothing
  Set ns = Nothing
End Function
</script>

Paranoid? ME?? WHO WANTS TO KNOW????
 
Try something like:

Code:
  ' Resolve each Recipient's name.
  For Each myRecipient In newMail.Recipients
     myRecipient.Resolve
  Next

D
 
Wouldnt that just validate multiple names in the TO: field?
I am only sending to one person but there may be several people with the same name.
It seems that even passing in a unique ID number, when the line: Set myRecipient = ns.CreateRecipient(ToAddress)
executes, it translates the ID into a last name, first name set. Then when it does myRecipient.Resolve it is only checking that the name exists. I lose my unique reference to the person at this point though and if there are two John Doe's then when it tries to send the email it chokes not knowing which one is correct.

Is there any way to pull back the user ID rather than the name? The ID will always resolve the way it should as there will never be two identical IDs.

For now I replaced the line:
newMail.Recipients.Add(myRecipient)
with
newMail.Recipients.Add(ToAddress)
so the original information will be passed into the TO: line instead of the resolved name.
The problem with this is it will only work effectively if the ID# is used. If a name is entered it will now give an error but then the person has to go back and enter an ID# instead of a name.
This only happens when there are multiple people with the same name but that happens quite often here.



Paranoid? ME?? WHO WANTS TO KNOW????
 
Actually, I don't think it resolves at all. I just tried it out. Was just sort of one of those...
"hmmmm.... I wonder if this works!"

D
 
Oh, if you enter an invalid ID or name it reports back an error. It only moves forward if the name exists but it only seems to find a match of the first/last name fields, which can easily be duplicated. It really should match on the unique field.
Or, since I gave it a unique field to begin with, it should have resolved to the exact account.
I just do not know enough about accessing Exchange info to make it work exactly the way it should.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Oh, I had to modify the code a bit from the original VBA to make it work in the HTA application under VBScript tags.
It relies on a form field to send in the ID to look up.
That could be the reason it did not work when you tried it.

Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top