<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
  <channel>
    <title><![CDATA[[SecurityRatty] tag: eml]]></title>
    <link>http://securityratty.com/tag/eml</link>
    <description></description>
    <pubDate>Fri, 01 Aug 2008 09:59:01 +0000</pubDate>
    <generator>iRatty Engine</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <item>
      <title><![CDATA[Simulating Email in .NET]]></title>
      <link>http://securityratty.com/article/0c454dbe28b5b63d07ee0089e019de77</link>
      <guid>http://securityratty.com/article/0c454dbe28b5b63d07ee0089e019de77</guid>
      <description><![CDATA[I use email as a notification mechanism a lot, and often in class I'll demo sending email via a technique that I use frequently when developing code. It allows you to simulate sending an email...]]></description>
      <content:encoded><![CDATA[<p>I use email as a notification mechanism a lot, and often in class I&#39;ll demo sending email via a technique that I use frequently when developing code. It allows you to simulate sending an email message.</p> <p>The trick to doing this is not to hardcode things like host, port, etc. for your SMTP server when you use System.Net.Mail to send mail. Instead, use the default ctor for <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx" target="_blank">SmtpClient</a> as I&#39;ve done in the code below.</p> <blockquote><pre class="csharpcode"><span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)
{
    <span class="rem">// note the use of the MailAddress class</span>
    <span class="rem">// this allows me to specify display names as well as email addresses</span>
    MailAddress from = <span class="kwrd">new</span> MailAddress(<span class="str">&quot;admin@fabrikam.com&quot;</span>, <span class="str">&quot;Fabrikam Website&quot;</span>);
    MailAddress to = <span class="kwrd">new</span> MailAddress(<span class="str">&quot;mari@fabrikam.com&quot;</span>, <span class="str">&quot;Mari Joyce&quot;</span>);

    MailMessage msg = <span class="kwrd">new</span> MailMessage(from, to);
    msg.Subject  = <span class="str">&quot;Testing 123&quot;</span>;
    msg.Body = <span class="str">&quot;This is only a test!&quot;</span>;

    <span class="rem">// note use of default ctor</span>
    <span class="rem">// this looks in config to figure out how to send mail</span>
    <span class="kwrd">new</span> SmtpClient().Send(msg);
}</pre></blockquote>
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }


<p>What you&#39;re telling .NET by using the default ctor for SmtpClient is, &quot;please use my config file to figure out how to send mail&quot;. Now you can use the system.net/mailSettings/smtp section in config to specify the details of your mail server, and all of the code in your app that is written to use the default SmtpClient ctor will inherit these settings. Here&#39;s an example of what the config on a production server might look like (if you put passwords in your config files, be sure to <a href="http://msdn.microsoft.com/en-us/library/ms998283.aspx" target="_blank">encrypt those sections</a>): </p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">configuration</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">system.net</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">mailSettings</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">smtp</span> <span class="attr">deliveryMethod</span><span class="kwrd">=&quot;Network&quot;</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">network</span> <span class="attr">host</span><span class="kwrd">=&quot;mail.fabrikam.com&quot;</span>
                 <span class="attr">port</span><span class="kwrd">=&quot;25&quot;</span>
                 <span class="attr">userName</span><span class="kwrd">=&quot;WebsiteMailAccount&quot;</span>
                 <span class="attr">password</span><span class="kwrd">=&quot;whatever&quot;</span><span class="kwrd">/&gt;</span>
      <span class="kwrd">&lt;/</span><span class="html">smtp</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">mailSettings</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">system.net</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">configuration</span><span class="kwrd">&gt;</span></pre><pre class="csharpcode">&nbsp;</pre>
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }


<p>During development, I use different settings because I don&#39;t usually want to deal with the hassle of installing an SMTP server on my development box. Instead, I want email messages delivered as individual files in a directory on my hard drive (I always have a c:\mail directory on my development box for just this purpose):</p>
<blockquote><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">configuration</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">system.net</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">mailSettings</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">smtp</span> <span class="attr">deliveryMethod</span><span class="kwrd">=&quot;SpecifiedPickupDirectory&quot;</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">specifiedPickupDirectory</span> <span class="attr">pickupDirectoryLocation</span><span class="kwrd">=&quot;c:\mail&quot;</span><span class="kwrd">/&gt;</span>
      <span class="kwrd">&lt;/</span><span class="html">smtp</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">mailSettings</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">system.net</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">configuration</span><span class="kwrd">&gt;</span></pre></blockquote>
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }


<p>Now when I run the program above, I get a .EML file in my c:\mail directory:</p>
<p><a href="http://www.pluralsight.com/community/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/keith/image_5F00_2.png"><img style="border-right:0px;border-top:0px;margin:0px 0px 0px 35px;border-left:0px;border-bottom:0px;" height="230" alt="image" src="http://www.pluralsight.com/community/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/keith/image_5F00_thumb.png" width="404" border="0" /></a> </p>
<p>Outlook Express is normally registered as the viewer for .EML files, so double-click the file to view it:</p>
<p><a href="http://www.pluralsight.com/community/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/keith/image_5F00_4.png"><img style="border-right:0px;border-top:0px;margin:0px 0px 0px 35px;border-left:0px;border-bottom:0px;" height="287" alt="image" src="http://www.pluralsight.com/community/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/keith/image_5F00_thumb_5F00_1.png" width="292" border="0" /></a> </p>
<p>If you&#39;ve never seen this method of simulating email before, I hope you find it as useful as I have. Happy coding!</p><div style="clear:both;"></div><img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=52305" width="1" height="1">]]></content:encoded>
      <pubDate>Fri, 01 Aug 2008 09:59:01 +0000</pubDate>
      <category domain="http://securityratty.com/tag/csharpcode pre">csharpcode pre</category>
      <category domain="http://securityratty.com/tag/pre">pre</category>
      <category domain="http://securityratty.com/tag/csharpcode">csharpcode</category>
      <category domain="http://securityratty.com/tag/color">color</category>
      <category domain="http://securityratty.com/tag/email">email</category>
      <category domain="http://securityratty.com/tag/email addresses mailaddress">email addresses mailaddress</category>
      <category domain="http://securityratty.com/tag/mailaddress">mailaddress</category>
      <category domain="http://securityratty.com/tag/mail server">mail server</category>
      <category domain="http://securityratty.com/tag/mail">mail</category>
      <source url="http://www.pluralsight.com/community/blogs/keith/archive/2008/08/01/simulating-email-in-net.aspx">Simulating Email in .NET</source>
    </item>
  </channel>
</rss>
