打印本文 打印本文  关闭窗口 关闭窗口
在.NET 应用程序中用System.Web.Mail 发送电子邮件
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005/9/10 14:22:51
// </summary>
    [STAThread]
    static void Main(string[] args)
    {
      TestMail.SendAttachment("testuser@codeguru.com",
                              "mstrawmyer@crowechizek.com",
                              "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);
      }
    }
  }
}

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 网上书店的开发中,我们同样可以利用发送电子邮件来向客户反馈书籍信息,为客户提供定单服务等等。

上一页  [1] [2] [3] 



打印本文 打印本文  关闭窗口 关闭窗口