[Back to Top] to prevent confusion, in ASP. the system. NET 2.0. web. mail namespace has been marked deprecated. all of the classes are still accessible using intelliisense and will still function properly. however, they too are marked obsolete. instead, a new namespace found at system. net. mail shoshould be used. this new namespace contains classes to manage your mail client and messages. the smtpclient class is used to establish a host, port, and network credentials for your SMTP server. the mailmessage class is similar to the mailmessage class found in the old system. web. mail namespace. this class allows a full email message to be built. there is also an attachment class that allows an attachment to be generated so it can later be added to the mailmessage object. in addition to these three most commonly used classes, you will find that system. net. mail contains an alternateview and modify Resource class. unfortunately, Asp. NET 2.0 does not provide a clean and easy way to access the system.net section group from the web. config file within code. the smtpclient object contains a usedefacrecredentials boolean property, but that specifies whether the smtpclient object is to use the credentials of the user currently running the application. before we begin accessing the attributes in the web. config file, we will need to determine what credentials will be required to connect to the SMTP server and ensure that the values we will use will work. in your cases, the credentials that wocould be used to connect to a Microsoft Exchange Server wocould be different than those to connect to your ISP email systems. in the example to follow we will use a username, password, and hostname. the first task on our plate is to read the mailsettings section group from the web. config file. to accomplish this task, we will need to create a new object of type system. configuration. configuration. this object will store our web. config file. we will assign this new object to the applications Web. config by using the openwebconfiguration method of the webconfigurationmanager class. the openwebconfiguration method will look for a relative path. we will pass in the httpcontext. current. request. applicationpath since we will want to load the web. config file for the current application. The second task is to load the specific section group from the object that contains our web. config file into a new object. A new object of type system. net. configuration. mailsettingssectiongroup will be created. this object will be assigned to the mailsettings section in the web. config file. we will use the getsectiongroup method of our configuration object to obtain this section. ASP. net does not know that the mailsettings section is found under system.net so we must specify the full section group as system.net/mailsettings. an example of this can be seen in Listing 2. Listing 2 DimConfigAsSystem. configuration. Configuration=_ Webconfigurationmanager. openwebconfiguration (_ httpcontext. Current. Request. applicationpath)DimSettingsAsSystem. net. configuration. mailsettingssectiongroup=_Ctype(Config. getsectiongroup ("System.net/mailsettings"), _ System. net. configuration. mailsettingssectiongroup)
|