Sql – EWS – import email into sql database

emailexchange-serverexchangewebservicesimportsql

I've been trying to import the body from emails into a field in a SQL 2005 database but it keeps losing the formatting (carriage returns, tabs, etc)

Anyway of getting around this?

Best Solution

This is the code I used to strip an email body of the html formatting and all the characters so you are left with the actual email content. It looks like a mess, but it works:

foreach (Item item in findResults.Items)
            {
                MessageBody messageBody = new Microsoft.Exchange.WebServices.Data.MessageBody();
                List<Item> items = new List<Item>();
                if (findResults.Items.Count > 0) // Prevent the exception
                {
                    foreach (Item item2 in findResults)
                    {
                        items.Add(item2);
                    }
                }
                service.LoadPropertiesForItems(items, PropertySet.FirstClassProperties);
                messageBody = item.Body.ToString().Replace("<html dir=", "").Replace("<head>", "").Replace("<meta http-equiv=", "").Replace("content=", "")
                              .Replace("<style type=", "").Replace("</style>", "").Replace("</head>", "").Replace("<body fpstyle=", "").Replace("ocsi=", "")
                              .Replace("<div style=", "").Replace("direction: ltr;font-family: Tahoma;color: #000000;font-size: 10pt;", "").Replace("</div>", "")
                              .Replace("<div>", "").Replace("</body>", "").Replace("</html>", "").Replace("<br>", "").Replace(">", "").Replace("\"Content-Type", "")
                              .Replace("\"text/html; charset=utf-8", "").Replace("\"0", "").Replace("\"", "").Replace("text/css", "")
                              .Replace("id=owaParaStyle", "").Replace("ltr", "").Replace("<meta name=GENERATOR MSHTML 9.00.8112.16470", "").Replace("<style P {", "")
                              .Replace("MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px", "").Replace("<p", "").Replace("</p>", "").Replace("</p", "").Replace("&nbsp;", "")
                              .Replace("<body fPStyle= ", "").Replace("%", "").Replace("<", "").Replace(">", "").Replace("}", "").Replace("\"","").Replace("body fPStyle=1","");