EWS - 将电子邮件导入 SQL 数据库

发布于 2024-07-22 03:38:11 字数 83 浏览 6 评论 0原文

我一直在尝试将电子邮件正文导入到 SQL 2005 数据库中的字段中,但它不断丢失格式(回车符、制表符等)。

无论如何要解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流年里的时光 2024-07-29 03:38:11

这是我用来去除电子邮件正文中的 html 格式和所有字符的代码,以便您留下实际的电子邮件内容。 它看起来很乱,但它有效:

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(" ", "")
                              .Replace("<body fPStyle= ", "").Replace("%", "").Replace("<", "").Replace(">", "").Replace("}", "").Replace("\"","").Replace("body fPStyle=1","");

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(" ", "")
                              .Replace("<body fPStyle= ", "").Replace("%", "").Replace("<", "").Replace(">", "").Replace("}", "").Replace("\"","").Replace("body fPStyle=1","");
棒棒糖 2024-07-29 03:38:11

如果您的 SQL 字段是哪种数据类型? 如果您使用“文本”字段(nvarchar 或类似字段),那么这可能就是原因。

如果您确实需要完整的电子邮件正文,那么您可能需要将其存储为 blob(二进制)字段。 这会产生影响(您应该阅读有关在 SQL 中存储二进制对象的信息),但它允许您存储消息正文。

想必您有充分的理由为什么这些消息需要存储在 SQL 中,而不是存储在外部(例如文件系统上),而仅使用指向 SQL 中消息的指针?

What data type if your field in SQL? If you're using a "text" field (nvarchar or similar) then this is likely to be the cause.

If you really need the e-mail body intact then you'll probably need to store it as a blob (binary) field. This will have implications (you should read up about storing binary object in SQL) but it will allow you to store the body of the message.

Presumably you have some good reason why these messages need to be stored IN SQL and not outside (on the file system for example) with only pointers to the messages in SQL?

時窥 2024-07-29 03:38:11

我已经设法找到问题所在。 我使用的数据集将值作为字符串传递。 我已更改为 char 并且所有格式都被保留。 非常感谢你的帮助。

I've managed to find the problem. The dataset I was using was passing the values though as a string. I've changed to char and all formats are preserved. Thanks so much for you help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文