簡體   English   中英

C#將整個Outlook .msg文件保存在SQL Server數據庫中

[英]C# saving whole outlook .msg file in SQL Server database

無論如何,如何將帶有附件的整個.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中打開它。

查看了許多資源,但只發現了如何在本地存儲它。 誰能給我一個提示?

當然,請調用MailItem.SaveAs(..., olMsg)將其另存為本地文件,然后將文件數據讀取到緩沖區或流中並設置二進制字段的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM