2

I am retrieving, this mailItem in compose mode, But when I check for the mailItem.SenderEmailAddress, it is NULL, but all other properties have values there (Ex:- body, body format, to, .... ). How I get the sender email FROM THE MAIL-ITEM IT-SELF?

I am using Visual Studio 2013 with Addin express v.7.7.4087

Here is the code :-

        Outlook.Inspector currentInspector = null;
        Outlook.MailItem mailItem = null;
        Outlook.Folder outboxFolder = null;
        Outlook.Recipients recipients = null;

        const string PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E";

        currentInspector = Globals.ObjOutlook.ActiveInspector();

        if (currentInspector != null)
            {

            if (currentInspector.CurrentItem is Outlook.MailItem)
            {
                mailItem = currentInspector.CurrentItem as Outlook.MailItem;
                mailItem.Save();
                string sender = mailItem.SenderEmailAddress; //This is null 
            }
        }

P.S I have to deal with multiple mail boxes. so, I can't get current users address using Namespace. It always return me the address of primary mail box user.

Please see following screen shot enter image description here

Thanks in advance.

Kushan Randima

Kushan Randima
  • 2,174
  • 5
  • 31
  • 58

2 Answers2

4

Are you sending using multiple Exchange accounts? Use MailItem.SendUsingAccount, then read Account.SmtpAddress. If it is "", use Account.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress.

If MailItem.SendUsingAccount == null, you can assume the default account.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Awesome, this saved me. Thanks. – Kushan Randima Aug 14 '15 at 04:52
  • when I save the mail item in a self-created outlook folder and retreive it back, then 'SendUsingAccount' property is null. But it holds a value before save in outlook folder. So, I managed to add the value of "SendUsingAccount" as a user property before save it in OL folder and read it after getting it back. Is there any better solution for that? – Kushan Randima Aug 24 '15 at 03:09
  • How do you save the message to that folder? – Dmitry Streblechenko Aug 24 '15 at 03:16
2

Use the CurrentUser property of the Namespace class to get the currently logged-on user as a Recipient object. The Address property of the Recipient class returns a string representing the e-mail address of the Recipient.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks for the concern, Since I am dealing with multiple mail boxes, our solution will not work for me. Do you have any other suggestions please? I edited question too. Please see the changes. Thanks again – Kushan Randima Aug 13 '15 at 10:27
  • :If you are interested please commit for this : stackoverflow.com/documentation/outlook-addin/commit – Kushan Randima Jul 27 '16 at 05:24