asp.net 郵件發送

來源:互聯網
上載者:User

剛幫一朋友在asp.net發送郵件 故分享一下!

 

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Mail;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
    
            MailMessage mailMsg;

            //建立郵件訊息
            mailMsg = new MailMessage();
            //發送地址
            mailMsg.From = txtFromEmail.Text.Trim();
            //接收地址
            mailMsg.To = txtTargetEmail.Text.Trim(); ;
            //設定郵件內文內容的類型式
            mailMsg.BodyFormat = MailFormat.Text;
            //郵件主題
            mailMsg.Subject = txtSubject.Text.Trim();]

          // 建立一個附件對象 
        MailAttachment ma = new MailAttachment(f.Value);//f.value附件完整路徑

            mailMsg.Attachments.Add(ma);

            //郵件內容       
            mailMsg.Body = txtContext.Text.Trim();
            //伺服器端的ip,因為我們用的是本地的虛擬smtp伺服器,所以只需要填寫本地ip地址
            SmtpMail.SmtpServer = "127.0.0.1";

 

///以下三條一般都要加 一般的郵箱伺服器都需要身分識別驗證

            mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //設定驗證使用者名稱(把userName改為你的驗證使用者名稱)
            mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "userName");
            //設定驗證密碼(把pwd改為你的驗證密碼)     //寄件者使用者名稱
            mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "pwd");//郵箱密碼

                           
            SmtpMail.SmtpServer = "smtp.126.com"; //可選擇其他伺服器

            SmtpMail.Send(mailMsg);
 

    }
}

 注意:要設定原生SMTP虛擬伺服器 如上的IP要在設定的授權IP列表中。最終郵件發送時由這個SMTP虛擬伺服器發送的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.