Saturday 6 December 2014

C#/ASP.NET - Testing Email from localhost without SMTP Server


A lot of the time when developing apps, you need to send emails from your application. Developers need a way to test this and sometimes setting up a SMTP server is a little overkill. Thankfully, the .NET framework has a built in way for you to set this up so you can send the emails to your hard drive as opposed to a SMTP server.

Simply add the following code to your web.config or app.config...

Code Snippet
  1. <system.net>
  2. <mailSettings>
  3. <smtp deliveryMethod="SpecifiedPickupDirectory">
  4. <specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\" />
  5. </smtp>
  6. </mailSettings>
  7. </system.net>
End of Code Snippet


Usage:
Code Snippet
  1. System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("fromEmail@email.com",
  2. "toEmail@email.com", "Test", "Test Body");
  3. System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
  4. client.Send(mail);
End of Code Snippet

No comments: