Share via


E-Mail Messages

Outlook Developer Reference
E-Mail Messages

You can link a new E-Mail Message object or an existing message to an Account, Business Contact, or Business Project object. The following C# and Visual Basic for Applications (VBA) examples show how to add a new e-mail message linked to an Account.

  private void CreateEmailMessage()
        {
        Outlook.ApplicationClass _app = new Outlook.ApplicationClass();
        Outlook.Application olApp = (Outlook.Application)_app;
        Outlook.NameSpace olNameSpace = _app.GetNamespace("MAPI");
        Outlook.Folders folders = olNameSpace.Session.Folders;
        Outlook.Folder bcmRootFolder = (Outlook.Folder)folders["Business Contact Manager"];
        Outlook.Folder accountsFolder = (Outlook.Folder)bcmRootFolder.Folders["Accounts"];

        string strQuery = "[FileAs] = 'Wide World Importers'";

        Outlook.ContactItem accountItem = (Outlook.ContactItem)accountsFolder.Items.Find(strQuery);
        if (accountItem != null)
        {
            Outlook.MailItem mailItem = (Outlook.MailItem)olApp.CreateItem(Outlook.OlItemType.olMailItem);
            mailItem.Subject = "A Test Mail";
            mailItem.Body = "This is the Body of an e-mail message";
            mailItem.Recipients.Add(accountItem.Email1Address);
            mailItem.Send();
        }

        else
        {
            Console.WriteLine("Please create an account with Full name: Wide World Importers, give it a valid e-mail address, and re-run the script");
        }

    }
  Sub CreateMailMsgWithAccount() 

MsgBox ("E-mail Auto Linking can be set to link this e-mail directly to the account.") Dim olApp As Outlook.Application Dim objNS As Outlook.NameSpace Dim olFolders As Outlook.Folders Dim bcmRootFolder As Outlook.MAPIFolder Dim bcmAccountsFldr As Outlook.MAPIFolder Dim existAcct As Outlook.ContactItem Dim objMail As Outlook.MailItem

Set olApp = CreateObject("Outlook.Application") Set objNS = olApp.GetNamespace("MAPI") Set olFolders = objNS.Session.Folders Set bcmRootFolder = olFolders("Business Contact Manager") Set bcmAccountsFldr = bcmRootFolder.Folders("Accounts")

Set existAcct = bcmAccountsFldr.Items.Find("[FileAs] = 'Wide World Importers'")

If Not TypeName(existAcct) = "Nothing" Then

  Set objMail = olApp.CreateItem(olMailItem)
  objMail.Subject = "Mail for the account"
  objMail.Recipients.Add (existAcct.Email1Address)
  objMail.Body = "This mail will get attached to the Account if E-mail Auto Linking is set for the Sent Items Folder"
  objMail.Send
  MsgBox ("New Mail Sent and it will be attached to the account with Full name Wide World Importers, if E-mail Auto linking is set.") 

Else

  MsgBox("Please create an account with Full name: Wide World Importers, give it a valid e-mail address, and re-run the script.") 

End If

Set objMail = Nothing Set existAcct = Nothing Set bcmAccountsFldr = Nothing Set bcmHistoryFolder = Nothing Set olFolders = Nothing Set bcmRootFolder = Nothing Set objNS = Nothing Set olApp = Nothing

End Sub

The following example code shows how to link an e-mail message to an Account or Business Contact object.

  private void LinkEmailMessageToContact()
        {
        Outlook.ApplicationClass _app = new Outlook.ApplicationClass();
        Outlook.Application olApp = (Outlook.Application)_app;
        Outlook.NameSpace olNameSpace = _app.GetNamespace("MAPI");
        Outlook.Folders folders = olNameSpace.Session.Folders;
        Outlook.Folder bcmRootFolder = (Outlook.Folder)folders["Business Contact Manager"];
        Outlook.Folder accountsFolder = (Outlook.Folder)bcmRootFolder.Folders["Accounts"];
        Outlook.Folder contactsFolder = (Outlook.Folder)bcmRootFolder.Folders["Business Contacts"];
        Outlook.Folder historyFolder = (Outlook.Folder)bcmRootFolder.Folders["Communication History"];
        Outlook.UserProperty userProp;

        string strQuery = "[FileAs] = 'Wide World Importers'";

        Outlook.ContactItem accountItem = (Outlook.ContactItem)accountsFolder.Items.Find(strQuery);
        if (accountItem != null)
        {
            Outlook.MailItem mailItem = (Outlook.MailItem)olApp.CreateItem(Outlook.OlItemType.olMailItem);
            mailItem.Subject = "A Test Mail";
            mailItem.Body = "This is the Body of a mail Message";
            mailItem.Recipients.Add(accountItem.Email1Address);
            mailItem.Send();

            Outlook.ContactItem newContact = (Outlook.ContactItem)contactsFolder.Items.Add("IPM.Contact.BCM.Contact");
            newContact.FullName = "John Smith";
            newContact.FileAs = "John Smith";

            Outlook.Folder inboxItemsFolder = (Outlook.Folder)olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.MailItem newMailItem = (Outlook.MailItem)inboxItemsFolder.Items.Find("[Subject] = 'Mail for the account'");

            Outlook.ContactItem objContact = (Outlook.ContactItem)contactsFolder.Items.Find("[FullName] = 'John Smith'");

            if (objContact != null)
            {
                Outlook.JournalItem mailJournal = (Outlook.JournalItem)historyFolder.Items.Add("IPM.Activity.BCM");
                mailJournal.Subject = newMailItem.Subject;
                mailJournal.Body = newMailItem.Body;
                mailJournal.Type = "E-mail Message";

                if (mailJournal.UserProperties["Parent Entity EntryID"] == null)
                {
                    userProp = mailJournal.UserProperties.Add("Parent Entity EntryID", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
                    userProp.Value = objContact.EntryID;
                }


                if (mailJournal.UserProperties["LinkToOriginal"] == null)
                {
                    userProp = mailJournal.UserProperties.Add("LinkToOriginal", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, false);
                    userProp.Value = newMailItem.EntryID;
                }

                mailJournal.Save();
            }
            else
            {
                Console.WriteLine("Please create a business contact with Full name 'John Smith', and re-run the script");
            }
        }
        else
        {
            Console.WriteLine("Please create an account with Full name: Wide World Importers, give it a valid e-mail address, and re-run the script");
        }

    } 
  Sub LinkEmailToContact()

MsgBox ("See that the E-mail Auto Linking is set...") Dim olApp As Outlook.Application Dim objNS As Outlook.NameSpace Dim olFolders As Outlook.Folders Dim bcmRootFolder As Outlook.Folder Dim bcmContactsFldr As Outlook.Folder Dim bcmAccountsFldr As Outlook.Folder Dim bcmHistoryFolder As Outlook.Folder Dim existAcct As Outlook.ContactItem Dim objMail As Outlook.MailItem Dim objContactItem As Outlook.ContactItem Dim mailJournal As Outlook.JournalItem Dim newContact As Outlook.ContactItem Dim userProp As Outlook.UserProperty

Set olApp = CreateObject("Outlook.Application") Set objNS = olApp.GetNamespace("MAPI") Set olFolders = objNS.Session.Folders Set bcmRootFolder = olFolders("Business Contact Manager") Set bcmAccountsFldr = bcmRootFolder.Folders("Accounts") Set bcmContactsFldr = bcmRootFolder.Folders("Business Contacts") Set bcmHistoryFolder = bcmRootFolder.Folders("Communication History")

Set existAcct = bcmAccountsFldr.Items.Find("[FileAs] = 'Wide World Importers'")

If Not TypeName(existAcct) = "Nothing" Then Set objMail = olApp.CreateItem(olMailItem) objMail.Subject = "Mail for the account" objMail.Recipients.Add (existAcct.Email1Address) objMail.Body = "This mail will get attached to the Account if E-mail Auto Linking is set for the Sent Items Folder" objMail.Send

  Set newContact = bcmContactsFldr.Items.Add("IPM.Contact.BCM.Contact")
  newContact.FullName = "John Smith"
  newContact.FileAs = "John Smith"
  newContact.Email1Address = "someone@example.com"
  newContact.Save

  Set objMail = objNS.GetDefaultFolder(olFolderInbox).Items.Find("[Subject] = 'Mail for the account'")

  Set objContactItem = bcmContactsFldr.Items.Find("[FileAs] = 'John Smith'")
  If Not TypeName(objContactItem) = "Nothing" Then
    Set mailJournal = bcmHistoryFolder.Items.Add("IPM.Activity.BCM")
    mailJournal.Subject = objMail.Subject
    mailJournal.Body = objMail.Body
    mailJournal.Type = "E-mail Message"
    
    If (mailJournal.UserProperties("Parent Entity EntryID") Is Nothing) Then
        Set userProp = mailJournal.UserProperties.Add("Parent Entity EntryID", olText, False, False)
        userProp.Value = objContactItem.EntryID
    End If
    
    If (mailJournal.UserProperties("LinkToOriginal") Is Nothing) Then
        Set userProp = mailJournal.UserProperties.Add("LinkToOriginal", olText, False, False)
        userProp.Value = objMail.EntryID
    End If

    mailJournal.Save

  Else

     MsgBox ("Please create a business contact with Full name 'John Smith', and re-run the script")
  
  End If

Else

  MsgBox ("Please create an account with Full name: Wide World Importers, give it a valid e-mail address, and re-run the script")

End If

Set mailJournal = Nothing Set newContact = Nothing Set objContactItem = Nothing Set objMail = Nothing Set existAcct = Nothing Set bcmAccountsFldr = Nothing Set bcmHistoryFolder = Nothing Set olFolders = Nothing Set bcmRootFolder = Nothing Set objNS = Nothing Set olApp = Nothing

End Sub

See Also

About Communication History Items | Office Developer Center: Outlook 2007