I have written some code to make an outlook addin, which is working great. Except that the to field from outlook is displaying the contact na,e instead of the actual real address. IE it is displaying geoff instead of someone@gmail.com unfortunatly I want the who address not just the shortened version. Does anyone know where to get this.
My doe simply uses .To

Dani AI

Generated

— the To textbox is just a display string (display names). To get the actual SMTP addresses you need to walk the MailItem.Recipients collection and inspect each Recipient.AddressEntry. 's "make an enum" remark doesn't address this; the correct approach is to read each Recipient and query the AddressEntry for an SMTP address.

Recommended steps:

  • Iterate MailItem.Recipients (call rec.Resolve() if not resolved).
  • For each Recipient.AddressEntry try GetExchangeUser().PrimarySmtpAddress (Exchange users).
  • If that is null, use the AddressEntry.PropertyAccessor to read PR_SMTP_ADDRESS (http://schemas.microsoft.com/mapi/proptag/0x39FE001E) as a fallback.
  • If neither yields a value, fall back to AddressEntry.Address or Recipient.Address.
  • For distribution lists, expand the AddressEntry (GetExchangeDistributionList/GetMembers) and repeat for members.
  • Always release COM objects (Marshal.ReleaseComObject) in add-ins to avoid leaks.

Example VB.NET snippet:

Dim addresses As New List(Of String)
For Each rec As Outlook.Recipient In mail.Recipients
    If Not rec.Resolved Then rec.Resolve()
    Dim smtp As String = String.Empty
    Dim ae As Outlook.AddressEntry = rec.AddressEntry
    If ae IsNot Nothing Then
        Dim exchUser As Outlook.ExchangeUser = TryCast(ae.GetExchangeUser(), Outlook.ExchangeUser)
        If exchUser IsNot Nothing Then
            smtp = exchUser.PrimarySmtpAddress
        Else
            Try
                smtp = CStr(ae.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E"))
            Catch
                smtp = ae.Address
            End Try
        End If
    Else
        smtp = rec.Address
    End If
    addresses.Add(smtp)
Next

Caveats and tips: Exchange recipients may have AddressType "EX" and Address may be an X500 DN; PrimarySmtpAddress or PR_SMTP_ADDRESS is the reliable value. Test with GAL entries, contact items, and external SMTP recipients. For server-side or bulk work consider EWS or a safe third-party library (Redemption) to avoid Outlook security prompts. See Microsoft docs for AddressEntry.GetExchangeUser and PropertyAccessor.GetProperty for reference: AddressEntry.GetExchangeUser and PropertyAccessor.GetProperty.

Make an enum.

whats an enum?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.