【发布时间】:2016-07-13 14:49:53
【问题描述】:
无论如何如何将带有附件的整个 .msg 文件从 Outlook 保存到 SQL Server 数据库中?
我能够将 MailItem 对象发送到数据库中。
Outlook._Application _app = new Outlook.Application();
Outlook.NameSpace _ns = _app.GetNamespace("MAPI");
Outlook.MailItem item = _ns.GetItemFromID(selectedMailEntryId);
string sentOn = item.SentOn.ToLongDateString() + " " + item.SentOn.ToLongTimeString();
if (ServicesMail.InsertMail(txtSenderName.Text, txtSenderAddress.Text, item.Subject, txtQuestions.Text, txtAnswers.Text, item.HTMLBody, sentOn))
{
MessageBox.Show("Your Mail has been successfully saved in databse.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
这就是查询。
cmd.CommandText = @"INSERT INTO EmailStorage (fullName, emailAddress, questions, answers, htmlBody, sentOn, savedOn, subject)
VALUES(@fullName, @emailAddress, @questions, @answers, @htmlBody, @sentOn, @savedOn, @subject)";
cmd.Parameters.AddWithValue("@fullName", fullName);
cmd.Parameters.AddWithValue("@emailAddress", emailAddress);
cmd.Parameters.AddWithValue("@questions", questions);
cmd.Parameters.AddWithValue("@answers", answers);
cmd.Parameters.AddWithValue("@htmlBody", htmlBody);
cmd.Parameters.AddWithValue("@sentOn", sentOn);
cmd.Parameters.AddWithValue("@savedOn", DateTime.Now);
cmd.Parameters.AddWithValue("@subject", subject);
cmd.ExecuteNonQuery();
完美运行。但我需要存储整条消息,以便在需要时从数据库中取出并在 Outlook 中打开。
查看了很多资源,但只找到了如何在本地存储。谁能给个提示?
【问题讨论】:
-
您也许可以将 .msg 文件保存为 VarBinary,但您为什么要这样做?文件存储更适合存储...文件。
-
您可能还想看看这篇文章。当您使用 AddWithValue 传递这样的查询时,有时会得到错误的数据类型。 blogs.msmvps.com/jcoehoorn/blog/2014/05/12/…
标签: c# sql-server outlook