Frage

I'm bit confused in parsing outlook email body message. The sender seemingly sending the messages in HTML, but when I try to parse the HTML using HTMLAgilityPack I get NullExceptionError.

Here's the message that I obtain from Outlook using "view source".

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head>
<body style="font-family: arial; font-size: 10pt;">
<br><br>
<table border="1" width="100%">
<tr>
<td width="100%" style="font-weight: bold;" bgcolor="#0066CC" colspan="2">
<font color="white">
<p align="center"><b>
MAIL2FAX DETAILED DELIVERY REPORT

</b></td>
</font>
<tr bgcolor="#99CCCC">
<td width="30%" style="font-weight: bold;">Attention</td>
<td width="70%"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="30%" style="font-weight: bold;">Job Number</td>
<td width="70%">48762955</td>
</tr>
<tr bgcolor="#99CCCC">
<td width="30%" style="font-weight: bold;">Sent By User                    </td>
<td width="70%"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="30%" style="font-weight: bold;">Entered Fax2Mail System         </td>
<td width="70%">03/28 13:02</td>
</tr>
<tr bgcolor="#99CCCC">
<td width="30%" style="font-weight: bold;">Report Generated</td>
<td width="70%">03/28 13:33</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="30%" style="font-weight: bold;">Subject</td>
<td width="70%"> Requests: 1</td>
</tr>
<tr bgcolor="#99CCCC">
<td width="30%" style="font-weight: bold;">Page Count</td>
<td width="70%">
2&nbsp;(including cover sheet)
</td>
</tr>
</table>
<br><br>
<table border="1" width="50%">
<tr bgcolor="#0066CC">
    <td width="100%" colspan="3">
    <p align="center" style="font-weight: bold;"><font color="white">SUMMARY</td>
</tr>
<tr bgcolor="#99CCCC">
<td width="33%" style="font-weight: bold;"><p align="center">Sent: </b>      0</td>
<td width="33%" style="font-weight: bold;"><p align="center">Errors: </b>      1</td>
<td width="34%" style="font-weight: bold;"><p align="center">Cancelled: </b>      0</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="33%" style="font-weight: bold;"><p align="center">Total: </b>      1</td>
<td width="67%" colspan="2">&nbsp;</td>
</tr>
</table>
<br><br>
<table border="1" width="100%">
<tr style="font-weight: bold;" bgcolor="#0066CC">
<td width="40%"><font color="white">Destination</td>
<td width="15%"><font color="white">Status</td>
<td width="15%"><font color="white">Date</td>
<td width="15%"><font color="white">Time</td>
<td width="15%"><font color="white">Num. Retries</td>
</tr>
</font>
<tr bgcolor="#99CCCC">
<td width="40%"><p align="left">1111111111                      </td>
<td width="15%"><p align="left">ERR </td>
<td width="15%"><p align="left">03/28</td>
<td width="15%"><p align="left">13:33</td>
<td width="15%"><p align="right"> 4</td>
</tr>
</table>
</body>
</html>

And here's my code

Outlook.MailItem faxReport = Item as Outlook.MailItem;

        (faxReport.Subject.IndexOf("Uploaded") == -1)
            {
                HtmlAgilityPack.HtmlDocument faxDoc = new HtmlAgilityPack.HtmlDocument();
                faxDoc.LoadHtml(faxReport.Body);

                HtmlNode tableNode = faxDoc.DocumentNode.SelectSingleNode("//table");

                foreach (HtmlNode row in tableNode.SelectNodes("tr") )
                {
                    HtmlNodeCollection cells = row.SelectNodes("th|tr");
                    MessageBox.Show("row and cells found");

                    foreach(HtmlNode cell in cells)
                    {
                        MessageBox.Show(cell.InnerText);
                    }


                }

}

I'm not sure whether it's giving me this error message cause faxReport.Body is string type and doesn't show html tags, or is it cause I'm doing something wrong? How can I fix this?

Thanks for your help.

War es hilfreich?

Lösung

You should be using MailItem.HTMLBody

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top