Testing Email Sending
Recently I learned a couple of interesting things related to sending emails. One doesn’t relate to .NET at all, so if you’re a developer and you want to easily be able to test whether or not emails are working correctly in your application without actually sending them and/or installing a real SMTP server on your machine, pay attention. You can grab the smtp4dev application from codeplex, which will listen for SMTP emails and log them for you (and will even pop-up from the system tray to notify you when it receives them) – but it will never send them. Here’s what the notification looks like:
The other interesting tidbit about emails that is specific to .NET is that the MailMessage class is IDisposable. I never realized that before, but I noticed it while reading through the Ben Watson’s C# 4 How To book I picked up at TechEd last week (which is done cookbook style and has a ton of useful info in it). Looking at the MailMessage’s Dispose() method in Reflector reveals that it only really matters if you’re working with AlternateViews or Attachments (this.bodyView is also of type AlternateView):
All the same, you should get in the habit of wrapping your calls to MailMessage in a using() { … } block. Here’s some sample code showing how I would typically work with MailMessage:
Bad Example (don’t cut and paste and use):
string fromAddress = "nobody@somewhere.com";
string toAddress = "somebody@somewhere.com";
string subject = "Test Message from IDisposable";
string body = "This is the body.";
var myMessage = new MailMessage(fromAddress, toAddress, subject, body);
var myClient = new SmtpClient("localhost");
myClient.Send(myMessage);
And here’s how that code would look once it’s properly set up using IDisposable / using:
Good Example (use this one!):
string fromAddress = "nobody@somewhere.com";
string toAddress = "somebody@somewhere.com";
string subject = "Test Message from IDisposable";
string body = "This is the body.";
using(var myMessage = new MailMessage(fromAddress, toAddress, subject, body))
using(var myClient = new SmtpClient("localhost"))
{
myClient.Send(myMessage);
}
Note also that SmtpClient is also IDisposable, and that you can stack your using() statements without introducing extra { … } when you have instances where you need to work with several Disposable objects (like in this case).
Note that if you run Code Analysis in VS2010 (not sure which version you have to have – right click on a project in your solution explorer and look for Analyze Code) you will get warnings for any IDisposable classes you’re using outside of using statements, like these:
Summary
Sometimes things you don’t expect to be Disposable are. It’s good to run some code analysis tools from time to time to help find things like this. Visual studio has some nice static analysis built into some versions, or you can use less expensive tools like Fx Cop or Nitriq.
6 Comments
John Sheehan said
Here's another easy way to test email sending without having to run a fake server: <a target="_blank" href="http://stackoverflow.com/questions/54929/hidden-features-of-asp-net/75170#75170">stackoverflow.com/.../75170</a>
ssmith said
@John,
Nice, although I have to say I like the pop-up notifications that smtp4dev offers. But if I don't have that installed, I'll definitely have to remember the config trick.
Greg Law said
Just keep in mind that SmtpClient in releases prior to 4.0 do not implement IDisposable.
Greg Law said
Just keep in mind that SmtpClient in releases prior to 4.0 do not implement IDisposable.
Great Uses of Using Statement in C# : Steve Smith's Blog said
Pingback from Great Uses of Using Statement in C# : Steve Smith's Blog
yusuf derici said
very nıce.thank you.but my englısh is thn,not strong.so ıcant understand or ı don'n read.ı am writing from celikhan/adıyaman/turkey.ı am trying to send e-mail but ıcan't.ı wıl tasting alot of.thank you.by.