none
System.ArgumentNullException in Parsing outlook e-mail body with html agility pack RRS feed

  • Question

  • Hi , 

    Please help me to solve this System.ArgumentNullException  on line : 

    Line 41 : HtmlAgilityPack.HtmlNode[] nodes = emailBody.DocumentNode.SelectNodes("//td").ToArray()

    I have the below stack trace of the exception but i do not know how to resolve this issue.

    at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
    at Test_HTML_Agility.Program.Main(String[] args) in C:\Users\daniel.tou\Documents\Visual Studio 2015\Projects\Test HTML Agility\Test HTML Agility\Program.cs:line 42
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
    at System.Threading.ExecutionContext.RunInternal(Exec utionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext,
    static void Main(string[] args)
            {
                Microsoft.Office.Interop.Outlook.Application app = null;
                Microsoft.Office.Interop.Outlook._NameSpace ns = null;
                Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
                Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
     
                Excel.Application oApp;
                Excel.Workbook oWB;
                Excel.Worksheet oSheet;
     
                app = new Microsoft.Office.Interop.Outlook.Application();
                ns = app.GetNamespace("MAPI");
                ns.Logon("daniel.tou@XXXXX", "XXXXXX", false, true);
                inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                subFolder = inboxFolder.Folders["Test"]; //folder.Folders[1]; also works
     
                oApp = new Excel.Application();
                oWB = oApp.Workbooks.Add();
                oSheet = (Excel.Worksheet)oWB.Worksheets.get_Item(1);
     
                MailItem mailItem = app.CreateItem(OlItemType.olMailItem);
                mailItem.HTMLBody = subFolder.ToString();
     
                HtmlAgilityPack.HtmlDocument emailBody = new HtmlAgilityPack.HtmlDocument();
                emailBody.LoadHtml(mailItem.HTMLBody);
     
                HtmlAgilityPack.HtmlNode[] nodes = emailBody.DocumentNode.SelectNodes("//td").ToArray();
     
                foreach (HtmlAgilityPack.HtmlNode item in nodes)
                {
                    Console.WriteLine(item.InnerHtml);
                }
    <div>
    <p><span style="font-family:&quot;Georgia&quot;,&quot;serif&quot;"><o:p>&nbsp;</o:p></span></p>
    <p><span style="font-size:12.0pt;font-family:&quot;Times New Roman&quot;,&quot;serif&quot;"><o:p>&nbsp;</o:p></span></p>
    <table border="0" cellspacing="0" cellpadding="0" width="719" style="width:539.25pt;border-collapse:collapse">
    <tbody>
    <tr style="height:20.25pt">
    <td width="719" nowrap="" colspan="3" valign="top" style="width:539.25pt;border:solid #4472C4 1.5pt;border-bottom:solid #BFBFBF 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:20.25pt">
    <p align="center" style="text-align:center"><b><span style="font-size:14.0pt;color:black">Detalii Comerciant</span></b><o:p></o:p></p>
    </td>
    <td width="0" style="width:.3pt;padding:0in 0in 0in 0in;height:20.25pt"></td>
    </tr>
    <tr style="height:16.5pt">
    <td width="335" nowrap="" valign="top" style="width:251.25pt;border:solid #4472C4 1.5pt;border-top:none;background:#F2F2F2;padding:0in 5.4pt 0in 5.4pt;height:16.5pt">
    <p><b><span style="font-size:10.0pt;color:black">Denumire Societate :</span></b><o:p></o:p></p>
    </td>
    <td width="384" nowrap="" colspan="2" valign="top" style="width:4.0in;border-top:none;border-left:none;border-bottom:solid #4472C4 1.5pt;border-right:solid #4472C4 1.5pt;background:#F2F2F2;padding:0in 5.4pt 0in 5.4pt;height:16.5pt">
    <p><span style="font-family:&quot;Georgia&quot;,&quot;serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LIVMAR SRL</span><o:p></o:p></p>
    </td>
    <td width="0" style="width:.3pt;padding:0in 0in 0in 0in;height:16.5pt"></td>
    </tr>
    <tr style="height:16.5pt">
    <td width="335" nowrap="" valign="top" style="width:251.25pt;border:solid #4472C4 1.5pt;border-top:none;padding:0in 5.4pt 0in 5.4pt;height:16.5pt">
    <p><b><span style="font-size:10.0pt;color:black">Denumire Comerciala ( numele magazinului ) :</span></b><o:p></o:p></p>
    </td>
    <td width="384" nowrap="" colspan="2" valign="top" style="width:4.0in;border-top:none;border-left:none;border-bottom:solid #4472C4 1.5pt;border-right:solid #4472C4 1.5pt;padding:0in 5.4pt 0in 5.4pt;height:16.5pt">
    <p align="center" style="text-align:center"><span style="color:black">&nbsp;</span><span style="font-family:&quot;Georgia&quot;,&quot;serif&quot;">LIVMAR</span><o:p></o:p></p>
    </td>
    <td width="0" style="width:.3pt;padding:0in 0in 0in 0in;height:16.5pt"></td>
    </tr>
    <tr style="height:16.5pt">
    <td width="335" nowrap="" valign="top" style="width:251.25pt;border:solid #4472C4 1.5pt;border-top:none;background:#F2F2F2;padding:0in 5.4pt 0in 5.4pt;height:16.5pt">
    <p><b><span style="font-size:10.0pt;color:black">Obiect de activitate ( ce vinde , nu descriere cod CAEN )</span></b><o:p></o:p></p>
    </td>
    <td width="384" nowrap="" colspan="2" valign="top" style="width:4.0in;border-top:none;border-left:none;border-bottom:solid #4472C4 1.5pt;border-right:solid #4472C4 1.5pt;background:#F2F2F2;padding:0in 5.4pt 0in 5.4pt;height:16.5pt">
    <div align="center">
    <table border="0" cellspacing="0" cellpadding="0">
    <tbody>
    <tr>


    • Moved by Sabah ShariqMVP Wednesday, March 1, 2017 12:29 PM Moved From Visual C#
    Sunday, February 26, 2017 6:32 PM

All replies

  • Seems that SelectNodes returns null, which means that there are no <td> elements.

    Have you checked if HTMLBody contains the expected string?

    In order to verify that SelectNodes works, you can perform an experiment:

      emailBody.LoadHtml( "<table><tr><td>TEXT</td></tr><table>" );

    Then the loop should work.

    Monday, February 27, 2017 7:33 AM
  • Select Nodes  works.

    The problem is that  mailItem.HTMLBody = subFolder.ToString(); is returning System.__ComObject

    then i tried :

    mailItem.HTMLBody = ((MailItem)inboxFolder.Folders["Test"].Items).HTMLBody; 

    and i get Unable to cast COM object of type ‘System.__ComObject’ 

    to interface type ‘Microsoft.Office.Interop.Outlook.MailItem’. 

    This operation failed because the QueryInterface call on the COM 

    component for the interface with IID ‘{00063034-0000-0000-C000-000000000046}’ 

    failed due to the following error: No such interface supported 

    (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

    How can i cast the COM object ? or in what way to resolve this to parse the emails

    using HtmlAgilityPack , MailItem and HTMLBody ?
    Tuesday, February 28, 2017 11:00 PM
  • Hello Danie,

    The HTMLBody property returns or sets a string representing the HTML body of the specified item. 

    When you try to assign it to the folder (path?) it gets a not a well formed HTML markup:

    mailItem.HTMLBody = subFolder.ToString();

    What are you trying to achieve writing these lines of code? What is your final goal?


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

    Wednesday, March 1, 2017 3:13 PM
  • I want to loop thru a folder in outlook, that contains over 1000 e-mails,  parse the emails to get the text,  usually is a table ,using html agility pack, then to write the text of each cell from the table to different cells in excel.


    Wednesday, March 1, 2017 6:42 PM
  • Please give me an example on how to resolve this , i`m stuck on that line.I don`t know how to get the html body of the e-mails in order to parse them with htmlagilitypack.

    Saturday, March 4, 2017 10:12 PM
  • Hi Danie,

    >> i`m stuck on that line.I don`t know how to get the html body of the e-mails in order to parse them with htmlagilitypack.

    For getting html body of e-mails in Folder, I suggest you try something like below:

      Microsoft.Office.Interop.Outlook.Application app = null;
                Microsoft.Office.Interop.Outlook._NameSpace ns = null;
                Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
                Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
                app = new Microsoft.Office.Interop.Outlook.Application();
                ns = app.GetNamespace("MAPI");
                ns.Logon("xxx", "xxx", false, true);
                inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                subFolder = inboxFolder.Folders["Thread"]; //folder.Folders[1]; also works
                foreach (MailItem item in subFolder.Items)
                {
                    System.Diagnostics.Debug.Print(item.HTMLBody);
                }

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 7, 2017 7:23 AM
  • I still get  System.ArgumentNullException  on the line :

    HtmlAgilityPack.HtmlNode[] nodes = emailBody.DocumentNode.SelectNodes("/table").ToArray();

    I don`t know what is wrong , please guide me.

       

       foreach ( MailItem item in subFolder.Items)
                {
                    
                    HtmlAgilityPack.HtmlDocument emailBody = new
    HtmlAgilityPack.HtmlDocument();
                    emailBody.LoadHtml(item.HTMLBody);
    
                    HtmlAgilityPack.HtmlNode[] nodes =
    emailBody.DocumentNode.SelectNodes ("/table").ToArray();
    
                    foreach (HtmlAgilityPack.HtmlNode items in nodes)
                    {
                        Console.WriteLine(items.InnerText);
                        Console.ReadLine();
                    }
    
    
                }

    Tuesday, March 7, 2017 11:12 PM
  • Hi Danie,

    If you add watch on item.HTMLBody, is it null or empty? Is there any table node in the HTMLBody of MailItem? I would suggest you output the item.HTMLBody to check.

    Sometimes, the table in mailitem is not converted to table node in HTMLBody. I suggest you try below code to get the table object.

                foreach (MailItem item in subFolder.Items)
                {
                    System.Diagnostics.Debug.Print(item.HTMLBody);
                    foreach (Microsoft.Office.Interop.Word.Table table in item.GetInspector.WordEditor.Tables)
                    {
                        for (int r = 1; r <=table.Rows.Count; r++)
                        {
                            for (int c = 1; c <=table.Columns.Count; c++)
                            {
                                System.Diagnostics.Debug.Print(table.Cell(r,c).Range.Text);
                            }
                        }
                    }
                }

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 8, 2017 5:16 AM