使用 System.Web.Mail 发送 E-Mail

下面是一个Windows控制台应用程序,它显示了如何发送 e-mail。在你不指定 SmtpMail.SmtpServer 属性时,localhost 被用作默认的服务器(此时要在机器上安装 IIS 和 SMTP Service)。你需要确认已经添加了对 System.Web.dll 的引用,因为控制台应用程序不是一个 Asp.NET 应用程序,不会自动添加对 System.Web.dll 的引用。

发送消息的例子

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("testuser@codeguru.com",
                        "mstrawmyer@crowechizek.com",
                        "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 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>   
       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);     
            }
      }
   }
}

发送带附件的消息的例子

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("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);
                }
         }
    }
}

posted on 2006-04-13 09:30  Easy Company  阅读(722)  评论(0编辑  收藏  举报

导航