470 likes | 692 Views
第 1 5 章 使用服务应用程序和 电子邮件消息 . .NET Framework 2.0 程序设计. 第 1 章 : .NET Framework 2.0 简介 第 2 章:公共语言运行库和类型 第 3 章:托管代码的编译和执行 第 4 章:委托和事件 第 5 章:读取和写入文件 第 6 章:集合和泛型 第 7 章:数据的序列化 第 8 章: GDI+ 第 9 章: 在 .NET Framework 2.0 中实现加密.
E N D
.NET Framework 2.0程序设计 第 1 章: .NET Framework 2.0简介 第 2 章:公共语言运行库和类型 第 3 章:托管代码的编译和执行 第 4 章:委托和事件 第 5 章:读取和写入文件 第 6 章:集合和泛型 第 7 章:数据的序列化 第 8 章:GDI+ 第 9 章:在 .NET Framework 2.0中实现加密 第 10 章: COM 组件与.NET Framework程序集之间的交互操作 第 11 章:使用类型元数据 第 12 章:创建多线程应用程序和应用程序域 第 13 章:代码访问安全性 第 14 章:监视和调试应用程序 第 15 章:使用服务应用程序和电子邮件消息 第 16 章:创建全球化应用程序 第 17 章:配置和安装程序集
本章学习目标: 使用 ServiceBase、ServiceInstaller、ServiceProce ssInstaller 和 ServiceController 类管理 Windows 服务应用程序 通过MailMessage、MailAddress、MailAddress Collection、MailAttachment、SmtpClient、Smtp Exception 和 SmtpFailedRecipientException 类以及 SendCompleteEventHandler 委托使用电子邮件消息 目标
第15章使用服务应用程序和电子邮件消息 • 使用 Windows 服务应用程序 • 使用 ServiceController 类控制 Windows 服务 • 使用电子邮件消息 • 小结 • 实验 • 习题
使用 Windows 服务应用程序 15.1 使用 Windows 服务应用程序 • 服务应用程序 • 使用 ServiceBase 类创建 Windows 服务 • 使用 ServiceInstaller 类安装服务应用程序
显示计算机中可用服务的完整列表 多媒体演示
. 是一个长时间运行的可执行文件或进程 在用户没有登录计算机的情况下也可以运行 有许多服务可以作为操作系统的一部份来安装,也可作为其他应用程序的一部分来安装 .NET Framework 可以创建两种类型的服务: Win32OwnProcess 服务 Win32ShareProcess 服务 服务应用程序 15.1.1 服务应用程序 服务应用程序
使用 ServiceBase 类创建 Windows 服务 15.1.2 使用 ServiceBase 类创建 Windows 服务 System.ServiceProcess.ServiceBase 类是Windows 服务应用程序的核心。 Windows 服务应用程序的 Main 方法: • 必须创建一个服务类的实例并将其传递给ServiceBase 类的 Run 方法 • 必须设置服务类的 ServiceName 属性 • Main 方法可以为服务类设置其他属性, 也可以在 Main 方法中创建多个服务类的实例 • 在 Windows 服务应用程序中,服务类必须重写 ServiceBase 类的 OnStart 和 OnStop 方法
. ServiceBase 类中的常用成员 15.1.2 使用 ServiceBase 类创建 Windows 服务 ServiceBase 类中的常用成员
. ServiceBase 类中的 常用成员(续) 15.1.2 使用 ServiceBase 类创建 Windows 服务 续表:
. ServiceBase 类中的常用成员(续) 15.1.2 使用 ServiceBase 类创建 Windows 服务 续表:
. ServiceBase 类中的常用成员(续) 15.1.2 使用 ServiceBase 类创建 Windows 服务 续表: 代码示例C# 代码示例VB
. 使用 ServiceInstaller 类安装服务应用程序 15.1.3 使用 ServiceInstaller 类安装服务应用程序 System.ServiceProcess.ServiceInstaller 和 System.ServiceProcess.ServiceProcessInstaller 类的功能: • ServiceInstaller 类 • 用于在 Windows 服务应用程序中安装一个服务,在用户没有登录计算机的情况下也可以运行。 • ServiceProcessInstaller 类 • 用于安装 Windows 服务应用程序的可执行文件。 代码示例C# 代码示例VB
. ServiceInstaller 类中的常用成员 15.1.3 使用 ServiceInstaller 类安装服务应用程序
. ServiceInstaller 类中的常用成员(续) 15.1.3 使用 ServiceInstaller 类安装服务应用程序 续表:
. ServiceProcessInstaller 类中的常用成员 15.1.3 使用 ServiceInstaller 类安装服务应用程序
使用 Windows 服务应用程序 使用 ServiceController 类控制 Windows 服务 使用电子邮件消息 小结 实验 习题 第15章使用服务应用程序和电子邮件消息
. 使用 ServiceController 类控制 Windows 服务 15.2 使用 ServiceController 类控制 Windows 服务 示例:使用 ServiceController 类来停止一个正在运行的服务。 [C#] using System; using System.ServiceProcess; namespace FreeSpaceMonitor { public class Program { static void Main() { ServiceController fsmService = new ServiceController("FreeSpaceMonitor"); if (fsmService.Status == ServiceControllerStatus.Running) fsmService.Stop(); } } } [VB.NET] Imports System.ServiceProcess Public Class Program Shared Sub Main() Dim fsmService As New ServiceController("FreeSpaceMonitor") If fsmService.Status = ServiceControllerStatus.Running Then fsmService.Stop() End If End Sub End Class
. ServiceController 类中的常用成员 15.2 使用 ServiceController 类控制 Windows 服务
. ServiceController 类中的常用成员(续) 15.2 使用 ServiceController 类控制 Windows 服务 续表:
. ServiceController 类中的常用成员(续) 15.2 使用 ServiceController 类控制 Windows 服务 续表:
. ServiceController 类中的常用成员(续) 15.2 使用 ServiceController 类控制 Windows 服务 续表:
使用 Windows 服务应用程序 使用 ServiceController 类控制 Windows 服务 Http协议的应用 使用电子邮件消息 小结 实验 习题 第15章使用服务应用程序和电子邮件消息
使用HTTP对象模型 WebClient类 WebRequest 类 WebResponse 类
创建 WebRequest • WebRequest 类封装了一个 web 请求的详细细节 • 调用 WebRequest.Create 方法来创建实例 • 设置需要的属性值 • 转换对象,以便访问和协议相关的一些特点 WebRequest req = WebRequest.Create("http://www.contoso.com/"); req.Credentials = new NetworkCredential("username","password"); HttpWebRequest httpReq = (HttpWebRequest) WebRequest.Create("http://www.contoso.com/"); //关闭 HTTP Keep-alive 行为. httpReq.KeepAlive = false;
调用 WebRequest • 通过调用 GetResponse 方法来创建 WebRequest 实例 • 转换 WebResponse 到 HTTPWebResponse 以便访问和HTTP协议有关的特点 WebResponse resp = req.GetResponse(); HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse(); //取得从服务器返回的HTTP内容长度。 String contentLength = httpResp.ContentLength.ToString();
使用 Windows 服务应用程序 使用 ServiceController 类控制 Windows 服务 Http协议的应用 使用电子邮件消息 小结 实验 习题 第15章使用服务应用程序和电子邮件消息
使用电子邮件消息 15.3 使用电子邮件消息 • 使用邮件类创建电子邮件消息 • 使用 MailAttachment 类向电子邮件消息添加资源 • 使用 SmtpClient 类发送电子邮件消息 • 使用 SMTP 异常类来处理电子邮件异常 • 使用 SendCompleteEventHandler 处理电子邮件完成事件
. 使用邮件类创建电子邮件消息 15.3.1 使用邮件类创建电子邮件消息 MailMessage 类 MailAddress 类 MailAddressCollection 类
. MailMessage 类 1. MailMessage 类 示例:在 C#和VB.NET 中创建新消息。 [C#] using System; using System.Net.Mail; public class Program { static void Main() { string sender = "I@A.COM"; string recipient = "You@B.COM"; MailMessage myMessage = new MailMessage(sender, recipient); myMessage.Subject = "Sending messages from .NET applications is easy!"; } } [VB.NET] Imports System.Net.Mail Public Class Program Shared Sub Main() Dim sender As String = "I@A.COM" Dim recipient As String = "You@B.COM" Dim myMessage As New MailMessage(from:=sender, to:=recipient) myMessage.Subject = "Sending messages from .NET applications is easy!" End Sub End Class
. MailAddress 类 2. MailAddress 类 MailAddress 类中的常用成员:
. MailAddressCollection 类 3. MailAddressCollection 类 MailAddressCollection 类的成员:
. 使用 MailAttachment 类向电子邮件消息添加资源 15.3.2 使用 MailAttachment 类向电子邮件消息添加资源 AttachmentBase 类的成员:
AlternateView 类 1. AlternateView 类 常用成员:
Attachment 类 2. Attachment 类 Attachment 构造函数的重载方法: • [C#] • Attachment(string FileName) • Attachment(string FileName, string MediaType) • Attachment(string FileName, System.Net.Mime.ContentType ContentType) • Attachment(System.IO.Stream ContentStream, string Name) • Attachment(System.IO.Stream ContentStream, string Name, string MediaType) • [VB.NET] • Attachment(FileName as String) • Attachment(FileName as String, MediaType as String) • Attachment(FileName as String, ContentType as System.Net.Mime.ContentType) • Attachment(ContentStream as System.IO.Stream, Name as String) • Attachment(ContentStream as System.IO.Stream, Name as String, MediaType as String)
Attachment 类(续) 2. Attachment 类 Attachment 类的常用成员:
LinkedResource 类 3. LinkedResource 类 常用成员: 代码示例C# 代码示例VB
. 使用 SmtpClient 类发送电子邮件消息 15.3.3 使用 SmtpClient 类发送电子邮件消息 示例:标识 SMTP 主机的 machine.config 文件: <configuration> <system.net> <mailSettings> <smtp deliveryMethod="network"> <network host="localhost" port="25" defaultCredentials="true" /> </smtp> </mailSettings> </system.net> </configuration> 代码示例VB 代码示例C#
SmtpClient 类中的常用成员 15.3.3 使用 SmtpClient 类发送电子邮件消息 SmtpClient 类中的常用成员
SmtpClient 类中的 常用成员(续) 15.3.3 使用 SmtpClient 类发送电子邮件消息 续表:
SmtpClient 类中的 常用成员(续) 15.3.3 使用 SmtpClient 类发送电子邮件消息 续表:
SmtpClient 类中的 常用成员(续) 15.3.3 使用 SmtpClient 类发送电子邮件消息 续表:
使用 SMTP 异常类处理电子邮件异常 15.3.4 使用 SMTP 异常类处理电子邮件异常 示例:[C#] using System; using System.Net.Mail; public class SendSimpleMessageDemo { static void Main() { try { SmtpClient mailServer = new SmtpClient("OurEmailServer"); MailMessage theMessage = new MailMessage("Somebody@Microsoft.com", "Somebody@Contoso.com"); theMessage.Subject = "This is just a test"; theMessage.Body = "Always check for excpetions when sending email."; mailServer.Send(theMessage); } catch (SmtpFailedRecipientsException xcp) { Console.WriteLine(xcp.ToString()); } catch (SmtpFailedRecipientException xcp) { Console.WriteLine(xcp.ToString()); } catch (SmtpException xcp) { Console.WriteLine(xcp.ToString()); } } }
使用 SMTP 异常类处理电子邮件异常(续) 15.3.4 使用 SMTP 异常类处理电子邮件异常 示例:[VB.NET] Imports System.Net.Mail Public Class SendSimpleMessageDemo Shared Sub Main() Try Dim mailServer As New SmtpClient("OurEmailServer") Dim theMessage As New MailMessage("Somebody@microsoft.com", "Somebody@Contoso.com") theMessage.Subject = "This is just a test" theMessage.Body = "Always check for exceptions when sending e-mail!" mailServer.Send(theMessage) Catch xcp As System.Net.Mail.SmtpFailedRecipientsException Console.WriteLine(xcp.ToString()) Catch xcp As System.Net.Mail.SmtpFailedRecipientException Console.WriteLine(xcp.ToString()) Catch xcp As System.Net.Mail.SmtpException Console.WriteLine(xcp.ToString()) Catch xcp As Exception Console.WriteLine(xcp.ToString()) End Try End Sub End Class
使用 SendCompleteEventHandler 处理电子邮件完成事件 15.3.5 使用 SendCompleteEventHandler 处理电子邮件完成事件 • SmtpClient 类还能使用 SmtpClient.SendAsync 方法来异步发送电子邮件消息 Attachment(string FileName) • 调用 SendAsync 后,必须等到传送完成后才能通过相同操作发送另一个消息 • SendAsync 方法接受两个参数 : • message • userToken 代码示例C# 代码示例VB
使用 Windows 服务应用程序 使用 ServiceController 类控制 Windows 服务 使用电子邮件消息 小结 实验 习题 第15章使用服务应用程序和电子邮件消息
小结 15.4 小结 本章介绍了如何使用Windows服务应用程序 ,.NET Framework的System.ServiceProcess 命名空间中提供了多个类来帮助用户使用和控制Windows服务应用程序。本章还介绍了如何使用电子邮件消息。使用System.Net.Mail命名空间下的类,可以方便地发送电子邮件。