发送带有附件的邮件示例代码 下面的 C# 示例代码将包含一个演示如何发送带有附件的电子邮件的 Windows 控制台程序 这将由创建类 MessageAttachment 的实例 然后将其添加到 Attachments 集合来完成 using System using System Web Mail namespace CodeGuru SendMail { /// <summary> /// console application to demonstrate sending e mail with an /// attachment /// </summary> class TestMail { /// <summary> /// The main entry point for the application /// </summary> [STAThread] static void Main(string[] args) { TestMail SendAttachment( t mstraw Test Message Using CDOSYS Hello World! This is a simple message sent using CDOSYS c:\\myattachment txt ) } /// <summary> /// Send a message using the NET wrapper for Collaborative Data /// Objects (CDO) This method should be used when sending to /// a single recipient onlyotherwise the list of recipients /// will be known /// </summary> /// <param name= MessageFrom >Message originator</param> /// <param name= MessageTo >Message receipent</param> /// <param name= MessageSubject >Message subject</param> /// <param name= MessageBody >Message body</param> /// <param name= MessageAttachmentPath >Path to attachment /// </param> public static void SendAttachment(string MessageFrom string MessageTo string MessageSubject string MessageBody string MessageAttachmentPath) { // Create and setup the message MailMessage message = new MailMessage() message From = MessageFrom message To = MessageTo message Subject = MessageSubject message BodyFormat = MailFormat Text message Body = MessageBody // Create and add the attachment MailAttachment attachment = new MailAttachment(MessageAttachmentPath) message Attachments Add(attachment) try { // Deliver the message System Console WriteLine( Sending outgoing message ) SmtpMail Send(message) } catch( System Web HttpException exHttp ) { System Console WriteLine( Exception occurred: + exHttp Message) } } } }
可能的改进 我们已经从不同途径演示了如何发送电子邮件 现在轮到你想想如何在你的应用程序中应用这项功能了 这里有一些想法和你共同分享 E mail 警告—当一个致命的或无法恢复的应用程序错误发生时 您的应用程序可以发送电子邮件到一指定地址以使其能很快得知 创建一个基于Web的联系消息窗体—你可以使用户通过填写 Web 表单来发送用户反馈 然后编写相关程序把消息发送给适当的联系人 订阅服务—当通过 CDOSYS 组件发送电子邮件来支持订阅类型的服务时 您将需要发送多封邮件而不是单一邮件给所有的接收者 当一则消息拥有大量的接收者时 处理所有的接收者将戏剧性地减满处理速度 这时候您最好将接收者列表分解成多个列表 分好几次发送消息 使用 Bcc 发送消息—当通过 CDOSYS 组件发送电子邮件来支持订阅类型的服务时 您也许想要使用 Bcc 而不是 To 来填写接收人地址 这样可以使得所有接收者无法得知接收者列表 发送 HTML 格式的邮件—消息主体格式可以是 HTML 它可以使消息主体以 HTML 格式发送而不是普通文本 原文 Sending E Mail with System Web Mail Mark Strawmyer (view PRofile) February Rating: not yet rated Wele to the next installment of the NET Nuts &Bolts column In this column we ll explore sending e mail from within applications This will involve utilizing classes contained in the System Web Mail namespace Collaboration Data Objects Collaboration Data Objects for Windows (CDOSYS) is a Microsoft messaging ponent that allows for standards based e mail messages to be constructed and sent It is a replacement of the Collaboration Data Objects for NTS (CDONTS) which as you can probably guess by the name was for Windows NT CDONTS is included with Windows for backwards patibility but Windows XP Windows Server and beyond do not include or support the use of CDONTS Thus any applications that send messages using CDONTS must be migrated to use CDOSYS It provides the same functionality and is just as easy to use In addition to serving as a replacement CDOSYS introduces some new functionality that was not previously available in CDONTS Some of the functionality includes: Ability to post messages to newsgroups Control of MIME body structure for messages Reply and forward functionality Transport event sink to allow responding to events The System Web Mail namespace contains classes that interact with CDOSYS to construct and send the message(s) Using IIS and SMTP Service In order for CDOSYS to send e mail or other messages from your application you need to enlist the services of IIS with the SMTP Service installed Both are available in Windows /XP through the Control Panel >Add/Remove Programs >Add/Remove Windows Components option The job of the STMP Service is to accept and deliver the messages based on the configuration The service can attempt to deliver the messages directly or it can utilize a *** art host to deliver the message instead When a *** art host is enlisted all messages are forwarded to it for delivery You need to have IIS and the SMTP service installed and configured The SMTP Service uses a directory structure to contain messages prior to delivery The default directory is C:\Inetpub\mailroot This folder contains a number of subdirectories such as Queue Drop and Badmail If you are unable to configure your instance of the SMTP Service for delivery you can find the message in a * EML file in the C:\Inetpub\mailroot\Queue directory This technique can be useful when creating messages offline Sending a Message As previously mentioned sending an e mail is a relatively simple thing to do The System Web Mail MailMessage class represents the message to be sent E mail messages are constructed by using instances of this class This class contains properties such as To From and Subject that allow you to control the message being sent Attachments can be created through instances of the System Web Mail MailAttachment and then added to the MailMessage through the Attachments collection of the MailMessage The message is then delivered through the System Web Mail SmtpMail class
Sending a Message Sample Code The following sample code contains a C# based Windows Console application that shows how to send an e mail message By not specifying that the SmtpMail SmtpServer property is not set the localhost is used by default You need to make sure to add a reference to the System Web dll because this is a console application and not an ASP NET application using System using System Web Mail namespace CodeGuru SendMail { /// <summary> /// Test console application to demonstrate sending e mail /// </summary> class TestMail { /// <summary> /// The main entry point for the application /// </summary> [STAThread] static void Main(string[] args) { TestMail Send( t mstraw Test Message Using CDOSYS Hello World! This is a simple message sent using CDOSYS ) } /// <summary> /// Send a message using the NET wrapper for Collaborative Data /// Objects (CDO) This method should be used when sending to a /// single recipient onlyotherwise the list of recipients /// will be known /// </summary> /// <param name= MessageFrom >Message originator</param> /// <param name= MessageTo >Message receipent</param> /// <param name= MessageSubject >Message subject</param> /// <param name= MessageBody >Message body</param> public static void Send(string MessageFrom string MessageTo string MessageSubject string MessageBody) { MailMessage message = new MailMessage() message From = MessageFrom message To = MessageTo message Subject = MessageSubject message BodyFormat = MailFormat Text message Body = MessageBody try { System Console WriteLine( Sending outgoing message ) SmtpMail Send(message) } catch( System Web HttpException exHttp ) { System Console WriteLine( Exception occurred: + exHttp Message) } } } } Sending a Message with an Attachment Sample Code The following sample code contains a C# based Windows Console application that shows how to send an e mail message that includes an attachment This is done by creating instances of the MessageAttachment class and then adding them to the message through the Attachments collection
using System using System Web Mail namespace CodeGuru SendMail { /// <summary> /// console application to demonstrate sending e mail with an /// attachment /// </summary> class TestMail { /// <summary> /// The main entry point for the application /// </summary> [STAThread] static void Main(string[] args) { TestMail SendAttachment( t mstraw Test Message Using CDOSYS Hello World! This is a simple message sent using CDOSYS c:\\myattachment txt ) } /// <summary> /// Send a message using the NET wrapper for Collaborative Data /// Objects (CDO) This method should be used when sending to /// a single recipient onlyotherwise the list of recipients /// will be known /// </summary> /// <param name= MessageFrom >Message originator</param> /// <param name= MessageTo >Message receipent</param> /// <param name= MessageSubject >Message subject</param> /// <param name= MessageBody >Message body</param> /// <param name= MessageAttachmentPath >Path to attachment /// </param> public static void SendAttachment(string MessageFrom string MessageTo string MessageSubject string MessageBody string MessageAttachmentPath) { // Create and setup the message MailMessage message = new MailMessage() message From = MessageFrom message To = MessageTo message Subject = MessageSubject message BodyFormat = MailFormat Text message Body = MessageBody // Create and add the attachment MailAttachment attachment = new MailAttachment(MessageAttachmentPath) message Attachments Add(attachment) try { // Deliver the message System Console WriteLine( Sending outgoing message ) SmtpMail Send(message) } catch( System Web HttpException exHttp ) { System Console WriteLine( Exception occurred: + exHttp Message) } } } } Possible Enhancements We have demonstrated how to send e mail messages in a couple of ways It is now up to you to think about ways in which you can utilize this functionality within your applications Here are some ideas to consider on your own: E mail alerts—when a fatal or unrecoverable application error occurs your application could e mail information to a designated location so that it is immediately known Build a Web based contact form—you can allow users to send customer feedback by filling out a Web form and then programmatically e mailing it to the appropriate contact(s) Subscription service—when sending mail by using CDOSYS for a subscription type service you may want to send multiple messages instead of a single message with all of the recipients When a message has too many recipients it can drastically slow processing as all of the recipients are processed It is often better to break the list of recipients into multiple lists and send multiple messages Send messages using Bcc—when sending mail using by CDOSYS for a subscription type service you may want to address messages using the Bcc instead of To This will keep the list of recipients unknown to all of those that receive it Send HTML formatted mail—the message body format can be set to HTML This will allow the body of the message to be sent in HTML format rather than plain text lishixinzhi/Article/program/net/201311/12488
和转发机制
传输事件接受池以便对事件作出响应
System.Web.Mail 命名空间包含了与 CDOSYS 组件交互从而创建和发送信息的类。
使用互联网信息服务(IIS)和 SMTP 服务
为了能从应
转自cnbeta 新闻来源:wbpluto.com Windows 7 正式版本 7600.16385 已经确定,但今天,网上惊爆 Windows 7 有新版本 7600.16399 了。我看到这条新闻的第一感觉就是“不可能”,于是马上点击下载,大小只有16MB,原来是几个更新程序。Windows 更新程序所更新的文件版本号都会比原文件的版本号要高,因此说 Windows 7 有了新版本只是吸引眼球的手法罢了。 众所周知,Windows 7 正式版已经于7月22日 RTM,即送厂压制光盘,因此不可能再有新版本的ISO发布。经过前所未有大规模的内部和公开测试,Windows 7 的可用性和完善程度已经得到了很好的证明,即使送厂压盘的这个版本有问题,也不会是特别严重的问题,微软只可能通过更新程序的方式进行修补,这样不会使生 产线受到打击。此次泄露出来的8个更新程序即属于这种情况,这8个更新程序分别更新了以下组件:Windows6.1-KB123334 IE加载项安装程序和初始化程序:IEInstal.exe,版本号16400
Windows6.1-KB123456 记事本:notepad.exe,版本号16399
Windows6.1-KB674103 通用控件:Comctl32.dll,版本号5.82.7600.16400、6.10.7600.16400 CDO组件:cdosys.dll,版本号16400 GDI组件:gdi32.dll,版本号16400 NDIS6.20组件,版本号16400 SXS组件,版本号16400 TCP/IP组件,版本号16400 User API:user32.dll,版本号16400 多用户 Win32 驱动:Win32K.sys,版本号16400 Windows 搜索组件,版本号16400
Windows6.1-KB675605 操作系统引导程序:Bootmgr,版本号16399 硬件抽象层:HAL.dll,版本号16399 操作系统核心:ntoskrnl.exe 和 ntkrnlpa.exe,版本号16399
Windows6.1-KB972636 IE8兼容性视图数据库:iecompat.dll,版本号8.0.7600.16400
Windows6.1-KB973751 Windows 7 安装程序组件,版本号6.1.7600.20497
Windows6.1-KB974039 Windows 搜索组件,版本号16401
Windows6.1-KB974138 字体核心组件:版本号16402 得到更新的组件都是非常核心和常用的组件,编译于7月23日,可见Windows 7 正式版的完成还是非常仓促的,以至于RTM编译仅10天就要制作如此核心,如此大批量的修复程序。由于已经RTM,消费者买到的 Windows 7 将是7月13日编译完成的7600.16385原版镜像,微软可能将这些更新程序提供给OEM,以将其预装到新PC中,晚些时候也会通过 Windows Update 向其他零售版用户提供这些更新,更新之后,操作系统版本不会改变,仍然为7600.16385。大家也不用太担心,这些更新对普通用户的使用不会有太大的影响。
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)