December 25, 2012

Avoid creating your own SMTP settings

The <mailSettings> element has been around for years since the .NET Framework 2.0, but I still found people tend to create their own SMTP settings in the <appSettings> element like below.

<add key="SmtpServer" value="smtp.pete.tw"/>
<add key="SmtpUsername" value="pete"/>
<add key="SmtpPassword" value="F4g%j&k9"/>
<add key="SmtpPort" value="25"/>
<add key="SmtpEnableSSL" value="False"/>

Well, I know some are legacy projects you don't even want to touch, jsut like the famous phrase says - if it ain't broken, don't fix it. But for new projects, we should definitely use <mailSettings> for writing neat code and not messing up the <appSettings> element. Refer to http://msdn.microsoft.com/en-us/library/w355a94k.aspx and have your own configuration as below, the SmtpClient class in your code will read the <mailSettings> element automatically from your App.config/Web.config.

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network" from="Pete&lt;admin@pete.tw&gt;">
      <network defaultCredentials="true" host="smtp.pete.tw" port="25" enableSsl="false" userName="pete" password="F4g%j&k9" />
    </smtp>
  </mailSettings>
</system.net>

No comments: