none
Not able to read Sender Infromation from c# code :: Getting Unspecified error (Exception from HRESULT: 0x80004004 (E_Abort)) RRS feed

  • Question

  • Hi,

    My aim is to read basic information like sender Name, email address from outlook mail item for that I created one console application. Blow basic code I used for that.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Interop.Outlook;
    
    namespace MAPI
    {
        class Program
        {
            static void Main(string[] args)
            {
    
    
                string lblSubject, txtBody, lblSenderName, lblSenderEmail;
                 List<string> litems = new List<string>();
    
                try
                {
                    Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
                    Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
                    Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                    if (myInbox.Items.Count > 0)
                    {
                        
                        lblSubject = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Subject;
                        
    
                        litems.Add(lblSubject);
    
                        txtBody = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Body;
                        litems.Add(txtBody);
    
                        lblSenderName = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).SenderName;
                        litems.Add(lblSenderName);
    
                        lblSenderEmail = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).SenderEmailAddress;
                        litems.Add(lblSenderEmail);
    
    
                        Console.WriteLine(lblSubject +  "," + txtBody + "," + lblSenderName + "," + lblSenderEmail);
    
                    }
                    else
                    {
                        Console.WriteLine("There are no emails in your Inbox.");
                    }
                }
                catch (System.Exception ex)
                {
                    foreach(string item in litems)
                    {
                        Console.Write(item);
                    }
    
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    Console.ReadLine();
                }
    
            }
        }
    }
    

    Above code target build is x86 machines.

    I'm not able to read the information what I'm expected from my code, getting error in outlook 2010 in few machines.

    Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

    and in few machines

    Unspecified error (Operation aborted from HRESULT: 0x80004004(E_Abort))

    I hope my code is perfect because, I can able to get same information from different exchange server as well different outlook version say 2013, 2016 with the same code.

    System specifications where I'm getting error:

    Outlook 2010

    Windows 7 64 bit

    McAfee End point protection (its protecting outlook emails also I think - not having permissions to change settings here )

    Some group policies are applied on same machine

    And I'm having complete rights(Admin) on my machine

    Can you please help which one blocking me to get those details or any code  changes needed ??

    Or any settings in outlook 2010 client need to be changed?? or McAfee having any special setting to block reading of outlook mails programmatically?? or Is there any chance to block programatic reading of outlook items using group policy??

    Is there any permissions at exchange server level which blocking me?? (exchange sever 2003 - patch applied to support 2010)

    Please Help me out from this issue. YOUR HELP SHOULD BE MUCH APPRICIATED.

    Feel free to ask any other information needed for you to sortout this issue.

    Thanks,

    Adithya.

    Wednesday, March 16, 2016 7:58 AM

All replies

  • Hello Adithya,

    First of all, I have  noticed the following line of code where you can each object in the folder to the MailItem class:

    lblSenderEmail = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).SenderEmailAddress;

    Please remember that folders may contain various types of items. You need to check the type first. Take a look at the following sample code:

    if (this.Application.ActiveExplorer().Selection.Count > 0)
            {
                Object selObject = this.Application.ActiveExplorer().Selection[1];
                if (selObject is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem =
                        (selObject as Outlook.MailItem);
                    itemMessage = "The item is an e-mail message." +
                        " The subject is " + mailItem.Subject + ".";
                    mailItem.Display(false);
                }
                else if (selObject is Outlook.ContactItem)
                {
                    Outlook.ContactItem contactItem =
                        (selObject as Outlook.ContactItem);
                    itemMessage = "The item is a contact." +
                        " The full name is " + contactItem.Subject + ".";
                    contactItem.Display(false);
                }
                else if (selObject is Outlook.AppointmentItem)
                {
                    Outlook.AppointmentItem apptItem =
                        (selObject as Outlook.AppointmentItem);
                    itemMessage = "The item is an appointment." +
                        " The subject is " + apptItem.Subject + ".";
                }
                else if (selObject is Outlook.TaskItem)
                {
                    Outlook.TaskItem taskItem =
                        (selObject as Outlook.TaskItem);
                    itemMessage = "The item is a task. The body is "
                        + taskItem.Body + ".";
                }
                else if (selObject is Outlook.MeetingItem)
                {
                    Outlook.MeetingItem meetingItem =
                        (selObject as Outlook.MeetingItem);
                    itemMessage = "The item is a meeting item. " +
                         "The subject is " + meetingItem.Subject + ".";
                }

    Read more about that in the following articles:

    How to: Programmatically Determine the Current Outlook Item

    How To Get the Currently Selected Item in an Outlook Folder from Visual Basic

    Most probably you faced with a standard Outlook security issue. "Security" in this context refers to the so-called "object model guard" that triggers security prompts and blocks access to certain features in an effort to prevent malicious programs from harvesting email addresses from Outlook data and using Outlook to propagate viruses and spam. These prompts cannot simply be turned off, except in Outlook 2007 and later with an anti-virus application running. You can read about possible ways for suppressing or avoiding such issues in the Outlook "Object Model Guard" Security Issues for Developers article. 

    The most common ways are:

    1. Use Extended MAPI (or any other third-party wrapper around that API) instead of OOM.

    2. Use the Outlook Security Manager component. 

    3. The administrator may choose to loosen Outlook security for some or all users via GPO.


    Wednesday, March 16, 2016 1:30 PM
  • Thank you Eugene Astafiev for your kind reply with good information.

    My Inbox containing only Mail Items nothing else so no need to check for item type I think and I forgot to mention in my Question, I'can able to read Subject of mail item with the same code, but not other information like Sender Name, Sender Mail address, Body.

    Sorry, I already gone through all suggested articles before posting my question here because I'm not able to get expected things even if I followed steps provided. 

    I'm suspecting there may be some rules enabled in Security Compliance Manager for Office 2010, they may dragging me back I think, if yes can you please help me how to make this disable for particular user(for me).

    Thanks.

    Wednesday, March 16, 2016 3:31 PM
  • Hi Adithya,

    To narrow down this issue, I suggest that change the index in the previews code to see whether this issue is relative to the specific item.

    In addition, I suggest that you send a new email to yourself and using the VBA code below to read the subject:

    Sub printCurrentMessageSubject()
    Debug.Print Application.ActiveInspector.CurrentItem.Subject
    End Sub

    Here are the test steps:
    1. Open Outlook VBE(Alt & F11)

    2. Copy the code into ThisOutlookSessition module

    3. Open a message in the inspector and run the code

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 17, 2016 9:43 AM
    Moderator
  • Hi Adithya,

    I am also facing same issue.

    Did you find any solution for this?

    Thanks in Advance...!!!

    Monday, November 28, 2016 8:58 AM
  • Hello Sravanthi,

    What issue did you meet on the way? Could you please be more specific?

    Do you automate Outlook form another application?

    Anyway, I'd suggest asking new questions on separate forum threads. Thus, forum readers may recognize questions and answers easily. 


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, November 28, 2016 1:39 PM
  • Hi Eugene,

    I am facing below issue.

     

    I'can able to read Subject of mail item with the similar code what adithya mentioned, but not other information like Sender Name, Sender Mail address, Body.

    while reading the properties i am getting below exception.

    Err Msg: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
    Err Code: -2147467260

    Thanks,

    Tuesday, November 29, 2016 5:36 AM
  • Hello Sravanthi,

    I need some details to answer this, which outlook client you are using? Is there any custom GPO's applied to your user id apart from Microsoft standard GPO's for Outlook? Is your organization ready relax outlook security for this requirement??

    You can contact over a mail

    aditya.dmar@outlook.com


    Thanks,

    A.


    • Edited by Adithya D Tuesday, June 20, 2017 12:20 PM
    Tuesday, November 29, 2016 6:06 AM
  • Hi Aditya,

    We are using outlook 2013 version.

    I am not sure about the remaning questions.

    Can you please let me know that issue is mainly causing due to security or is there any thing wrong with code.

    Thanks in advance...

    Tuesday, November 29, 2016 7:34 AM
  • Sravanthi,

    Most probably you get a security issue in OOM. "Security" in this context refers to the so-called "object model guard" that triggers security prompts and blocks access to certain features in an effort to prevent malicious programs from harvesting email addresses from Outlook data and using Outlook to propagate viruses and spam. Read more about that on the Outlook "Object Model Guard" Security Issues for Developers page. 


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Wednesday, November 30, 2016 12:41 PM
  • Hi Sravanthi,

    I am facing the same issue. Did you find any solution for this? If so, Could you please share your solution.

    Thanks in Advance.

    Thursday, July 20, 2017 12:59 PM