How to avoid Outlook security alert when reading outlook message from C# program

Naga picture Naga · Oct 24, 2008 · Viewed 39.5k times · Source

I have a requirement of reading subject, sender address and message body of new message in my Outlook inbox from a C# program. But I am getting security alert 'A Program is trying to access e-mail addresses you have stored in Outlook. Do you want to allow this'.

By some googling I found few third party COM libraries to avoid this. But I am looking for a solution which don't require any third party COM library.

Answer

kumar picture kumar · Jul 29, 2009

I ran into same issue while accessing sender email address for outlook mail item. To avoid 'security alert' do not create new Application object, instead use Globals.ThisAddIn.Application to create new mailitem.

string GetSenderEmail(Outlook.MailItem item)
    {
        string emailAddress = "";
        if (item.SenderEmailType == "EX")
        {
            Outlook.MailItem tempItem = (Outlook.MailItem)Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem);
            tempItem.To = item.SenderEmailAddress;
            emailAddress = tempItem.Recipients[1].AddressEntry.GetExchangeUser().PrimarySmtpAddress.Trim();

        }
        else
        {
            emailAddress = item.SenderEmailAddress.Trim();

        }

        return emailAddress;
    }