Parsing outlook e-mail body with html agility pack

Daniel Tou

New member
Joined
Jan 15, 2017
Messages
4
Programming Experience
Beginner
Hi ,

Please help me with the exception i get :
"An unhandled exception of type 'System.ArgumentNullException' occurred in System.Core.dll

Additional information: Value cannot be null."
And tell me if i am on the wright track to parse the email body.
Thank you !



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);
            }
 
Last edited by a moderator:
That's the offending line in your code. What's the actual method that throws the exception? Take a look at the stack trace of the exception and it will show you what methods were currently executing when the exception was thrown. At a guess, I'd say that SelectedNodes is returning Nothing and thus the first argument to the ToArray extension method is Nothing. The stack trace is there so we don't have to guess though.
 
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.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
 
mailItem.HTMLBody = subFolder.ToString();
Doesn't this just return the folder name?
 
I knew something was wrong there , i`m new to c# , trying to learn when i have time.What should i put there ? mailItem.HTMLBody = ?



 
Back
Top Bottom