none
office 2003 Can't get SMTP address of a local contact created by an exchange user RRS feed

  • Question

  • I created an addin of outlook2003 to check the smtp address of recipients of sending mail.But it works some error when adding a local contact comes from an exchange user of GAL.

    I had read http://www.pcreview.co.uk/forums/cant-get-smtp-address-contact-shares-email-address-exchange-user-t3055223.html but it's useless because I can't use Redemption dll and CDO.

    The method to get smtp address I used is follow as [C# sample Outlook add-in for VS 2005] on http://www.add-in-express.com/creating-addins-blog/2009/05/08/outlook-exchange-email-address-smtp/

    the code seem difficult to me, so I just copy and use it.

    By Debuging, I found that

    1 if directly use the exchange user without creating local contact , it works.

    2 if create it by show mail address in displayname, it works. but directly use the used exchange user , it error.

    3 if create it by not show, it error. And used one still error.

     4 if create it by show mail address in displayname , and then change the address to another, it still works, but smtp address show as  the changed one, through mail sent normally.

    What I want is : whichever using the local contact or exchange user,  whatever displayname changed , addins cant get the correct smtp address.

    Thanks & Regards

    Tai

    Wednesday, November 21, 2012 7:37 AM

Answers

All replies

  • BTW, the method I used is like:

      private string GetSMTPAddress(string exchangeAddress)
      {
       string smtpAddress = string.Empty;
       IAddrBook addrBook = GetAddrBook();
       if (addrBook != null)
        try
        {
         IntPtr szPtr = IntPtr.Zero;
         IntPtr propValuePtr = Marshal.AllocHGlobal(16);
         IntPtr adrListPtr = Marshal.AllocHGlobal(16);

         Marshal.WriteInt32(propValuePtr, (int)MAPI.PR_DISPLAY_NAME);
         Marshal.WriteInt32(new IntPtr(propValuePtr.ToInt32() + 4), 0);
         szPtr = Marshal.StringToHGlobalAnsi(exchangeAddress);
         Marshal.WriteInt64(new IntPtr(propValuePtr.ToInt32() + 8), szPtr.ToInt32());

         Marshal.WriteInt32(adrListPtr, 1);
         Marshal.WriteInt32(new IntPtr(adrListPtr.ToInt32() + 4), 0);
         Marshal.WriteInt32(new IntPtr(adrListPtr.ToInt32() + 8), 1);
         Marshal.WriteInt32(new IntPtr(adrListPtr.ToInt32() + 12), propValuePtr.ToInt32());
         try
         {
          if (addrBook.ResolveName(0, MAPI.MAPI_DIALOG, null, adrListPtr) == MAPI.S_OK)
          {
           SPropValue spValue = new SPropValue();
           int pcount = Marshal.ReadInt32(new IntPtr(adrListPtr.ToInt32() + 8));
           IntPtr props = new IntPtr(Marshal.ReadInt32(new IntPtr(adrListPtr.ToInt32() + 12)));
           for (int i = 0; i < pcount; i++)
           {
            spValue = (SPropValue)Marshal.PtrToStructure(
             new IntPtr(props.ToInt32() + (16 * i)), typeof(SPropValue));
            if (spValue.ulPropTag == MAPI.PR_ENTRYID)
            {
             IntPtr addrEntryPtr = IntPtr.Zero;
             IntPtr propAddressPtr = IntPtr.Zero;
             uint objType = 0;
             uint cb = (uint)(spValue.Value & 0xFFFFFFFF);
             IntPtr entryID = new IntPtr((int)(spValue.Value >> 32));
             if (addrBook.OpenEntry(cb, entryID, IntPtr.Zero, 0, out objType, out addrEntryPtr) == MAPI.S_OK)
              try
              {
               if (MAPI.HrGetOneProp(addrEntryPtr, MAPI.PR_EMS_AB_PROXY_ADDRESSES, out propAddressPtr) == MAPI.S_OK)
               {
                IntPtr emails = IntPtr.Zero;
                SPropValue addrValue = (SPropValue)Marshal.PtrToStructure(propAddressPtr, typeof(SPropValue));
                int acount = (int)(addrValue.Value & 0xFFFFFFFF);
                IntPtr pemails = new IntPtr((int)(addrValue.Value >> 32));
                for (int j = 0; j < acount; j++)
                {
                 emails = new IntPtr(Marshal.ReadInt32(new IntPtr(pemails.ToInt32() + (4 * j))));
                 smtpAddress = Marshal.PtrToStringAnsi(emails);
                 if (smtpAddress.IndexOf("SMTP:") == 0)
                 {
                  smtpAddress = smtpAddress.Substring(5, smtpAddress.Length - 5);
                  break;
                 }
                }
               }
              }
              finally
              {
               if (propAddressPtr != IntPtr.Zero)
                Marshal.Release(propAddressPtr);
               if (addrEntryPtr != IntPtr.Zero)
                Marshal.Release(addrEntryPtr);
              }
            }
           }
          }
         }
         finally
         {
          Marshal.FreeHGlobal(szPtr);
          Marshal.FreeHGlobal(propValuePtr);
          Marshal.FreeHGlobal(adrListPtr);
         }
        }
        finally
        {
         Marshal.ReleaseComObject(addrBook);
        }
       return smtpAddress;
      }

      private IAddrBook GetAddrBook()
      {
       if (MAPI.MAPIInitialize(IntPtr.Zero) == MAPI.S_OK)
       {
        IntPtr sessionPtr = IntPtr.Zero;
        MAPI.MAPILogonEx(0, null, null, MAPI.MAPI_EXTENDED | MAPI.MAPI_ALLOW_OTHERS, out sessionPtr);
        if (sessionPtr == IntPtr.Zero)
         MAPI.MAPILogonEx(0, null, null, MAPI.MAPI_EXTENDED | MAPI.MAPI_NEW_SESSION | MAPI.MAPI_USE_DEFAULT, out sessionPtr);
        if (sessionPtr != IntPtr.Zero)
         try
         {
          object sessionObj = Marshal.GetObjectForIUnknown(sessionPtr);
          if (sessionObj != null)
           try
           {
            IMAPISession session = sessionObj as IMAPISession;
            if (session != null)
            {
             IntPtr addrBookPtr = IntPtr.Zero;
             session.OpenAddressBook(0, IntPtr.Zero, MAPI.AB_NO_DIALOG, out addrBookPtr);
             if (addrBookPtr != IntPtr.Zero)
              try
              {
               object addrBookObj = Marshal.GetObjectForIUnknown(addrBookPtr);
               if (addrBookObj != null)
                return addrBookObj as IAddrBook;
              }
              finally
              {
               Marshal.Release(addrBookPtr);
              }
            }
           }
           finally
           {
            Marshal.ReleaseComObject(sessionObj);
           }
         }
         finally
         {
          Marshal.Release(sessionPtr);
         }
       }
       else
        throw new ApplicationException("MAPI can not be initialized.");
       return null;
      }

    Thanks for any suggestion.

    Tai

    Wednesday, November 21, 2012 7:52 AM
  • HI Tai,

    Tanks for positing in the MSDN Forum.

    I will invoke some experts into your thread and to see whether they can help you. There might be some time delay, appreciate for your patience.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 22, 2012 8:03 AM
    Moderator
  • To Tom

    Thank you very much.


    • Edited by TaiZZH Friday, November 23, 2012 1:11 AM
    Friday, November 23, 2012 1:10 AM
  • Hi, Tai,

    The question falls into the extended support category which requires a more in-depth level of support. Please visit the below link to see the various paid support options that are available to better meet your needs.

    http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    best regards,


    Forrest Guo | MSDN Community Support | Feedback to manager

    Friday, November 23, 2012 1:58 PM
    Moderator