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

2008-02-22 09:27:28来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


"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 only; otherwise, 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 9, 2004
Rating: not yet rated

--------------------------------------------------------------------------------

Welcome 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 2000 (CDOSYS) is a Microsoft messaging component 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 2000 for backwards compatibility, but Windows XP, Windows Server 2003, 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 2000/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 smart host to deliver the message instead. When a smart host is enlisted, all messages are forwarded to it for delivery. You need to have IIS and the SMTP service installed and configured.

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:基于ASP.NET的Web动态控件创建

下一篇:用Asp.net实现基于XML的留言簿之一