<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>dev{shaped} &#187; smtp</title>
	<atom:link href="http://devshaped.com/tag/smtp/feed/" rel="self" type="application/rss+xml" />
	<link>http://devshaped.com</link>
	<description></description>
	<lastBuildDate>Mon, 13 Jul 2009 14:47:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<!-- podcast_generator="podPress/8.8" -->
		<copyright>&#xA9; </copyright>
		<managingEditor>derek@webradius.com ()</managingEditor>
		<webMaster>derek@webradius.com()</webMaster>
		<category></category>
		<itunes:keywords></itunes:keywords>
		<itunes:subtitle></itunes:subtitle>
		<itunes:summary></itunes:summary>
		<itunes:author></itunes:author>
		<itunes:category text="Society &amp; Culture"/>
		<itunes:owner>
			<itunes:name></itunes:name>
			<itunes:email>derek@webradius.com</itunes:email>
		</itunes:owner>
		<itunes:block>No</itunes:block>
		<itunes:explicit>no</itunes:explicit>
		<itunes:image href="http://devshaped.com/wp-content/plugins/podpress/images/powered_by_podpress_large.jpg" />
		<image>
			<url>http://devshaped.com/wp-content/plugins/podpress/images/powered_by_podpress.jpg</url>
			<title>dev{shaped}</title>
			<link>http://devshaped.com</link>
			<width>144</width>
			<height>144</height>
		</image>
		<item>
		<title>Practical Programming: Sending Email</title>
		<link>http://devshaped.com/2009/01/practical-programming-sending-email/</link>
		<comments>http://devshaped.com/2009/01/practical-programming-sending-email/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 01:33:00 +0000</pubDate>
		<dc:creator>Derek</dc:creator>
				<category><![CDATA[Practical Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://devshaped.com/2009/01/practical-programming-sending-email/</guid>
		<description><![CDATA[By Derek Hatchard
 Sending email from a .NET application is incredibly simple:
using System.Net.Mail;     &#8230;     SmtpClient _smtp = new SmtpClient(&#34;smtp.test.com&#34;);     _smtp.Send(&#34;from@test.com&#34;,&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;derek@devshaped.com&#34;,&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;Subject&#34;,&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;Body&#34;); 
With just these few lines of code, you are sending an email from from@test.com to derek@devshaped.com. Pretty [...]]]></description>
			<content:encoded><![CDATA[<p>By <a href="http://derekh.com/" target="_blank">Derek Hatchard</a></p>
<p><img style="border-right-width: 0px; margin: 0px 0px 22px 22px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Derek_Hatchard" border="0" alt="Derek_Hatchard" align="right" src="http://devshaped.com/wp-content/uploads/2009/01/derek-hatchard.jpg" width="85" height="57" /> Sending email from a .NET application is incredibly simple:</p>
<div style="font-family: courier new"><span style="color: blue">using</span> System.Net.Mail;     <br />&#8230;     <br />SmtpClient _smtp = <span style="color: blue">new</span> SmtpClient(<span style="color: maroon">&quot;smtp.test.com&quot;</span>);     <br />_smtp.Send(<span style="color: maroon">&quot;from@test.com&quot;</span>,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;derek@devshaped.com&quot;</span>,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Subject&quot;</span>,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Body&quot;</span>); </div>
<p>With just these few lines of code, you are sending an email from from@test.com to derek@devshaped.com. Pretty easy stuff.</p>
<p>But what if you need something a little more complicated? The example above assumes the standard SMTP port for email with no authentication and an unencrypted connection. Fortunately these “complications” are quite easy to deal with in .NET.</p>
<p> <span id="more-31"></span><br />
<h2>Different Port</h2>
<p>If you need to send email on a different port, simply add the following line:</p>
<div style="font-family: courier new">_smtp.Port = <span style="color: maroon">587</span>;</div>
<h2>SMTP Authentication</h2>
<p>If you are not sending through a local SMTP server, your app will likely need to authenticate itself. If you need to pass credentials to the SMTP server, use the Credentials property on the SmtpClient class:</p>
<div style="font-family: courier new"><span style="color: blue">using</span> System.Net;     <br />&#8230;     <br />_smtp.Credentials = <span style="color: blue">new</span> NetworkCredential(<span style="color: maroon">&quot;username&quot;</span>, <span style="color: maroon">&quot;pwd&quot;</span>);</div>
<h2>Encrypted Connection</h2>
<p>Since you are passing credentials to the server, you really should be using a secure connection (many mail providers <em>require</em> you to use a secure connection). In code, it’s just one line:</p>
<div style="font-family: courier new">_smtp.EnableSsl = <span style="color: maroon">true</span>;     </div>
<h2>Sending Email On Behalf of Someone</h2>
<p>A final practical consideration when sending email is the distinction between From and Sender. In a simple scenario, you only need a From address. But if you are writing code to send email, chances are good that there is something not-quite-so-simple happening. </p>
<p>Perhaps you have received an email with a header like this in Outlook:</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="fromsender" border="0" alt="fromsender" src="http://devshaped.com/wp-content/uploads/2009/01/fromsender.jpg" width="382" height="88" /> </p>
<p>This is an example of one email account sending an email on behalf of someone else. This is often used by automated systems and it is trivial to add to your application. To add a Sender you cannot pass values directly to an SmtpClient object; you must create a new instance of the MailMessage class, set the properties, and then pass the MailMessage object to the SmtpClient object:</p>
<div style="font-family: courier new">MailMessage _msg = <span style="color: blue">new</span> MailMessage(<span style="color: maroon">&quot;from@test.com&quot;</span>,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;derek@devshaped.com&quot;</span>,&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Subject&quot;</span>,&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Body&quot;</span>);&#160; <br />_msg.Sender = <span style="color: blue">new</span> MailAddress(<span style="color: maroon">&quot;sender@test.com&quot;</span>);</div>
<div style="font-family: courier new">_smtp.Send(_msg); </div>
<p>Keep in mind that your message is going through an SMTP server that might not let you set arbitrary From and Sender values.</p>
<h2>Bringing it All Together</h2>
<p>Here is what the final code looks like that incorporates an alternate port, credentials, encrypted transport, and a sender:</p>
<div style="font-family: courier new"><span style="color: blue">using</span> System.Net;     <br /><span style="color: blue">using</span> System.Net.Mail;     <br />&#8230;     <br />SmtpClient _smtp = <span style="color: blue">new</span> SmtpClient(<span style="color: maroon">&quot;smtp.test.com&quot;</span>);     <br />_smtp.Port = <span style="color: maroon">587</span>;     <br />_smtp.Credentials = <span style="color: blue">new</span> NetworkCredential(<span style="color: maroon">&quot;username&quot;</span>, <span style="color: maroon">&quot;pwd&quot;</span>);     <br />_smtp.EnableSsl = <span style="color: maroon">true</span>;     </p>
<p>MailMessage _msg = <span style="color: blue">new</span> MailMessage(<span style="color: maroon">&quot;from@test.com&quot;</span>,     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;derek@devshaped.com&quot;</span>,     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Subject&quot;</span>,     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: maroon">&quot;Body&quot;</span>);     <br />_msg.Sender = <span style="color: blue">new</span> MailAddress(<span style="color: maroon">&quot;sender@test.com&quot;</span>);     <br />_smtp.Send(_msg); </div>
</p>
<p>&#8212;-</p>
<p>Derek Hatchard is the content editor for <a href="http://microsoft.com/youshapeit/msdn">http://microsoft.com/youshapeit/msdn</a> and <a href="http://devshaped.com">http://devshaped.com</a>. He is also the founder of <a href="http://crowdspace.net/" target="_blank">Crowd Space</a>. You can find him online at <a href="http://derekh.com">http://derekh.com</a>, <a href="http://ardentdev.com">http://ardentdev.com</a>, and <a href="http://twitter.com/derekhat">http://twitter.com/derekhat</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://devshaped.com/2009/01/practical-programming-sending-email/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
