實現Email傳送

來源:互聯網
上載者:User

 

using System.Web.Mail;
using System.IO;

        private void btnSend_Click(object sender, System.EventArgs e)
        {
            //分別取得郵件的收信人的地址、發信人的地址、抄送、主題、內容等資訊
            string strTo        = tbTo.Text;
            string strFrom        = tbFrom.Text;
            string strPwd        = tbPwd.Text;
            string strCopyTo    = tbCopyTo.Text;
            string strSubject    = tbSubject.Text;
            string strBody        = tbBody.Text;

            try
            {
                MailMessage ms=new MailMessage();

                ms.To            = strTo;            //收信人的地址
                ms.From            = strFrom;            //發信人的地址
                ms.Cc            = strCopyTo;        //抄送
                ms.Subject        = strSubject;        //主題
                ms.BodyFormat    = MailFormat.Html;    //本文格式html/text
                ms.Body            = strBody;            //本文

                string strPathOfAttachFile = "";    //初始化附件
                //如果有附件則上傳
                HttpPostedFile hpPFile = AttachFile.PostedFile;        //獲得上傳檔案的訪問
                if(hpPFile.FileName != "")
                {
                    //有附件,則上傳到Temp目錄中
                    //判斷是否存在Temp目錄,若無,則建立
                    string FolderName = Server.MapPath(".") + "\\Temp";
                    if(Directory.Exists(FolderName) == false)
                        Directory.CreateDirectory(FolderName);
                    //取得檔案名稱(不含路徑)
                    char[] separator = {'\\'};        //separator的值為"\"
                    string[] AFileName  = hpPFile.FileName.Split(separator);
                    string strFileName  = AFileName[AFileName.Length-1];
                    strPathOfAttachFile    = Server.MapPath(".")+"\\Temp\\"+strFileName;
                    hpPFile.SaveAs(strPathOfAttachFile);
                    //添加附件
                    ms.Attachments.Add(new MailAttachment(strPathOfAttachFile));

                }

                //從發信人的地址計算出郵件伺服器
                string[] strTemp = strFrom.Split('@');
                string strPartOfSmtpServer  = strTemp[strTemp.Length-1];
                string strSmtpServer = "smtp." + strPartOfSmtpServer;

                ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//value=0 No Check    value=1 Basic Check      Value=2 Exchage Check
                ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", strFrom); //發信人的郵箱地址                
                ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", strPwd);//驗證資訊
                SmtpMail.SmtpServer=strSmtpServer;    //郵件伺服器ip或網域名稱

                SmtpMail.Send(ms);            //發送    


                //清除控制項中的內容
                tbTo.Text        = "";
                tbCopyTo.Text    = "";
                tbSubject.Text    = "";
                tbBody.Text        = "";
                //刪除Temp目錄中的附件
                if(File.Exists(strPathOfAttachFile) == true)
                    File.Delete(strPathOfAttachFile);


                //確認郵件發送成功
                string strScript = "<script>alert('郵件發送成功!')</script>";
                if (! Page.IsStartupScriptRegistered("Alert"))
                {    
                    Page.RegisterStartupScript("Alert", strScript);
                }

            }
            catch
            {
                string strScript = "<script>alert('郵件發送失敗!')</script>";        
                if (! Page.IsStartupScriptRegistered("Alert"))
                {    
                    Page.RegisterStartupScript("Alert", strScript);
                }

            }

        }

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.