Exception from HRESULT: 0x80004004 (E_ABORT) at Outlook.Mailitem.getHtmlBody()

0

I have a custom add-in which gets the body on clicking a button when it got installed. so I am getting this error on a customer machine. code is working fine on my side and for other customers but one customer is facing this problem.

this is my code

 private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        Microsoft.Office.Interop.Outlook.Application olApp = new Microsoft.Office.Interop.Outlook.Application();
        Microsoft.Office.Interop.Outlook.NameSpace ns = olApp.GetNamespace("MAPI");
        Explorer olExp = olApp.ActiveExplorer();
        Selection olSel = olExp.Selection;
        string msg = "";
        int iterate = 1;
        MAPIFolder inbox = null;
        if (olSel.Count > 1)
        {
            MessageBox.Show("Sorry! You can't report more then 1 email at a time", "Report Email");
            return;
        }
        foreach (_MailItem mail in olSel)
        {
            inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
           
            
            mail.GetInspector.Display(false);
            Thread.Sleep(2100);
            string screenShot = getScreenShot(mail);
            mail.GetInspector.Close(OlInspectorClose.olDiscard);
            String msgToShow = "Are you sure you want to report this email as suspicious?";
            if (mail != null && mail.Subject != null)
                msgToShow += "\n\nSubject : " + mail.Subject.ToString();
            DialogResult dr = MessageBox.Show(msgToShow, "Please Confirm", MessageBoxButtons.YesNo,
            MessageBoxIcon.Exclamation);

            if (dr == DialogResult.Yes)
            {
                msg += reportMail(mail, screenShot, e);
            }
            else {
                return;
            }
            iterate++;
            break;
        }
        if (!msg.Equals(""))
            MessageBox.Show(msg, "Report Email");
        else
            return;

        if (!msg.Contains("Success"))
            return;

        MailItem moveMail = null;
        MAPIFolder subfolder = null;
        try
        {
            subfolder = inbox.Folders["Reported Emails"];
        }
        catch (System.Exception ex) {
            subfolder = inbox.Folders.Add("Reported Emails", OlDefaultFolders.olFolderInbox);
        }
        
        foreach (MailItem eMail in olSel)
            {
                try
                {
                    moveMail = eMail;
                    if (moveMail != null)
                    {
                        string titleSubject = (string)moveMail.Subject;
                            moveMail.Move(subfolder);
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    }

    private string reportMail(_MailItem mail, string screenShot, RibbonControlEventArgs e)
    {
        try
        {


            var m = Globals.ThisAddIn.Application.GetNamespace("MAPI");
            
            
            var mailitem = mail;

            if (mailitem != null)
            {
               // Console.WriteLine("Email body ::: " + mailitem.HTMLBody);
                String reporterEmail = getReporterEmail(mailitem);
                String senderEmailAddress = "";
                String senderName = "";
                AddressEntry mailsender;
                if (reporterEmail.Equals(""))
                {
                    MessageBox.Show("Sorry! This email can't be reported because you are not included in Recipients.", "Report Email");
                }
                else
                { 
                    if (mailitem.SenderEmailType == "EX")
                    {
                        mailsender = mailitem.Sender;
                        if (mailsender != null)
                        {
                            if (mailsender.AddressEntryUserType == OlAddressEntryUserType.olExchangeUserAddressEntry || mailsender.AddressEntryUserType == OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
                            {
                                ExchangeUser exchUser = mailsender.GetExchangeUser();
                                if (exchUser != null)
                                {
                                    senderEmailAddress = exchUser.PrimarySmtpAddress;
                                    senderName = exchUser.Name;
                                }
                            }
                        }
                    }
                    else
                    {
                        senderEmailAddress = mailitem.SenderEmailAddress;
                        senderName = mailitem.SenderName;
                    }
                    String emailHeader = mailitem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E");
                    String emailBody =  mailitem.Body.Replace("%"," percent").Replace("#","").Replace("|", "");

                    //String res = HttpPost(Properties.Settings.Default.address, "ReporterEmail=" + reporterEmail + "&suspectedName=" + senderName + "&FromEmail=" + senderEmailAddress 
                    //    + "&ToEmail=" + mailitem.To + "&Subject=" + mailitem.Subject + "&EmailBody=" + emailBody + "&AttachmentName=" + GetAttachments(mailitem) 
                    //    + "&reporter=" + reporterEmail + "&emailHeader=" + emailHeader + "&mailImage=" + screenShot);


                    String htmlBody = mail.HTMLBody;
                    MessageBox.Show(htmlBody);

                    Dictionary<string, object> postParameters = new Dictionary<string, object>();
                    postParameters.Add("ReporterEmail", reporterEmail);
                    postParameters.Add("suspectedName", senderName);
                    postParameters.Add("FromEmail", senderEmailAddress);
                    postParameters.Add("ToEmail", mailitem.To);
                    postParameters.Add("Subject", mailitem.Subject);
                    postParameters.Add("EmailBody", emailBody);
                    List<String> attachmentDetails = GetAttachments(mailitem);
                    postParameters.Add("AttachmentName", attachmentDetails[0]);
                    postParameters.Add("reporter", reporterEmail);
                    postParameters.Add("emailHeader", emailHeader);
                    postParameters.Add("mailImage", screenShot);
                    string res = HttpPost(Properties.Settings.Default.serverAddress+ "/PhishRod-portlet/reporter", postParameters, htmlBody,attachmentDetails[1]);
                    return res + "\n";
                }
            }
        }
        catch (System.Exception ex)
        {
            
                log.Error(ex);
           
            return "Error: " + ex + "-- - " + ex.StackTrace.ToString() + "\n";
        }
        return "";
    }
c#
vsto
outlook-addin
asked on Stack Overflow Jul 14, 2020 by Muhammad Saleh • edited Jul 14, 2020 by James Z

1 Answer

0

In the event handler of your button I see the following lines of code:

Microsoft.Office.Interop.Outlook.Application olApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace ns = olApp.GetNamespace("MAPI");

There is no need to create a new Outlook Application instance in the add-in. Instead, you need to use the Application property which doesn't trigger a security issue when dealing with OOM from external applications:

var app = Globals.ThisAddIn.Application
answered on Stack Overflow Jul 14, 2020 by Eugene Astafiev

User contributions licensed under CC BY-SA 3.0