发送Email

using System.Web.Mail;



/// <summary>
  /// 发邮件

  /// </summary>
  /// <param name="from"></param>
  /// <param name="fromName"></param>
  /// <param name="to"></param>
  /// <param name="toName"></param>
  /// <param name="subject"></param>
  /// <param name="body"></param>
  /// <param name="SmtpServer"></param>
  /// <param name="SmtpUserName"></param>
  /// <param name="SmtpUserPass"></param>
  public static void SendEmail(string from,string fromName,string to,string toName,
   string subject,string body,string SmtpServer,string SmtpUserName,string SmtpUserPass)
  {
   
   MailMessage myMail = new MailMessage();

   //发信人姓名和地址,形如username@domain.com(昵称)的形式

   myMail.From = from;

   //收件人,多个用逗号隔开,可以用括号附着昵称
   myMail.To = to;
   myMail.Subject = subject;
   myMail.BodyFormat = MailFormat.Html;
   myMail.Body = body;

   //myMail.Fields是一个HASH表,其中成对的储存着MAIL对象的配置值,实际上是这样的,System.Web.Mail.MailMessage是对CDO.MailMessage的包装而已,所以这个FIELDS实际上就是CDO.MailMessage.Fiels
   myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
   myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", SmtpUserName); //set your username here
   myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", SmtpUserPass); //set your password here

   // MailAttachment mailAttach = new MailAttachment(@"E:\Test.Rar");
   // myMail.Attachments.Add(mailAttach);  
   SmtpMail.SmtpServer = SmtpServer;  //smtp邮件服务器

   SmtpMail.Send(myMail);

   #region 下面是使用CDO发邮件的例子, 不好的地方是要引用CDO 才能发

   /*
      using CDO;

      MailMessage mail = new MailMessage();
      mail.To = "michael.rao@corp.elong.com";
      mail.From = "michael.rao@corp.elong.com";
      mail.Subject = "我发的邮件";
      mail.Body = "我发的邮件";
      mail.Fields.Add(CdoConfiguration.cdoSMTPAuthenticate, "1");
      mail.Fields.Add(CdoConfiguration.cdoSendUserName, "michael.rao@corp.elong.com"); //用户名

      mail.Fields.Add(CdoConfiguration.cdoSendPassword, "43434"); //密码

      SmtpMail.SmtpServer = "202.130.239.130";  //smtp邮件服务器

      try
      {
       SmtpMail.Send(mail);
      }
      catch(System.Runtime.InteropServices.COMException e1)
      {
       Response.Write(e1.ToString());
      }
   */
   #endregion
 
  }

posted @ 2005-04-15 15:46  烈马狂生  阅读(747)  评论(0编辑  收藏  举报