Is there anyway how to save whole .msg file with attachments from Outlook into a SQL Server database?
I am able to send MailItem objects into database.
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);
}
and this is the query.
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();
It works perfectly. But I need to store whole message, so I can take it from database when needed and open it in Outlook.
Looked at many resources, but only found how to store it locally. Can anyone give me a hint?