I have a requirement to save MailMessage objects (system.net.mail) to a table in a SQL Server 2005 database, with .Net 2.
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.
This works and creates my MailMessage object and it's able to be sent no worries.
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 <p> contents of the logo with a new HTML string header to contain the cid reference for the logo
string wsHdr = "<html><body><p><a href=\"http://www.tecnq.com.au\" target=\"_blank\"><img alt=\"tecnqlogo\" src=\"cid:" + contentID + "\" style=\"border-style:none; height:34px; width:100px\" /></a></p>";
//then place back the rest of the email
string wsBody = wsHdr + " " + wsMSG.Substring(wsMSG.IndexOf("<hr />")) + "</html></body>";
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;
}
}
I have read this and this, but all of these assume you're wanting to save the MailMessage to a file on disk. That is not the case for me.
I've also read this, which implies I should serialise
the message. But I think the MailMessage is a core object that cannot be serialised, according to Microsoft. Is that correct?
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?
UPDATE
This 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.
MailMessage
as single BLOB or other,is saving it's properties an option that can later be reconstructed? – Searching Nov 16 '16 at 3:57byte[]
first and save as VARBINARY(MAX)MyTable.Attachment
, and I guess other details are up to you. I could post an answer.. – Searching Nov 16 '16 at 4:06