I have a requirement where i need to save complete mailmessage object into database. Using below code i am able to save message body and single attachment into database but i can have n number of attachment in my email so how to handle such situation.
private static void SaveMessage(MailMessage message, byte[] arr)
{
using (var conn = new SqlConnection("CONNECTION_STRING"))
{
using (var cmd = new SqlCommand("INSERT INTO MailSent (Body,Attachment) VALUES(@BODY,@ATTACHMENT)", conn))
{
conn.Open();
var param = new SqlParameter("@BODY", SqlDbType.VarChar)
{
Value = message.Body
};
var param2 = new SqlParameter("@ATTACHMENT", SqlDbType.Binary)
{
Value = arr
};
cmd.Parameters.Add(param);
cmd.Parameters.Add(param2);
cmd.ExecuteNonQuery();
}
}
}
Can we have any solution where we can serilize complete mailmessage along with their attachment and then deserlize it again and retrive all information