asp.net发邮件的几种方法汇总

  using System;

  using System.Data;

  using System.Configuration;

  using System.Web;

  using System.Web.Security;

  using System.Web.UI;

  using System.Web.UI.WebControls;

  using System.Web.UI.WebControls.WebParts;

  using System.Web.UI.HtmlControls;

  using System.Net;

  using System.Net.Mail;

  using System.Text;

  public class SendMail

  ...{

  public SendMail()

  ...{

  }

  private string _host;

  /**////

  /// 服务器地址

  ///

  public string Host

  ...{

  get ...{ return _host; }

  set ...{ _host = value; }

  }

  private int _port;

  /**////

  /// 服务器端口

  ///

  public int Port

  ...{

  get ...{ return _port; }

  set ...{ _port = value; }

  }

  private string _smtpUsername;

  /**////

  /// 发送人邮箱用户名

  ///

  public string SmtpUsername

  ...{

  get ...{ return _smtpUsername; }

  set ...{ _smtpUsername = value; }

  }

  private string _sendemail;

  /**////

  /// 发送人邮箱帐号(只接收加密串,发邮件时解密)

  ///

  public string SendEmail

  ...{

  get

  ...{

  return _sendemail;

  }

  set ...{ _sendemail = value; }

  }

  private string _replyToEmail;

  /**////

  /// 回复人邮箱账号

  ///

  public string ReplyToEmail

  ...{

  get ...{ return _replyToEmail; }

  set ...{ _replyToEmail = value; }

  }

  private string _replyUserName;

  /**////

  /// 回复人用户名

  ///

  public string ReplyUserName

  ...{

  get ...{ return _replyUserName; }

  set ...{ _replyUserName = value; }

  }

  private string _getemail;

  /**////

  /// 收件人邮箱帐号

  ///

  public string GetEmail

  ...{

  get ...{ return _getemail; }

  set ...{ _getemail = value; }

  }

  private string _smtpPassword;

  /**////

  /// 发送人邮箱密码(只接收加密串,发邮件时解密)

  ///

  public string SmtpPassword

  ...{

  get ...{ return _smtpPassword; }

  set ...{ _smtpPassword = value; }

  }

  private string _content;

  /**////

  /// 邮件内容

  ///

  public string Content

  ...{

  get ...{ return _content; }

  set ...{ _content = value; }

  }

  private string _title;

  /**////

  /// 邮件标题

  ///

  public string Title

  ...{

  get ...{ return _title; }

  set ...{ _title = value; }

  }

  private string[] _cc = null;

  /**////

  /// 抄送邮箱

  ///

  public string[] cc

  ...{

  get ...{ return _cc; }

  set ...{ _cc = value; }

  }

  private string[] _bcc = null;

  /**////

  /// 密送邮箱

  ///

  public string[] bcc

  ...{

  get ...{ return _bcc; }

  set ...{ _bcc = value; }

  }

  /**////

  ///发送邮件

  ///

  /// 返回是否成功

  public bool Send()

  ...{

  try

  ...{

  MailMessage objMailMessage;

  objMailMessage = new MailMessage(SendEmail, _getemail, _title, _content);

  if (!string.IsNullOrEmpty(_replyToEmail) && !string.IsNullOrEmpty(_replyUserName))

  ...{

  MailAddress reply = new MailAddress(_replyToEmail, _replyUserName);

  objMailMessage.ReplyToList.Add(reply);

  }

  objMailMessage.BodyEncoding = Encoding.GetEncoding(936);

  objMailMessage.IsBodyHtml = true;

  if (cc != null && cc.Length > 0)

  ...{

  foreach (string ccAddress in cc)

  ...{

  objMailMessage.CC.Add(new MailAddress(ccAddress));

  }

  }

  if (bcc != null && bcc.Length > 0)

  ...{

  foreach (string bccAddress in bcc)

  ...{

  objMailMessage.Bcc.Add(new MailAddress(bccAddress));

  }

  }

  SmtpClient client = new SmtpClient(this._host, this._port);

  if (!String.IsNullOrEmpty(this.SmtpUsername) && !String.IsNullOrEmpty(this.SmtpPassword))

  ...{

  client.Credentials = new NetworkCredential(this.SmtpUsername, this.SmtpPassword);

  }

  client.EnableSsl = false;

  client.Send(objMailMessage);

  objMailMessage.Dispose();

  return true;

  }

  catch

  ...{

  return false;

  }

  }

  }