コード例 #1
0
        //This code has been sourced from:
        //https://www.add-in-express.com/creating-addins-blog/2009/05/08/outlook-exchange-email-address-smtp/
        //https://www.add-in-express.com/files/howtos/blog/adx-ol-smtp-address-cs.zip
        public static string ADX_GetSMTPAddress(string exchangeAddress)
        {
            string    smtpAddress = string.Empty;
            IAddrBook addrBook    = ADX_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);
        }