How to Get Senders Address using Mapi

Add-in Express™ Support Service
That's what is more important than anything else

How to Get Senders Address using Mapi
 
Thomas Grossen




Posts: 134
Joined: 2005-10-21
Hi again,

Do you have any example in Delphi about how to get the e-mail senders address using MAPI (for Outlook version < 2003)?

Thank you.
Thomas.
Posted 20 Nov, 2006 08:05:32 Top
Dmitry Kostochko


Add-in Express team


Posts: 2865
Joined: 2004-04-05
Hi Thomas,

We don't have such an example. But if you have some working code written in another programming language, you can send it to me, I will try to convert it to Delphi.


P.S. Note that we take up your forum requests in the order we receive them.
Besides, it may take us some time to investigate your issue. Please be sure we will let you know as soon as the best possible solution is found.


Posted 20 Nov, 2006 11:44:36 Top
Thomas Grossen




Posts: 134
Joined: 2005-10-21
Dmitry,

Hi Dmtry,

here is the code that I am using in the .NET version. I have written the code using Sergey's sample:

http://www.add-in-express.com/projects/getsenderemailexample.zip

So if you have something that can help me in Delphi I would be grateful :)


        static public string GetSendersEmailAdress(Outlook._MailItem oMail)
        {
            string senderEmail = String.Empty;
            object mapiObject = null;
            mapiObject = oMail.MAPIOBJECT;
            if (mapiObject != null)
            {
                string senderType = String.Empty;
                IntPtr unk = IntPtr.Zero;
                IntPtr unkObj = IntPtr.Zero;
                try
                {
                    unkObj = Marshal.GetIUnknownForObject(mapiObject);
                    Guid iMapiProp = new Guid("00020303-0000-0000-C000-000000000046");
                    Marshal.QueryInterface(unkObj, ref iMapiProp, out unk);
                    if (unk != IntPtr.Zero)
                    {
                        IntPtr pPropValue = IntPtr.Zero;
                        try
                        {
                            MAPI.HrGetOneProp(unk, MAPI.PR_SENDER_EMAIL_ADDRESS, out pPropValue);
                            MAPI.SPropValue propValue = (MAPI.SPropValue)Marshal.PtrToStructure(pPropValue, typeof(MAPI.SPropValue));
                            propValue.Value &= 0xFFFFFFFF;
                            senderEmail = Marshal.PtrToStringAnsi(new IntPtr(propValue.Value));
                        }
                        catch
                        {
                        }
                        if (pPropValue != IntPtr.Zero)
                            MAPI.MAPIFreeBuffer(pPropValue);

                        /* To get Senders Address Type ("EX" "SMTP")            
                        IntPtr pPropValue = IntPtr.Zero;
                        try
                        {
                            MAPI.HrGetOneProp(unk, MAPI.PR_SENDER_ADDRTYPE, out pPropValue);
                            MAPI.SPropValue propValue = (MAPI.SPropValue)Marshal.PtrToStructure(pPropValue, typeof(MAPI.SPropValue));
                            propValue.Value &= 0xFFFFFFFF;
                            senderType = Marshal.PtrToStringAnsi(new IntPtr(propValue.Value));
                        }
                        catch
                        {
                        }
                        if (pPropValue != IntPtr.Zero)
                            MAPI.MAPIFreeBuffer(pPropValue);
                        */
                    }
                }
                finally
                {
                    Marshal.Release(unkObj);
                    if (unk != IntPtr.Zero) Marshal.Release(unk);
                    Marshal.ReleaseComObject(mapiObject);
                }
            }
            return senderEmail;
        }



Thomas.
Posted 20 Nov, 2006 12:25:40 Top
Dmitry Kostochko


Add-in Express team


Posts: 2865
Joined: 2004-04-05
Hi Thomas,

Thank you. You can download example for Delphi here:
http://www.add-in-express.com/projects/getsenderemailexample_d7.zip


Posted 20 Nov, 2006 13:11:31 Top
Thomas Grossen




Posts: 134
Joined: 2005-10-21
Hi,

Thank you for the sample; it works perfectly! :D

Thomas.
Posted 20 Nov, 2006 17:18:36 Top