Welcome to

The Conxsys Blog

Thoughts on Conxsys, Notes/Domino, LotusScript, & Technology

SnTT: Function to Get an Addressee’s Domain

October 9th, 2008 by Corey Davis

Show-n-Tell ThursdayOne of the little functions that I wrote some time ago yet has come in handy time and time again is the AddresseeDomain function. When given the recipient address of an e-mail, this function will pass back the recipient’s domain be it the Internet domain or Domino domain (which is useful if you are in an environment with multiple Domino domains).

Because this function is a mere 23 lines of code (including comments), I am going to walk you through it line-by-line.

1:  Function AddresseeDomain(addressee As String) As String
2:      Dim nam As New NotesName(addressee)

So far pretty simple. We are creating a function named AddresseeDomain which accepts a string variable and also returns a string variable.  The string variable that is passed to the function will contain the addressee for whom we want to parse out the domain.  The domain will be assigned to the string variable that will be returned by the function.

On line 2 we create a NotesName object and instantiate it with the addressee string variable that was passed into the function.

4:      ‘Strip angle brackets if they exist
5:      If Instr(1, addressee, “<”) > 0 Then
6:          Let addressee = nam.Addr821
7:      End If

We are not exactly sure what type of address we are being given. It could be a regular Notes name or an SMTP address. If it is an SMTP address it may be in the form of darth_vader@empire.com or it may be “Darth Vader” <darth_vader@empire.com>. If it is the former or a Notes name then we are good to move on, but if it is the latter we need to strip the extraneous stuff (friendly name and angle brackets) so that when we get to the real meat of the function we are always working with the same address format.

To do this, on line 5 we check to see if our addressee variable contains an angle bracket.  If not then we will just move on, but if it does contain the brackets then we will strip them.  We do this in line 6 by using the NotesName object and overwrite our addressee variable with the Addr821 representation of the name.

NotesName.Addr821 returns the text between angle brackets (“<” and “>”).  Therefore, when we are done with this section of code, no matter which address we were given — darth_vader@empire.com or “Darth Vader” <darth_vader@empire.com> — we end up with only darth_vader@empire.com.  If we were passed a Notes name then nothing has changed because angle brackets are illegal characters in Notes names.

9:    ‘Get the domain if there is one
10:      If Instr(addressee, “@”) > 0 Then
11:          If Not(nam.IsHierarchical) Then
12:              ‘Get the internet domain (everything after the @ symbol)
13:              AddresseeDomain = Lcase(Strrightback(addressee, “@”, 5, 1))
14:          Else
15:              ‘Get the users Domino domain
16:              Dim domains As Variant
17:              Let domains = Split(addressee, “@”)
18:              AddresseeDomain = domains(1)    ‘domains(0) will always be the user name and
19:                                      ‘domains(1) will always be the internet or home Domino domain
20:          End If
21:      Else
22:          AddresseeDomain = “”
23:      End If

And here is where all the work is really being done.  Let’s deconstruct this.

By the time we get to line 10, as mentioned above, we either have a Notes name — Darth Vader/Sith/DeathStar or Darth Vader/Sith/DeathStar@Empire if a Domino domain was tacked on — or we have an SMTP address — darth_vader@empire.com.  Thankfully Domino domains emulate Internet domains by separating them from the address with an @ symbol, so this makes our life very simple.  If the address does not contain an @ symbol — Darth Vader/Sith/DeathStar — then there is no domain name to return.  We skip down to line 22 and assign a empty string to the AddresseeDomain variable which will be passed back to the calling code.

If at line 10 we determine that we do have an @ symbol, however, then we need to do a little more work.  First, we need to determine if we have a Notes name or not.  You may be wondering why this is important.  After all, by now we know that we have an address of some sort with an @ symbol, so why not just get everything after the @ symbol and be done with it? It all has to do with multi-domain Domino environments and I will explain that in just a moment, but first let’s deal with SMTP addresses.

At line 11 we are checking the Notes name object to see if the name assigned to it is hierarchical or not. I am making an assumption here that, by now, there are unlikely to be any Domino domains that still use flat names. The exception to this being group names and mail-in databases. This function operates under the assumption that only names will be passed to it. In my code group names are never passed to this function as they are first expanded in a recursive manner meaning that all groups and nested groups are expanded leaving us with nothing but names or SMTP addresses.  In a future SnTT I will provide my group expansion code. As for mail-in databases, if these are a concern to you, you may want to add a call to the Domino directory to see if the name is a mail-in database and act on it accordingly.

With all that said, if the address is not hierarchical then we simply drop into line 13 and return everything after the @ symbol.  In our example above, darth_vader@empire.com would return empire.com.  So, for SMTP addresses we are outta here!

If, however, we had a hierarchical name as in Darth Vader/Sith/Empire@ DeathStar or, if the addressee is in another Domino domain, Emperor Palpatin/Senate/Empire@Coruscant@DeathStar we drop into lines 16 – 18.  This is where the power of Split comes into play especially for multi-domain environments.

Split takes in a string and delimiter and returns back an array of strings parsed by the delimiter. Let’s take Emperor Palpatin/Senate/Empire@Coruscant@DeathStar as an example. Because we want the Emperor’s domain — Coruscant — we cannot use the same code we used in line 13 otherwise we would get back Coruscant@DeathStar. But, with Split, we get back an array like this:

domains(0) = Emperor Palpatin/Senate/Empire
domains(1) = Coruscant
domains(2) = DeathStar

Line 18 will assign the AddresseeDomain return variable with the contents of the array entry for domains(1). This same functionality can be accomplished with more verboten code, but Split saves us a ton of time and effort.  The address Darth Vader/Sith/Empire@ DeathStar would return DeathStar.

You should be able to find may uses for this code or similar, but even if you don’t at least take away with you the knowledge of what Split can do and how it can help you quickly and easily parse strings of text into a neat and tidy array ready for processing.

Next week I will show you how I use this function to take an e-mail address and determine if the recipient is internal or external to the domain whether the recipient is addressed with their Notes name or SMTP address. This is especially useful in environments with either multiple Domino domains, multiple Internet domains, or both.


Tags: , , ,
Posted in Domino, SnTT, 4,992 views, 3 Comments
Digg This Submit to del.icio.us Submit to Technorati  

3 Responses

  1. Dan Soares Says:

    Very Nice Corey. You have a knack for teaching :)


  2. Corey Davis Says:

    Thanks, Dan!

  3. Patrick Kwinten Says:

    thank you, smart code!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

CC-GNU GPL Creative Commons License
The Conxsys Blog by Corey Davis is licensed under a Creative Commons Attribution 3.0 Unported License unless otherwise specified. Based on a work at conxsys.com/blog. All code on this blog is licensed under the CC-GNU GPL version 2.0 or later unless otherwise specified.

Copyright © 2006-2010 by Conxsys | Login | Powered by Wordpress | Template based on a design by Design4