HomeASP.NET Finishing an Online E-Mail System in ASP.N...
Finishing an Online E-Mail System in ASP.NET 2.0
Yesterday we began designing the interface for an online e-mail system in ASP.NET 2.0. Today, in the conclusion to this four-part series, we pick up where we left off. As you'll recall, the last thing we did was learn how to create folders; in this article, we'll learn how to rename and delete folders, and read and move e-mail.
Contributed by Xianzhong Zhu Rating: / 4 June 13, 2007
In this sample, renaming the folder is achieved through the RenameFolder.aspx, page. Figure 10 shows the relevant design-time snapshot.
Figure 10-the design-time view of the RenameFolder.aspx page
Since there are few controls on the page and their functions can be easily doped out, we choose not to waste time on the controls but to dive right into the associated code.
SqlDataReader dr = folder.GetSingleFolder(nFolderID);
if(dr.Read())
{
Name.Text = dr["Name"].ToString();
}
dr.Close();
}
Here, when the RenameFolder.aspx page is initialized, we first obtain the value of the nFolderID parameter out of the address bar (of the browser), then call a helper function named BindFolderData which bears the responsibility of grabbing the information of the folder out of the database using the nFolderID parameter, and then displays the folder name in the text box called Name. Next, when you click the OK button (with the id of RenameBtn), renaming is started:
Here, when you click the OK button, the NewBtn_Click event is triggered to call the helper function named RenameFolder to rename the folder and persist the new name into the database. Finally, clicking the Return button will navigate you back to the MailDesktop.aspx page.
Deleting folders is accomplished inside the MailDesktop.aspx page. To do so, you can just click the X button on the web page, and then the selected folders will be deleted.
When we use Windows Explorer.exe to delete something, a dialog always appears to remind us of the possible danger. To achieve such an effect ourselves, we add the related code into the RowDataBound event of the FolderView control.
Here, in the RowCommand event, we obtain the value of the CommandName property of the X button and save it in the CommandName parameter. Next, we get the value of the CommandArgument property and convert it into an integer which is stored in the nFolderID parameter. If all the requirements are satisfied, then we start to delete the selected folder, which is fulfilled by calling the DeleteFolder (int nFolderID) function of the IDisk interface, removing the item (whose value of FolderID is nFolderID) from the database. And finally, an appropriate dialog appears to indicate whether or not the operation is successful.
Now we'll examine how to read e-mail, which is done through the Reader.aspx page. Figure 11 gives its design-time snapshot.
Figure 11-the design-time snapshot of the Reader.aspx page
Here we use a table to exhibit the controls and their functions.
Id
Control Type
Function
To
TextBox
The receiver's address (may be more than one)
CC
TextBox
The email address(es) to whom the email is copied
Title
TextBox
Title of the email
Body
TextBox
Body of the email
AttachView
GridView
Used to show the attachment(s) with the email
ReceiverBtn
Button
Reply to the receiver(s) (including those to be copied to)
ReturnBtn
Button
Return to the ViewMail.aspx page
HtmlCB
HTML checkbox Button
Set the format of the email
2. Initialization
When initialized, the Reader.aspx page first obtains the parameters named FolderID and MailID from the Request object (which is passed when you click the corresponding hyperlink address of the email in the ViewMail.aspx page):
You can see here that we define a helper function named BindMailData which picks out the detailed information about the email from the underlying database according to the nMailID parameter identifying the email. And also, the corresponding attachment is shown from inside the BindMailData function, with a GridView control (AttachView) to display the content of the attachment. Additionally, when you click the 'New Mail' link from inside the draft box, you can view the email in it, too.
3. Reply and Return
When you click the Reply button on the page, the control is directed to the Sender.aspx page:
As is seen from above, we first find the ID value matching each e-mail to be deleted in the mail list control named MailView, and then invoke the DeleteMail method of the Mail object, which is finished through a typical for loop sentence. Here, remember that after deleting the selected e-mail, you have to rebind the data to MailView (i.e. refresh).
Finally we come to the last piece -- moving selected e-mails, which is also accomplished through the ViewMail.aspx page. When clicking the Move To button, you can move the selected e-mails to another folder or mailbox. To do so, we'll follow the steps listed below:
1. Select the e-mails to move;
2. Select the destination folder or mailbox;
3. Click the Move To button to finish the task.
All the moving functions are fulfilled inside the MoveBtn_Click(object sender, EventArgs e) event, whose complete code is shown below.
To begin, we iterate through the mail list control named MailView to obtain the ID(s) of the e-mail to move, and then get the FolderID of the destination folder/mailbox, and finally call the MoveMail function to perform the movement. Similarly, after moving all the selected e-mails, you must remember to rebind data to the MailView control.
Conclusion
Whew, it has been a long a journey! However, this four-part tutorial has only presented you with a general framework for constructing an online e-mail system under ASP.NET 2.0/SQL Server 2005/.NET 2.0 environments, and you are sure to agree that there is nothing incredibly complex in it. Moreover, there are still a great many items that can be modified or enhanced, such as providing the function of strict user info authentication, using the popular AJAX technique to improve the users' real time experience, supplying the users with the ability to customize the system, and so on. Okay, there is no sense in piling up more words, just roll up your sleeves and give it a test run!