I talked about my home office Exchange server crashing in a September post and how I came up with an approach to test HTML-based email components without requiring an Exchange Server. I needed to re-create this functionality on a different project today since I still haven't re-installed Exchange (and don't want to until I absolutely have to!) The result is that HTML emails are sent to web folder for viewing in IE rather than sent to Exchange for viewing in Outlook.
This is a snap when you've implemented a single Email component which is used for your entire application. If that's the case, the lines where (if you
did have an exchange server) you previously set the local SmtpMail.SmtpServer property you now send out the message to your need MailToFile() class.
if (ConfigurationSettings.AppSettings["Environment].ToString().IndexOf("dev") > 0)
{
imsii.Utils.MailToFile.Send(msg);
}
else
{
SmtpMail.SmtpServer = "";
SmtpMail.Send(msg);
}
The small MailToFile.cs class is located here, with the essence of the thing being to create a filestream to write the email message to file.
string s = msg.To.Substring(1, msg.To.IndexOf("@") -1);
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("/dump/mail/" +
FileTimeString() + s + ".htm"), FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.Begin);
sw.Write(msg.Body + "\r\n");
sw.WriteLine("<ul><b>TO:</b> " + msg.To + "<br>");
sw.WriteLine("<b>Subject:</b> " + msg.Subject + "</ul><p></p>");
sw.Close();
return true;
With folder browsing enabled, an IE window lists the output files for viewing.