郵件發送測試。Codebehind

來源:互聯網
上載者:User
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Blood.Com.ClassLib;

namespace Test
{
    /// <summary>
    /// 郵件發送例子
    /// </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 == "預設")
                {
                    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 = "郵件發送成功";
                    lblMessage.ForeColor = System.Drawing.Color.Blue;
                }
            }
        }
    }
}


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。