title: How to save System.Net.Mail MailMessage object into SQL Server database?
body: <p>I have a requirement to save MailMessage objects (system.net.mail) to a table in a SQL Server 2005 database, with .Net 2.</p>
  
  <p>The mail object also contains a single image attachment which is part of the mail message body as a logo on the top of the message.</p>
  
  <p>This works and creates my MailMessage object and it's able to be sent no worries.</p>
  
  <pre><code>   private bool SendEMAIL(string wsStudent, string wsNOKemails, string wsMSG)
      {
          try
          {
                  MailMessage message = new MailMessage();
                  message.To.Clear();
                  string[] wsEmails = wsNOKemails.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                  foreach (string address in wsEmails)
                  {
                      MailAddress to = new MailAddress(address);
                      message.To.Add(to);
                  }
  
                  //get staff email as the FROM address
                  DataSourceSelectArguments dss = new DataSourceSelectArguments();
                  sdsStaffDetails.SelectParameters["StaffID"].DefaultValue = Session["StaffID"].ToString();
                  DataView dv = sdsStaffDetails.Select(dss) as DataView;
                  DataTable dt = dv.ToTable() as DataTable;
                  if (dt != null)
                  {
                      foreach (DataRow dr in dt.Rows) //this loop will only happen once
                      {
                          message.From = new MailAddress(dr.ItemArray[2].ToString());    //TET's staffer email address
                          message.Subject = "Tec-NQ - TET Report for " + wsStudent;
                          message.Priority = MailPriority.High;
                          message.IsBodyHtml = true;
  
                          //add the logo attachment
                          MemoryStream ms = new MemoryStream(File.ReadAllBytes(Server.MapPath("../images/Tec-NQ-Logo-BlueOrange-60x174.png")));
                          Attachment logo = new Attachment(ms, "logo");
                          message.Attachments.Add(logo);
  
                          //..but create a Base64 version of it, as we'll need to embedd this into the message so it knows
                          //where to place the logo into the email
                          string contentID = "Image";
                          logo.ContentId = contentID;
                          logo.ContentDisposition.Inline = true;
                          logo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
  
                          //replace the &lt;p&gt; contents of the logo with a new HTML string header to contain the cid reference for the logo
                          string wsHdr = "&lt;html&gt;&lt;body&gt;&lt;p&gt;&lt;a href=\"http://www.tecnq.com.au\" target=\"_blank\"&gt;&lt;img alt=\"tecnqlogo\" src=\"cid:" + contentID + "\" style=\"border-style:none; height:34px; width:100px\" /&gt;&lt;/a&gt;&lt;/p&gt;";
  
                          //then place back the rest of the email
                          string wsBody = wsHdr + " " + wsMSG.Substring(wsMSG.IndexOf("&lt;hr /&gt;")) + "&lt;/html&gt;&lt;/body&gt;";
                          message.Body = wsBody;
  
                          //send it!
                          SmtpClient SmtpClient = new SmtpClient();
                          SmtpClient.Send(message);
                      }
                      return true;
              }
              else
                  return false;
          }
          catch (Exception msg)
          {
              StatusMessage(msg.ToString());
              return false;
          }
      }
  </code></pre>
  
  <p>I have read <a href="https://forums.asp.net/t/1765853.aspx?How%20to%20store%20complex%20NET%20non%20POCO%20class%20types%20to%20database%20" rel="nofollow noreferrer">this</a> and <a href="https://stackoverflow.com/questions/1264672/how-to-save-mailmessage-object-to-disk-as-eml-or-msg-file">this</a>, but all of these assume you're wanting to save the MailMessage to a file on disk. That is not the case for me.</p>
  
  <p>I've also read <a href="https://stackoverflow.com/questions/18745392/elegant-way-to-serialize-a-mailmessage-object-in-net">this</a>, which implies I should <code>serialise</code> the message. But I think the MailMessage is a core object that cannot be serialised, according to Microsoft. Is that correct?</p>
  
  <p>So any ideas on how I can save the above as a BLOB or some data type, so all I have to do later on, is open the table, read the object and SEND it like a normal email?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p><a href="http://www.codeproject.com/Articles/32434/Adding-Save-functionality-to-System-Net-Mail-MailM" rel="nofollow noreferrer">This</a> CodeProject article is a way forward, that I just found. However, it's to specify a filename when saving the MailMessage object to disk. That's a start because I can then specifiy some field delimiters in the filename to specify what files to use later on, in terms of reading the EML file and later emailing the object.</p>