. Net (C #): Do not use SMTP port 25 to check the permission for sending emails

Source: Internet
Author: User

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 );

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.