Email (Asp. net2.0 ))

Source: Internet
Author: User
Tags mailmessage smtpclient
Introduction
[Back to Top]

In ASP. net 1.x sending email was fairly simple. the only issue was that there were different ways to send email depending on whether the application was web or Windows based. windows developers used mapi to send emails using an email client like outlook. web developers used cdosys to send rich emails. microsoft realized that a change was warranted. in ASP. NET 2.0 Microsoft released a new set of classes to handle email. the only issue is that to initialize developers, sending email became much more complex. within this article, we will walk through the changes to the Web. config file, the new classes that are available and how to send email using the new mailmessage class.

Examining the Web. config
[Back to Top]

By now you should be familiar with the system. web Section group that is found in the web. config file. in ASP. NET 2.0 a new group was added that contains writable able sections for the system. net namespace called the system.net section group. this group allows email settings to be established by using the mailsettings section group. since we are sending emails and not signing ing emails, we will use the SMTP protocol. to define the SMTP settings, we will add the SMTP section group to the mailsettings section group. the SMTP section group then contains a network section that specifies attributes for the SMTP network settings. these attributes include host, port, username, and password. an example of the system.net section group can be found in Listing 1.

Listing 1

<System.net><Mailsettings><SMTP><Network Host= "Mail.contoso.com"Username= "Email@contoso.com" Password= "Password" /></SMTP></Mailsettings></System.net>

Accessing the Web. config from code
[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)

Creating the SMTP client
[Back to Top]

A new object of Type smtpclient will be created to act as the SMTP server for mail being sent out. the smtpclient object will need to have the network credentials passed into it. thus, we will need to create a new object of type system. net. networkcredential and pass in the username and password values from the web. config. these values can be found by assessing the SMTP. network. username and SMTP. network. password properties of the settings object defined earlier. the networkcredential object will be assigned to the Credentials property of the smtpclient object. the other property we must set is the smtpclient object's host property. this too will be assigned a value from the settings object created earlier. an example of this can be seen in the listing given below.

Listing 3

'Obtain the network credentials from the mailsettings SectionDimCredentialAs NewSystem. net. networkcredential (_ settings. SMTP. Network. username, settings. SMTP. Network. Password)'Create the SMTP clientDimClientAs NewSmtpclient () client. Host=Settings. SMTP. Network. Host client. Credentials=Credential

Creating a mailmessage object
[Back to Top]

the mailmessage object in ASP. NET 2.0 is much more robust than the one found in the cdosys libraries in ASP. net 1. x. the new mailmessage object still contains all of the common properties that an email message has including to, from, subject, and body. the to and from properties have changed a bit. the from property is no longer "just a string. "Instead, the from property is of Type mailaddress. the to property is similar in that it is a collection of mailaddress objects. since the CC and BCC properties have the same requirements as the to property, they too are a collection of mailaddress objects. the mailmessage object contains some additional properties including, but not limited to, alternateviews, attachments, bodyencoding, deliveryicationicationoptions, isbodyhtml, priority, and replyto.

The alternateviews property allows an alternate view to be specified by passing in either a filename or a stream and assigning a MIME type to the view. the attachments property allows attachments to be added to the message. the bodyencoding property is a value that can specify the system. text. encoding type of the email message. the deliverynotificationoptions Property specifies when delivery notifications shoshould be specified ed. the isbodyhtml property is a Boolean value signifying whether the email's body is HTML or plain text. the priority Property specifies whether this email is of high, low, or normal importance. the replyto property is the email address that the reply message wocould go.

To send the mailmessage object, the object must be passed into the send method of the smtpclient object. An example of this can be seen in the listing below.

Listing 4

'Build email messageDimEmailAs NewMailmessage email. From= NewMailaddress (Email@contoso.com") Email.To.Add(Steveb@contoso.com") Email. CC.Add(Billg@contoso.com") Email. Subject= "Test email"Email. isbodyhtml= TrueEmail. deliverynotificationoptions=Deliverynotificationoptions. onfailure email. Body= "<Strong> Hello Steve! </Strong>"'Send emailClient. Send (email)

Conclusion
[Back to Top]

as you can see now, sending email in ASP. NET 2.0 is much more complex. however, it is also much more robust. authorization of the email options that you cocould not access before without adding an email header to the email message can now be added with properties. hopefully the Team will continue to enhance this namespace and add additional functionality in the future.

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.