直播中
namespace Test
{
/// <summary>
/// 郵件發(fā)送例子
/// </summary>
public class SendMailSample : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DropDownList ddlPriority;
protected System.Web.UI.WebControls.DropDownList ddlHtml;
protected System.Web.UI.WebControls.TextBox txtSmtpHost;
protected System.Web.UI.WebControls.TextBox txtPort;
protected System.Web.UI.WebControls.TextBox txtFrom;
protected System.Web.UI.WebControls.TextBox txtFromName;
protected System.Web.UI.WebControls.TextBox txtTo;
protected System.Web.UI.WebControls.TextBox txtToName;
protected System.Web.UI.WebControls.TextBox txtSubject;
protected System.Web.UI.WebControls.TextBox txtMessage;
protected System.Web.UI.WebControls.RegularExpressionValidator revPort;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvSmtpHost;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvPort;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvFrom;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvTo;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvSubject;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvMessage;
protected System.Web.UI.WebControls.Button btnSendMail;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.RegularExpressionValidator revFrom;
protected System.Web.UI.WebControls.RegularExpressionValidator revTo;
protected System.Web.UI.WebControls.Panel pelMessage;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSendMail.Click += new System.EventHandler(this.btnSendMail_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSendMail_Click(object sender, System.EventArgs e)
{
if(IsPostBack)
{
SMTPMail clsMail = new SMTPMail();
string strSmtpHost = txtSmtpHost.Text;
int intPort = Int32.Parse(txtPort.Text);
string strFrom = txtFrom.Text;
string strFromName = txtFromName.Text;
string strTo = txtTo.Text;
string strToName = txtToName.Text;
SMTPMail.Prioritys Priority = SMTPMail.Prioritys.NORMAL;
bool bolHtml;
string strSubject = txtSubject.Text;
string strMessage = txtMessage.Text;
if(ddlPriority.SelectedItem.Text == "默認(rèn)")
{
Priority = SMTPMail.Prioritys.NORMAL;
}
else if(ddlPriority.SelectedItem.Text == "高")
{
Priority = SMTPMail.Prioritys.HIGH;
}
else if(ddlPriority.SelectedItem.Text == "低")
{
Priority = SMTPMail.Prioritys.LOW;
}
if(ddlHtml.SelectedItem.Text == "是")
{
bolHtml = true;
}
else
{
bolHtml = false;
}
clsMail.SendMail(strSmtpHost,intPort,strFrom,strFromName,strTo,strToName,Priority,bolHtml,"",strSubject,strMessage);
if(clsMail.ErrorMessage !="")
{
pelMessage.Visible = true;
lblMessage.Text = clsMail.ErrorMessage;
lblMessage.ForeColor = System.Drawing.Color.Red;
}
else
{
pelMessage.Visible = true;
lblMessage.Text = "郵件發(fā)送成功";
lblMessage.ForeColor = System.Drawing.Color.Blue;