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=""/>
<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 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.

    <smtp deliveryMethod="Network" from="Pete&lt;;">
      <network defaultCredentials="true" host="" port="25" enableSsl="false" userName="pete" password="F4g%j&k9" />

No comments: