Dave Burke : Freelance .NET Web Developer specializing in Online Communities

Doing Email Testing in development without an Exchange Server

I mentioned my home office Exchange Server crash yesterday.  I'll be receiving a replacement machine from my fantastic System Admin at HQ next week, but in the meantime I wanted to continue working on the .Text comment notifier piece for my blog I recently started (before getting sidetracked by building a DNN Custom Module.)  Even though my office LAN consists of a single user (me), I use Exchange all the time in development.  This comment notifier function is just one example.

I have no Exchange server at the moment, so how do I test emails?  It dawned on me yesterday while looking at my dead server that since I'm simply spitting out HTML to view in Outlook, I can spit out HTML to view in a web page.  Heck, I might not even install Exchange for a while after getting the replacement machine, since this is so easy to implement.  It might also be a solution for other developers who don't have access to Exchange or a Windows POP Email Server on their network and need to test basic email functionality.

The email bypass requires a simple change to the original send code and replace the SMTPMAIL.SEND method with my own.  There are other ways to do this, but I simply changed the SMTPServer string in the .Text web.config Email Provider area (which is where .Text looks for it) to "DBVT.MailToFile."  Re-routing of the Send() method can then function independent of location.

MailMessage em = new MailMessage();
em.To = to;
em.From = from;
em.Subject = subject;
em.Body = message;
em.BodyFormat = blnHtmlFormat == true ? MailFormat.Html : MailFormat.Text;

SmtpMail.SmtpServer = this.SmtpServer;
if (SmtpMail.SmtpServer != "DBVT.MailToFile")
 SmtpMail.Send(em);
else
 DBVT.MailToFile.Send(em);
return true;

The spitting out of the HTML consists of these few lines of code. 

FileStream fs = new FileStream(HttpContext.Current.Server.MapPath
     ("/DBVT/dump/mail" + FileTimeString() + ".htm"), FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.Begin);
sw.Write(msg.Body + "\r\n");
sw.Close();

This creates a new HTML file in a directory I browse and select for viewing.  The resulting file looks exactly like the emails I was receiving before the crash, a sample shown in this post.

 

Comments (0) | Post RSS RSS comment feed

Posted on 9/25/2004 8:52:00 AM by Dave Burke
Categories: .NET
Tags:

Related posts


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke