专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »DotNet » webmail:在.NET 应用程序中用System.Web.Mail 发送电子邮件 »正文

webmail:在.NET 应用程序中用System.Web.Mail 发送电子邮件

来源: 发布时间:星期三, 2008年8月13日 浏览:217次 评论:0


作者:Mark Strawmyer
日期:February 9, 2004

欢迎来到 .NET Nuts & Bolts 栏目。在这个栏目中,我们将探讨怎样在应用中发送电子邮件。这将用到System.Web.Mail 名字空间中的类。

协作数据对象
Windows 2000 协作数据对象 (CDOSYS) 是微软用来创建和发送基于标准的电子邮件信息的消息组件。它是 用与 Windows NT的协作数据对象(CDONTS) 的替代物。 尽管由于向后兼容的原因 CDONTS 已包含在 Windows 2000 中, 但是 Windows XP, Windows Server 2003 以及更高版本均未包含或支持 CDONTS 组件。 所以任何使用 CDONTS 发送消息的应用程序都必须迁移到使用 CDOSYS 上来。它提供了相同的功能,而且易于使用。

除了作为替代物外, CDOSYS 还引入了一些 CDONTS 中没有的功能,如:

向新闻组发送消息的能力
对消息的 MIME 体结构的控制
接收和转发机制
传输事件接受池以便对事件作出响应
System.Web.Mail 命名空间包含了与 CDOSYS 组件交互从而创建和发送信息的类。

使用互联网信息服务(IIS)和 SMTP 服务
为了能从应用程序中利用 CDOSYS 发送电子邮件,您必须确认 IIS 服务列表中已经安装了SMTP 服务。在 Windows 2000/XP中,您可以通过控制面板 -> 添加/删除程序 -> 添加/删除 Windows 组件选项来设置。STMP 服务的任务就是基于配置接收和发送消息。这个服务可以直接投递消息,也可以使用代理服务器来发送消息。当代理服务器已配置时,所有的消息将转发给它以备发送。你必须确保 IIS 和 SMTP 服务正确的安装和配置好。

在投递之前,SMTP 服务使用一个目录结构来保存消息。默认的目录为C:\Inetpub\mailroot。这个文件夹中包含了一些子目录,如:Queue, Drop, Badmail。 如果你无法配置SMTP服务实例以便发送的话,您将可以在目录 C:\Inetpub\mailroot\Queue 中的 *.EML 文件中找到邮件。这个技巧在离线创建邮件时将很有用。

发送消息
正如前面提到的,发送电子邮件将是一件相对简单的事。类 System.Web.Mail.MailMessage class 代表了将要发送的消息。E-mail 消息将由该类的实例来创建。这个类包含了诸如:收件人,发件人和主题等属性来让你控制想要发送的消息。还可以使用类 System.Web.Mail.MailAttachment 的实例创建附件,然后添加到 MailMessage 的 Attachments (附件)集合中。随后该消息将由 类System.Web.Mail.SmtpMail 发送出去。

发送邮件示例代码
下面的 C# 示例代码将包含一个演示如何发送简单电子邮件的 Windows 控制台程序。当没有设置 SmtpMail 的 SmtpServer 属性时,本地机器将为其默认配置。你必须确保添加了针对 System.Web.dll 的引用,因为它是控制台应用程序而不是 ASP.NET 应用。

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("[email protected]",
"[email protected]",
"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 _disibledevent= 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.

翻译心得:

这篇文章介绍了在如何.Net程序中发送电子邮件,包括怎样配置IIS和Smtp服务,怎样发送简单邮件以及如何在应用程序中加以利用的一些想法。对于开发者来说不失为一篇介绍发送电子邮件的好文章。在.NET 网上书店的开发中,我们同样可以利用发送电子邮件来向客户反馈书籍信息,为客户提供定单服务等等。

标签:webmail
0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: