Eugene Astafiev

How To: Send a Contact item in Outlook programmatically

There are three ways of sending a contact in Outlook. Today we will examine them in detail. Please note that the ContactItem class doesn’t provide the Send method like the MailItem class does. You may wonder how a contact item can be sent then? In Outlook’s 2010 Object Browser I have encountered two forward-like methods: ForwardAsVcard and ForwardAsBusinessCard. Both methods return a mail item which can be sent afterwards. So far, so good. But what is the third way? The answer is simple – you can attach a contact to an e-mail like an Outlook embedded item. Let’s consider all of them in depth!

1. The most trivial and direct way is to attach a contact to an e-mail message. Not so long ago I described How to add an existing Outlook e-mail message as an attachment. Now I am using a contact instead of a mail item.

C#:

private void SendContactDirectAsAttachment(Outlook.ContactItem contact)
{
    Outlook.MailItem mail = null;
    Outlook.Recipients recipients = null;
    Outlook.Recipient recipient = null;
    Outlook.Attachments attachments = null;
    Outlook.Attachment attachment = null;
    try
    {
        mail = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
        attachments = mail.Attachments;
        attachment = attachments.Add(contact, Outlook.OlAttachmentType.olEmbeddeditem,
                     1, "The attached contact");
        mail.Subject = "A programatically generated e-mail";
        recipients = mail.Recipients;
        recipient = recipients.Add("Eugene Astafiev");
        recipient.Resolve();
        if (recipient.Resolved)
        {
            mail.Send();
        }
        else
        {
            System.Windows.Forms.MessageBox.Show(
               "There is no such record in your address book.");
        }
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message,
           "An exception is occured in the code of add-in.");
    }
    finally
    {
        if (attachment != null) Marshal.ReleaseComObject(attachment);
        if (attachments != null) Marshal.ReleaseComObject(attachments);
        if (mail != null) Marshal.ReleaseComObject(mail);
        if (recipient != null) Marshal.ReleaseComObject(recipient);
        if (recipients != null) Marshal.ReleaseComObject(recipients);
    }
}

VB.NET:

Private Sub SendContactDirectAsAttachment(ByRef contact As Outlook.ContactItem)
    Dim mail As Outlook.MailItem = Nothing
    Dim recipients As Outlook.Recipients = Nothing
    Dim recipient As Outlook.Recipient = Nothing
    Dim attachments As Outlook.Attachments = Nothing
    Dim attachment As Outlook.Attachment = Nothing
    Try
        mail = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem)
        attachments = mail.Attachments
        attachment = attachments.Add(contact, Outlook.OlAttachmentType.olEmbeddeditem, _
                     1, "The attached contact")
        mail.Subject = "A programatically generated e-mail"
        recipients = mail.Recipients
        recipient = recipients.Add("Eugene Astafiev")
        recipient.Resolve()
        If (recipient.Resolved) Then
            mail.Send()
        Else
            System.Windows.Forms.MessageBox.Show(
                "There is no such record in your address book.")
        End If
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message,
            "An exception is occured in the code of add-in.")
    Finally
        If Not IsNothing(attachment) Then Marshal.ReleaseComObject(attachment)
        If Not IsNothing(attachments) Then Marshal.ReleaseComObject(attachments)
        If Not IsNothing(mail) Then Marshal.ReleaseComObject(mail)
        If Not IsNothing(recipient) Then Marshal.ReleaseComObject(recipient)
        If Not IsNothing(recipients) Then Marshal.ReleaseComObject(recipients)
    End Try
End Sub

You can read more on How to create and send an Outlook message programmatically in my previous posts.

2. The version-neutral interops (correspond to the Outlook 2000 Object Model) provide only one method for such task – ForwardAsVcard. It doesn’t accept any parameters. A new mail item with a contact information attached (the vCard format is used) is returned. Please read more about the vCard format in Wikipedia.

C#:

private void SendContactUsingForwardAsVCard(Outlook.ContactItem contact)
{
    Outlook.MailItem mail = null;
    Outlook.Recipients recipients = null;
    Outlook.Recipient recipient = null;
    try
    {
        mail = contact.ForwardAsVcard();
        mail.Subject = "A programatically generated e-mail";
        recipients = mail.Recipients;
        recipient = recipients.Add("Eugene Astafiev");
        recipient.Resolve();
        if (recipient.Resolved)
        {
            mail.Send();
        }
        else
        {
            System.Windows.Forms.MessageBox.Show(
                "There is no such record in your address book.");
        }
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message,
            "An exception is occured in the code of add-in.");
    }
    finally
    {
       if (mail != null) Marshal.ReleaseComObject(mail);
       if (recipient != null) Marshal.ReleaseComObject(recipient);
       if (recipients != null) Marshal.ReleaseComObject(recipients);
    }
}

VB.NET:

Private Sub SendContactUsingForwardAsVCard(ByRef contact As Outlook.ContactItem)
    Dim mail As Outlook.MailItem = Nothing
    Dim recipients As Outlook.Recipients = Nothing
    Dim recipient As Outlook.Recipient = Nothing
    Try
        mail = contact.ForwardAsVcard()
        mail.Subject = "A programatically generated e-mail"
        recipients = mail.Recipients
        recipient = recipients.Add("Eugene Astafiev")
        recipient.Resolve()
        If (recipient.Resolved) Then
            mail.Send()
        Else
            System.Windows.Forms.MessageBox.Show(
                "There is no such record in your address book.")
        End If
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message,
            "An exception is occured in the code of add-in.")
    Finally
        If Not IsNothing(mail) Then Marshal.ReleaseComObject(mail)
        If Not IsNothing(recipient) Then Marshal.ReleaseComObject(recipient)
        If Not IsNothing(recipients) Then Marshal.ReleaseComObject(recipients)
    End Try
End Sub

3. Outlook 2007 introduced the ForwardAsBusinessCard method. It doesn’t accept any parameters either and returns a new MailItem object with the contact information attached (in vCard format again). The difference between the ForwardAsVcard and ForwardAsBusinessCard methods is that the latter generates and includes an image of the Electronic Business Card in the Body property of the MailItem object (only if the HTML body format is used). If PlainText or RichText is used, the behavior of these methods is identical: the vCard will be attached to an e-mail message. Please note that the ForwardAsBusinessCard method attaches a vCard file, which contains only the contact information included in the Electronic Business Card associated with the ContactItem object (any other contact information is excluded from the vCard file).

C#:

private void SendContactUsingForwardAsBusinessCard(Outlook.ContactItem contact)
{
    Outlook.MailItem mail = null;
    Outlook.Recipients recipients = null;
    Outlook.Recipient recipient = null;
    try
    {
        mail = contact.GetType().InvokeMember("ForwardAsBusinessCard",
                   System.Reflection.BindingFlags.Instance |
                   System.Reflection.BindingFlags.InvokeMethod |
                   System.Reflection.BindingFlags.Public,
                   null, contact, null) as Outlook.MailItem;
        mail.Subject = "A programatically generated e-mail";
        recipients = mail.Recipients;
        recipient = recipients.Add("Eugene Astafiev");
        recipient.Resolve();
        if (recipient.Resolved)
        {
            mail.Send();
        }
        else
        {
           System.Windows.Forms.MessageBox.Show(
               "There is no such record in your address book.");
        }
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message,
            "An exception is occured in the code of add-in.");
    }
    finally
    {
        if (mail != null) Marshal.ReleaseComObject(mail);
        if (recipient != null) Marshal.ReleaseComObject(recipient);
        if (recipients != null) Marshal.ReleaseComObject(recipients);
    }
}

VB.NET:

Private Sub SendContactUsingForwardAsBusinessCard(ByRef contact As Outlook.ContactItem)
    Dim mail As Outlook.MailItem = Nothing
    Dim recipients As Outlook.Recipients = Nothing
    Dim recipient As Outlook.Recipient = Nothing
    Try
        mail = contact.GetType().InvokeMember("ForwardAsBusinessCard", _
                 System.Reflection.BindingFlags.Instance Or _
                 System.Reflection.BindingFlags.InvokeMethod Or _
                 System.Reflection.BindingFlags.Public, _
                 Nothing, contact, Nothing)
        mail.Subject = "A programatically generated e-mail"
        recipients = mail.Recipients
        recipient = recipients.Add("Eugene Astafiev")
        recipient.Resolve()
        If (recipient.Resolved) Then
            mail.Send()
        Else
            System.Windows.Forms.MessageBox.Show(
                "There is no such record in your address book.")
        End If
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message,
            "An exception is occured in the code of add-in.")
    Finally
        If Not IsNothing(mail) Then Marshal.ReleaseComObject(mail)
        If Not IsNothing(recipient) Then Marshal.ReleaseComObject(recipient)
        If Not IsNothing(recipients) Then Marshal.ReleaseComObject(recipients)
    End Try
End Sub

As you can see I used the late-binding technology to access the ForwardAsBusinessCard method using version-neutral interops. If you use Outlook 2007 interops (or above), you can call this method directly. You can read more about late-binding and interop assemblies in the Supporting several Office versions in an add-in. Interop assemblies and late binding. article.

See you on our forums and in the e-mail support!

2 Comments

  • Robert says:

    Maybe a silly question, how do you use the SendContactUsingForwardAsBusinessCard from vb.net. New to using outlook and vb.net
    Thanks in advance

  • Eugene Astafiev says:

    Hi Robert,

    For example, the following code gets the first contact item from the Contacts folder in Outlook and passes it to the SendContactUsingForwardAsBusinessCard method for sending further:


    Dim ns As Outlook.NameSpace = OutlookApp.GetNamespace("MAPI")
    Dim folder As Outlook.MAPIFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
    Dim items As Outlook.Items = folder.Items
    Dim contact As Outlook.ContactItem = TryCast(items.GetFirst(),Outlook.ContactItem)
    SendContactUsingForwardAsBusinessCard(contact)
    If contact IsNot Nothing Then Marshal.ReleaseComObject(contact)
    If items IsNot Nothing Then Marshal.ReleaseComObject(items)
    If folder IsNot Nothing Then Marshal.ReleaseComObject(folder)
    If ns IsNot Nothing Then Marshal.ReleaseComObject(ns)

Post a comment

Have any questions? Ask us right now!