Sameer Kulkarni
Sameer Kulkarni

Reputation: 31

Outlook.MailItem - Unable to get To, CC & BCC from Outlook

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

I need All the email details before sending an email.

When i run the below code @ my home i get proper results by putting some personal email id its working.

But when i run this similar code in office outlook machine i get the names.

As by default outlook's check names code is enabled, this returns first and last name.

I am using Outlook 2010 @ both the places. Office outlook is mapped to office active directory. my home outlook is not mapped. Can anyone provide a common solution which will give me all the email address used(to, cc, bcc & from) irrespective of active directory mapped or not.

private void ThisAddIn_Startup(object sender, System.EventArgs e)        { 
          Application.ItemSend += new           Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);  
}

void Application_ItemSend(object Item, ref bool Cancel)        {
Outlook.MailItem mail = Item as Outlook.MailItem;            
Outlook.Inspector inspector = Item as Outlook.Inspector;

System.Windows.Forms.MessageBox.Show(mail.CC);
System.Windows.Forms.MessageBox.Show(mail.BCC);

} 

Upvotes: 0

Views: 2359

Answers (2)

Mr Z
Mr Z

Reputation: 101

Maybe is late, but someone can check this code (it's works for me)

    private string[] GetCCBCCFromEmail(Outlook.MailItem email)
    {
        string[] ccBCC = new string[] { "", "" };//cc y bcc
        Outlook.Recipients recipients = email.Recipients;

        foreach (Outlook.Recipient item in recipients)
        {
            switch (item.Type)
            {

                case (int)Outlook.OlMailRecipientType.olCC:                        
                    ccBCC[0] += GetEmail(item.AddressEntry) + ";";
                    break;
                case (int)Outlook.OlMailRecipientType.olBCC:
                    ccBCC[1] += GetEmail(item.AddressEntry) + ";";
                    break;
            }
        }
        return ccBCC;
    }
    private string GetEmail(Outlook.AddressEntry address) 
    {
        string addressStr = "";

        if (address.AddressEntryUserType ==
                Outlook.OlAddressEntryUserType.
                olExchangeUserAddressEntry
                || address.AddressEntryUserType ==
                Outlook.OlAddressEntryUserType.
                olExchangeRemoteUserAddressEntry)
        {
            //Use the ExchangeUser object PrimarySMTPAddress
            Outlook.ExchangeUser exchUser =
                address.GetExchangeUser();
            if (exchUser != null)
            {
                addressStr = exchUser.PrimarySmtpAddress;
            }
        }
        //Get the address from externals
        if (address.AddressEntryUserType == Outlook.OlAddressEntryUserType.
            olSmtpAddressEntry)
        {
            addressStr = address.Address;
        }

        return addressStr;
    }

Hope it helps

Upvotes: 1

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66258

To/CC/BCC properties (corresponding to PR_DISPLAY_TO/CC/BCC in MAPI) are updated by the store provider when the item is saved (MailItem.Save). You can also access all recipients using the MailItem.Recipeints collection.

Upvotes: 0

Related Questions