HomeASP.NET Sending Mail and Configuring Your Server f...
Sending Mail and Configuring Your Server for an ASP.NET 2.0 Email System
In the previous article, you learned many of the principles you need to know when building your own online e-mail system. In this second part of a four-part series, we will look at the various e-mail formats -- text, HTML, and with attachments -- and the code required to make the system able to send and receive those kinds of messages. You will also learn how to configure your e-mail server. It is strongly recommended that you read the first part of the series before reading this one.
Contributed by Xianzhong Zhu Rating: / 8 June 11, 2007
According to the analysis in the previous article, to send text-formatted e-mail it only takes steps 1-5 and step 8, or six steps in total. Feel free to check the first part of this series to refresh your memory. In any case, the corresponding code is as follows:
Similarly, according to the earlier explanations, it will take steps 1-6 and step 8, or seven steps altogether, to send HTML formatted e-mails. Here for brevity, we mainly list the different steps:
As is introduced above, it will take all nine steps to send e-mail with attachments, which is the most complicated mode of sending e-mail. We mainly list the distinct part of the source code below:
So far, we've discussed sending e-mails, with all the connected steps. Next, we will examine how to configure your e-mail server to ensure that e-mail will be sent out correctly and safely.
Please note that here I'm going to show you a simple online e-mail sample system, so I'll only provide you with a few items to configure. Figure 2 shows the design time snapshot of the profile interface.
Figure 2-the design time snapshot for configuring the system
After the SystemProfile.aspx page has been initialized, the system displays the current configured information, such as information for the server IP, the mail server port, and so forth. This is done in the Page_Load function with the help of a helper function, BindWebMailProfile:
Here, in the BindWebMailProfile function, we first create a new instance of the Mail class, then call the GetWebMailProfile method of that class to obtain the configuration information from the SQL Server database. We then assign it to the corresponding fields on the page. Clicking the SetBtn button (i.e. OK) will trigger the SetBtn_Click event in which the newly-populated data are persisted back into the database. To do this, we've called the WebMailProfile method of the Mail class.