When running ASP.NET in high and medium trust, ASP.NET 2.0+ SMTPClient related classes are not able to deliver emails if the port of the SMTP server is not 25.

This is not easily grasped from the documentation, but from the ASP.NET Code Access Security overview (http://msdn.microsoft.com/en-us/library/87x8e4d1.aspx) we can see the following table:

Permission Full High Medium Low Minimal
SmtpPermission Unrestricted Connect Connect No permission No permission

One has to go to the SmtpPermission definition and then the SmtpAccess enumeration to discover what Connect means:

Member name Description
None No access to an SMTP host
Connect Connection to an SMTP host on the default port (port 25)
ConnectToUnrestrictedPort Connection to an SMTP host on any port

This piece of configuration is really important in hosting or especially restrictive environments.

I found a workaround (in the case that you could create a custom policy definition) from Rick Strahl’s Web Log (SmtpClient in Medium Trust - Rick Strahl's Web Log):

<IPermission class="SmtpPermission"            
version="1"
Unrestricted="true" />
 
One has to include the above code in the custom policy definition.

Thanks to Rick by the tip.

I hope this post helps,

Álvar García

References:

  1. ASP.NET Code Access Security (http://msdn.microsoft.com/en-us/library/87x8e4d1.aspx)
  2. <mailSettings> Element (Network Settings) (http://msdn.microsoft.com/en-us/library/w355a94k(VS.80).aspx)
  3. SmtpClient in Medium Trust - Rick Strahl's Web Log