Sameer Kulkarni
Sameer Kulkarni

Reputation: 31

Outlook.MailItem.Sender is null- to fetch From email address

I am trying to create Outlook Addin using C#. Customizing Application_ItemSend event of the Send button.

I am unable to get from/sender email address. I am getting following things as null:

Any pointers what is wrong? Is it my outlook account is incorrectly configured?

Any help appreciated. Thanks in advance.

Below is the code to fetch address which i am using:

 private string GetSenderSMTPAddress(Outlook.MailItem mail)
    {
        string PR_SMTP_ADDRESS =   @"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
        if (mail == null)
        {
            throw new ArgumentNullException();
        }
        if (mail.SenderEmailType == "EX")
        {
            Outlook.AddressEntry sender =
                mail.Sender;
            if (sender != null)
            {
                //Now we have an AddressEntry representing the Sender
                if (sender.AddressEntryUserType ==
                    Outlook.OlAddressEntryUserType.
                    olExchangeUserAddressEntry
                    || sender.AddressEntryUserType ==
                    Outlook.OlAddressEntryUserType.
                    olExchangeRemoteUserAddressEntry)
                {
                    //Use the ExchangeUser object PrimarySMTPAddress
                    Outlook.ExchangeUser exchUser =
                        sender.GetExchangeUser();
                    if (exchUser != null)
                    {
                        return exchUser.PrimarySmtpAddress;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return sender.PropertyAccessor.GetProperty(
                        PR_SMTP_ADDRESS) as string;
                }
            }
            else
            {
                return null;
            }
        }
        else
        {
            return mail.SenderEmailAddress;
        }
    }

Upvotes: 3

Views: 4383

Answers (3)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66306

Sender related properties are only set after the message is actually sent. Try to use the Items.ItemAdd event on the Sent Items folder (retrieved using Namespace.GetDefaultFolder).

If SendUsingAccount property is not set, you can assume the default account is being used - use the first account from the Namespace.Accounts collection and retrieve the Account.SmtpAddress property.

Upvotes: 3

Max
Max

Reputation: 759

go for

 MailItem.SendUsingAccount.DisplayName

Upvotes: 0

Clever Neologism
Clever Neologism

Reputation: 1332

http://social.msdn.microsoft.com/Forums/vstudio/en-US/38d7b25a-8762-40c6-9e0a-903549462c0b/outlookmailitemsendername-always-null-under-outlook-2007-wvsto?forum=vsto

Short answer: those items aren't populated until it's actually sent (this hook is when you are just about to send the command to the exchange server to send the email). Use the "SendUsingAccount" field instead, as it will have all that information (in addition to the information you can find in the user's mailbox/account objects).

I'm pretty sure the reason why is that those field are not filled in until dynamic rules and policies are applied server-side.

Upvotes: 0

Related Questions