Getting From, SentOnBehalfOfName and Account name from MailItem Object (Outlook Shared Addins)

2.8k views Asked by At

I am using Vs2010 -> Extensibility->Shared Add-in I have added a event handler to my ItemSend

applicationObject.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(applicationObject_ItemSend);  

void applicationObject_ItemSend(object Item, ref bool Cancel)
{
    if(Item is Outlook.MailItem)
    {
        Outlook.MailItem mailItem = Item as Outlook.MailItem;
        if (mailItem != null)
        {
           MessageBox.Show("Sender's Email Address "+mailItem.SenderEmailAddress);
           MessageBox.Show("Sender's Email Address "+mailItem.SentOnBehalfOfName); 
          MessageBox.Show("Sender's Email Address "+mailItem.SendUsingAccount);
        }
    }
}

mailItem.SenderEmailAddress,mailItem.SentOnBehalfOfName and mailItem.SendUsingAccount I am geting all of this property null

Please can you anyone help me out I want to get From, SentOnBehalfOfName and Account name from with the email was sent.

2

There are 2 answers

4
Dmitry Streblechenko On BEST ANSWER

Sender related properties are set only after the message is actually sent and moved to the Sent Items folder. You might want to use the Items.ItemAdd event on the Sent Items folder.

0
C J On

This is the Code i have used

    public Outlook.MAPIFolder sentFolder = null;
    public Outlook.Items itmsSentFolder = null;
    sentFolder = applicationObject.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
    itmsSentFolder = sentFolder.Items;
    itmsSentFolder.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(itmsSentFolder_ItemAdd); 
void itmsSentFolder_ItemAdd(object Item)
        {

                if (Item is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = Item as Outlook.MailItem;
                    if (mailItem != null)
                    {
                        MessageBox.Show("Sender's Email Address " + mailItem.SenderEmailAddress);
                        MessageBox.Show("Sent On Behalf Of Name " + mailItem.SentOnBehalfOfName);
                        Outlook.Account ac = (Outlook.Account)mailItem.SendUsingAccount;
                        if(ac != null)
                        {
                            MessageBox.Show("Sender's Account Name " + ac.SmtpAddress);
                        }

                    }

            }
        }

Thanks for the idea Dmitry Streblechenko