Outlook 2007 Programmierung: Auslesen der Email Adresse aus einem Email

Letzte Woche haben wir ein Hands-on Lab zu Office Business Applications - also zur Programmierung von Outlook, Word und Excel - durchgeführt.

Details und Download: https://blogs.msdn.com/swiss_dpe_team/archive/2008/01/17/office-business-applications-hands-on-lab.aspx

In Lab Nummer 2 geht es darum, wie ERP Daten in Outlook verwendet werden können. Dabei wird das Standard Contact Fenster von Outlook wie folgt erweitert, um die Bestellungen des aktuellen Kontaktes anzuzeigen:

image

Im Lab wird das CompanyName Property (also Northwind Toms Spezialitäten) des Kontaktes verwendet, um die entsprechende Firma in der Datenbank zu finden.

Ein Teilnehmer hat mich gefragt, wie aufgrund einer Email Adresse eines Emails in Outlook die Verknüpfung zum ERP system gemacht werden könnte. Ähnlich wie die custom Form Region auf dem Outlook Screenshot, könnte nämlich auch eine custom Form Region direkt auf dem Email von Interesse sein:

image

Was auf den ersten Blick trivial klingt, stellt sich als etwas kompliziert heraus:

Die Grundidee ist die folgende: Aufgrund des Absenders des Emails wird der Kontakt im Outlook Adressbuch gesucht. Aus dem Kontakt wird die Firma ausgelesen und aufgrund dieser Firma wird auf die DB zugegriffen. Der zweite Schritt ist selbsterklärend, jedoch nicht der erste:

Angenommen, wir haben das Outlook.MailItem Objekt des aktuellen Emails zur Verfügung, stossen wir sehr schnell auf das Property SenderEmailAddress. Doch ergibt sich die folgende Herausforderung: Dieses Property wird nur mit der Email Adresse gefüllt, sofern es sich um ein externes Email handelt. Ist der Absender dem Exchange Server bekannt (hat er also einen AD Account in der Firmendomäne), ist dieses Property für unsere Zwecke unbrauchbar. In diesem Falle muss die Email Adresse auf ganz andere Art und Weise ergattert werden:

Outlook.Recipient recipient;
Outlook.ExchangeUser exchangeUser;
String address;

recipient = Globals.ThisAddIn.Application.GetNamespace("MAPI").
    CreateRecipient(mailItem.SenderName);

exchangeUser = recipient.AddressEntry.GetExchangeUser();
address = exchangeUser.PrimarySmtpAddress;

Die ganze Methode, welche die Unterscheidung externes/internes Email sowie den Zugriff auf die Outlook Kontakte enthält, könnte dann etwa wie folgt aussehen:

Bem: Die Methode erwartet ein Outlook.MailItem und gibt den entsprechenden CompanyName des Kontaktes zurück, falls vorhanden.

        private String FindContactcompanyByEmail(Outlook.MailItem mailItem)
{
Outlook.NameSpace outlookNameSpace = Globals.ThisAddIn.Application.
GetNamespace("MAPI");
Outlook.MAPIFolder contactsFolder =
outlookNameSpace.GetDefaultFolder(
Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items contactItems = contactsFolder.Items;
Outlook.ContactItem contact;
String returnValue="";

            //email related
Outlook.Recipient recipient;
Outlook.ExchangeUser exchangeUser;
String address;

            try
{
if (mailItem.SenderEmailType.ToLower() == "ex") //sender has AD account
{
recipient = Globals.ThisAddIn.Application.GetNamespace("MAPI").
CreateRecipient(mailItem.SenderName);

                    //recipient.AddressEntry might not be available
try
{
exchangeUser = recipient.AddressEntry.GetExchangeUser();
address = exchangeUser.PrimarySmtpAddress;
address = "''" + address + "''";
}
catch (System.Runtime.InteropServices.COMException ex)
{
returnValue = "The contact information was not found (by Email).";
return returnValue;
}
}
else //sender is not from our exchange server / no AD account
{
address = mailItem.SenderEmailAddress;
}

                //find contact based on email address
contact = (Outlook.ContactItem)contactItems.Find(
String.Format("[Email1Address]='{0}'", address));

                if (contact != null)
{
returnValue = contact.CompanyName;
}
//check whether the email address in stored in a contacts Email2Address attribute
//note: there's even an Email3Address property
else
{
contact = (Outlook.ContactItem)contactItems.Find(
String.Format("[Email2Address]='{0}'", address));
if (contact == null)
returnValue = "The contact information was not found (by Email).";
else
returnValue = contact.CompanyName;
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
return returnValue;
}

Ich hoffe, dass Sie dieses Beispiel im Rahmen Ihrer Outlook Office Business Application, weiterbringt!