When you do not use the SMTP port 25 to send an email, some low-Permission execution environments may throw an exception. You 'd better check the permissions before performing the operation.
SMTP has the following permissions in. Net: smtppermission type (and declarative smtppermissionattribute attributes, all of which are in the system. net. Mail namespace ). Its access attribute controls the permission content. Smtpaccess enumeration. There are three values: none (none), connect (default port 25 for connection), and connecttounrestrictedport (any port for connection ).
The following is a simple console code to check SMTP permissions:
// + Using system. Security;
// + Using system. net. mail;
Static void main (string [] ARGs)
{
VaR smtppermission = new smtppermission (smtpaccess. Connect );
If (isgranted (smtppermission ))
{
Console. writeline ("allow SMTP port 25 ");
VaR smtppermission2 = new smtppermission (smtpaccess. connecttounrestrictedport );
If (isgranted (smtppermission2 ))
Console. writeline ("allow SMTP for any port ");
}
}
// Determine the permission set of the current application domain
Static bool isgranted (ipermission per)
{
VaR pset = new permissionset (permissionstate. None );
Pset. addpermission (PER );
Return pset. issubsetof (appdomain. currentdomain. permissionset );
}