760 likes | 976 Views
Chapter Objectives. Sending E-Mail from the Web Application. Security and Privacy Issues Related to E-mail HTML-based Viruses Spam Authorized users send e-mail. The E-Mail Server. E-mail server - SMTP server Simple Mail Transfer Protocol (SMTP) Protocol, or set of rules
E N D
Chapter Objectives Introduction to ASP.NET, Second Edition
Sending E-Mail from the Web Application • Security and Privacy Issues Related to E-mail • HTML-based • Viruses • Spam • Authorized users send e-mail Introduction to ASP.NET, Second Edition
The E-Mail Server • E-mail server - SMTP server • Simple Mail Transfer Protocol (SMTP) • Protocol, or set of rules • Used to transfer mail between servers • Default folders • Pickup, Queue, Badmail, Drop, Route, Mailbox, SortTemp • C:\InetPub\MailRoot • Verify the service is running • Administrative Tools - Open Services • Runs on port 25 Introduction to ASP.NET, Second Edition
The E-Mail Server (continued) Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server • Use MMC • Default SMTP Virtual Server • General Tab • Log Files • C:\WINDOWS\Sytem32\Logiles\SmtpSvc1\ • W3C Extended Log File Format • Advanced button - IP address Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) • Access Tab • Authentication button • Anonymous access • Basic authentication • Integrated Windows Authentication • Similar to IIS Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) • Messages Tab • Size of message • Session size • Total size of all the message body sections within a single connection • Number of messages per connection • Number of recipients per connection • E-mail address for Non-delivery Report • C:\Inetpub\mailroot\Badmail Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) • Delivery Tab • How the message is delivered • If can’t be sent - number of minutes to wait before resend • Outbound Security • Configure users and authentication • Require encryption (TLS) - more secure then SSL • Outbound Connections • Limit number of messages, time-out • Limit number of connections per domain • Change port number Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) • Advanced – Figure 12-8 • Configure servers that can send or relay messages • Prevent invalid domain address in Mail From line using Masquerade domain • Hop count - number of servers permitted to pass through • Smart host - a virtual server which may be more direct or less costly than sending it directly to a domain Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) • LDAP Routing Tab • Lightweight Directory Access Protocol • Primary protocol for Active Directory • Directory service that stores information about objects on a network • Security Tab • Who can change settings - default administrators group • Only trusted users to “alter” settings • Domains • E-mail at tarastore.com and tarastore.net sent to same server Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) Introduction to ASP.NET, Second Edition
Configuring the E-Mail Server (continued) Introduction to ASP.NET, Second Edition
E-Mail Objects • .NET Mail classes • CDONTS Objects - communicate with SMTP server • C:\WINDOWS\system32\cdonts.dll • Places message in the Pickup folder - or by hand • SMTP places message into Queue folder • If local recipient - places into Drop folder • If remote account - sends message to another server • If rejected or returned, places in Badmail folder • Creates Non-Delivery report (NDR) Introduction to ASP.NET, Second Edition
E-Mail Objects (continued) Introduction to ASP.NET, Second Edition
The .NET Mail Classes • System.Web.Mail namespace • MailMessage class - construct an email message • MailAttatchment class - identify a file (attachments) • SmtpMail class - send the e-mail message Introduction to ASP.NET, Second Edition
The MailMessage Class Properties • MailFormat enumeration - format • MailFormat.Html, MailFormat.Text • MailPriority enumeration - importance • MailMessage.Normal, MailMessage.High, MailMessage.Low • MailAttachment class - construct an e-mail attachment • Filename - attachment location as string - drive, path, filename • MailMessage.Attachments - list of attachments • MailEncoding enumerator • MailEncoding.UUEncode default • MailEncoding.Base64 - binary encoding such as graphics Introduction to ASP.NET, Second Edition
The MailMessage Class Properties (continued) • MailMessage.To - recipient; multiple recipients separated with semi-colon • MailMessage.From - address of the sender • MailMessage.Subject - subject line • MailMessage.Body - body • MailMessage.Cc - a semicolon-delimited list • MailMessage.Bcc - semicolon-delimited list • MailMessage.UrlContentBase - URL base of all relative URLs used within the HTML - encoded body • MailMessage.UrlContentLocation - identify the location of content within message Introduction to ASP.NET, Second Edition
The MailMessage Class Properties (continued) • SmtpServer – change the SMTP Server from default SMTP Virtual Server to another server • Send method creates the message • Writes it to the Pickup folder • Eventually it’s moved to the Queue folder SmtpMail.Send("sender@sender.com", "recipient@recipient.com", "Subject", "Message") Introduction to ASP.NET, Second Edition
Sending an E-Mail Message from an ASP.NET Web Page • Create Chapter12 project and import files • Email_SendSingleMessage.aspx • Import namespaces Imports System.Web.Mail • Create mail object, set properties, send message • Release variables • View page in the browser • View message in Drop folder • Folder varies with your system Introduction to ASP.NET, Second Edition
Sending an E-Mail Message from an ASP.NET Web Page (continued) Dim MM As New MailMessage() MM.From = "[your_email@your_address.edu]" MM.To = "[your_email@your_address.edu]" MM.Subject = "CDONTS Exercise" MM.Body = "I have completed the CDONTS exercise" MM.BodyFormat = MailFormat.Text MM.Priority = MailPriority.Normal SmtpMail.SmtpServer = "[Server name]" SmtpMail.Send(MM) MM = Nothing Introduction to ASP.NET, Second Edition
Sending an E-Mail Message from an ASP.NET Web Page (continued) Introduction to ASP.NET, Second Edition
Troubleshooting E-Mail Messages • “The SendUsing configuration value is invalid" • Solution: • Reconfigure the SMTP settings programmatically and assign a server name before the message is sent. • "The server rejected one or more recipient addresses." • Unable to relay for [your-email@your_address.edu] • Solution • Change relay settings - allow your server to relay messages • Only allow messages from your computer to relay messages • Use exception handling and error messages • Read Non-Delivery report (NDR) Introduction to ASP.NET, Second Edition
Troubleshooting E-Mail Messages (continued) Introduction to ASP.NET, Second Edition
Troubleshooting E-Mail Messages (continued) Introduction to ASP.NET, Second Edition
Troubleshooting E-Mail Messages (continued) Introduction to ASP.NET, Second Edition
Storing and Retrieving Data in the Web Server’s File System • System.IO namespace • Interact with the file system • Classes: • File, FileStream, and Directory • To create, read, and delete files • Newer classes – FileSystemInfo • Access to DirectoryInfo and FileInfo classes • View Class Browser Application Introduction to ASP.NET, Second Edition
Storing and Retrieving Data in the Web Server’s File System (continued) Introduction to ASP.NET, Second Edition
Working with Directories • DirectoryInfo class • Access to properties Dim DD As DirectoryInfo = _ New DirectoryInfo("C:\Inetpub\wwwroot\Chapter12\") Label1.Text = DD.FullName • Other properties • Attributes, CreationTime, Exists • LastAccessTime, LastWriteTime • FullName, Extension, Name, Parent, and Root Introduction to ASP.NET, Second Edition
Working with Directories (continued) • Iterate through DirectoryInfo Dim MySubDirList As String Dim SubDir As DirectoryInfo For Each SubDir In DD.GetDirectories() MySubDir &= SubDir.Name & "<br />" Label2.Text = MySubDirList Next Introduction to ASP.NET, Second Edition
Working with Directories (continued) • Iterate through FileInfo Dim MyFileList, MyPath As String MyPath = TextBox1.Text Dim MyFile As FileInfo For Each MyFile In DFL.GetFiles Select Case MyFile.Extension Case ".aspx" MyFileList &= _ "<a href='" & MyFile.Name & "'>" & _ MyFile.Name & "</a><br />" Case ".vb", ".resx" Case Else MyFileList &= MyFile.Name & "<br />" End Select Next Label3.Text = MyFileList Introduction to ASP.NET, Second Edition
Working with Directories (continued) • DisplayChapter12Dir.aspx • DisplayDirectoryInfo function Dim DD As DirectoryInfo = _ New DirectoryInfo(MyPath) lbl1.Text = DD.FullName lbl2.Text = DD.Name lbl3.Text = DD.Attributes lbl4.Text = DD.CreationTime.ToShortDateString lbl5.Text = DD.LastAccessTime.ToShortDateString lbl6.Text = DD.LastWriteTime.ToShortDateString lbl7.Text = DD.Root.Name lbl8.Text = DD.Parent.Name lbl9.Text = DD.Exists lbl10.Text = GetLength(DirSize(DD)) Introduction to ASP.NET, Second Edition
Working with Directories (continued) Introduction to ASP.NET, Second Edition
Working with Directories (continued) Introduction to ASP.NET, Second Edition
Working with Directories (continued) Introduction to ASP.NET, Second Edition
Creating and Deleting Directories • DirectoryInfo - to create and delete directories • CreateSubdirectory method - create a new directory • Pass the name of the directory • CreateDir.aspx • Buttons to create and delete directories • DisplayDirectory function • Retrieve the Users subdirectory list • c:\Inetpub\wwwroot\Chapter12\Users Introduction to ASP.NET, Second Edition
Creating and Deleting Directories (continued) • System.Exception • System.IO • IOException • DirectoryNotFoundException - directory does not exist • PathTooLongException - exceeds 248 characters • FileNotFoundException • Security.SecurityException - don’t have permission • System.ArgumentException • Create a directory with invalid characters <>*?” or \\ Introduction to ASP.NET, Second Edition
Creating and Deleting Directories (continued) Dim DDL As DirectoryInfo = _ New DirectoryInfo(MyPath) Dim SubDir As DirectoryInfo Dim MyHash As New Hashtable For Each SubDir In DDL.GetDirectories() MyHash.Add(SubDir.Name, SubDir.Name) Next DropDownList1.DataTextField = "Key" DropDownList1.DataValueField = "Value" DropDownList1.DataSource = MyHash DropDownList1.DataBind() Introduction to ASP.NET, Second Edition
Working with Directories (continued) • btnCreate_Click procedure Dim DD As DirectoryInfo = _ New DirectoryInfo(MyPath) DD.CreateSubdirectory(TextBox1.Text) • btnDelete_Click procedure Dim DD2 As DirectoryInfo = _ New DirectoryInfo(MyPath & _ DropDownList1.SelectedItem.ToString) Label1.Text = "The <b>" & _ DropDownList1.SelectedItem.ToString & _ " </b>directory was deleted." DD2.Delete() Introduction to ASP.NET, Second Edition
Working with Directories (continued) Introduction to ASP.NET, Second Edition
Creating and Reading Files from an ASP.NET Page • FileInfo class • Common Properties • Attributes, CreationTime, LastAccessedTime, LastWriteTime • Name, FullName, and Exists • Attributes – (Right click on file and select Properties) • Hidden, Archive, ReadOnly, System • Methods • CopyTo, MoveTo, Delete • CreateText method • Returns StreamWriter object and open file for editing • Create method • Wrapper for the functionality of File.Create • Uses FileStream object not StreamWriter object Introduction to ASP.NET, Second Edition
Creating a Text File • CreateFile.aspx • btnCreate_Click - creates the FileInfo object • Create StreamWriter object to create the file and open it for writing Dim MyFile As String = MyPath & TextBox1.Text Dim MyFileInfo As FileInfo = New FileInfo(MyFile) Dim SW As StreamWriter = File.CreateText(MyFile) Introduction to ASP.NET, Second Edition
Creating a Text File (continued) • Write the contents to the file - Write and WriteLine SW.Write("This file was ") SW.WriteLine("created by: ") SW.Write("[Your Name]") SW.Write(vbCrLf) SW.WriteLine("on " & Now.Date.ToShortDateString) SW.WriteLine() SW.WriteLine() SW.Write(TextBox1.Text) SW.Close() MyFileInfo = Nothing Introduction to ASP.NET, Second Edition
Creating a Text File (continued) • btnDelete_Click event procedure Label1.Text = "The <b>" & TextBox1.Text & _ " </b>file was created successfully." Dim MyFileInfo As FileInfo = New FileInfo(MyFile) Label1.Text = "The <b>" & _ DropDownList1.SelectedItem.ToString & _ " </b>file was deleted." MyFileInfo.Delete() MyFileInfo = Nothing Introduction to ASP.NET, Second Edition
Creating a Text File (continued) • btnDisplay_Click • Retrieves file - FileInfo object and opens file with OpenText • Returns StreamReader object • ReadToEnd - reads entire file line by line until end of file Dim MyFileInfo As FileInfo = New FileInfo(MyFile) Dim SR As StreamReader = MyFileInfo.OpenText() Label3.Text = SR.ReadToEnd SR.Close() SR = Nothing Introduction to ASP.NET, Second Edition