日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

使用ASP.NET發(fā)送HTML格式郵件

 行走在理想邊緣 2019-03-25

1.在頁面添加一個(gè)輸入框和一個(gè)按鈕


復(fù)制代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendEMail.aspx.cs" Inherits="SendEMail" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www./1999/xhtml" >

<head runat="server">

    
<title>使用ASP.NET發(fā)送HTML格式郵件</title>

</head>

<body>

    
<form id="form1" runat="server">

    
<div>

        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

        
<asp:Button ID="Button1" runat="server" Text="SendEmail" OnClick="Button1_Click" /></div>

    
</form>

</body>

</html>
復(fù)制代碼

2.添加后臺(tái)事件

復(fù)制代碼
using System;

using System.Data;

using System.Configuration;

using System.Collections;

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.Text;//StringBuilder相關(guān)的命名空間

using System.IO;//文件流相關(guān)的命名空間



public partial class SendEMail : System.Web.UI.Page

{

    
protected void Page_Load(object sender, EventArgs e)

    
{



    }


    
protected void Button1_Click(object sender, EventArgs e)

    
{

        
string strResult = string.Empty;

        
if (SendEmail(this.TextBox1.Text.Trim()))

        
{

            strResult 
= "Successed!";

        }


        
else

        
{

            strResult 
= "Defeated!";

        }


        Response.Write(
"<script language='javascript'>");

        Response.Write(
"alert('"+strResult+"!');");

        Response.Write(
"</script>");

    }


    
private static bool SendEmail(string strName)

    
{

        
string Subject = "使用ASP.NET發(fā)送HTML格式郵件";

        jmail.Message Jmail 
= new jmail.Message();

        
//讀取HTML模板,即發(fā)送的頁面

        string strPath = System.Web.HttpContext.Current.Server.MapPath("~/a.html");

        
//讀取文件,“System.Text.Encoding.Default”可以解決中文亂碼問題

        StreamReader sr = new StreamReader(strPath, System.Text.Encoding.Default);

        StringBuilder body 
= new StringBuilder();

        body.Append(sr.ReadToEnd());

        
//關(guān)閉文件流

        sr.Close();

        
//替換指定內(nèi)容,通常為需要變動(dòng)的內(nèi)容

        body=body.Replace("<%Message%>", strName);



        
string FromEmail = "XXXX@163.com";

        
string ToEmail = "XXXX@XXX";

        
//Silent屬性:如果設(shè)置為true,JMail不會(huì)拋出例外錯(cuò)誤. JMail. Send( () 會(huì)根據(jù)操作結(jié)果返回true或false

        Jmail.Silent = true;

        
//Jmail創(chuàng)建的日志,前提loging屬性設(shè)置為true

        Jmail.Logging = true;

        
//字符集,缺省為"US-ASCII"

        Jmail.Charset = "GB2312";

        
//信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式發(fā)送郵件, 改為"text/html"即可。

        Jmail.ContentType = "text/html";

        
//添加收件人

        Jmail.AddRecipient(ToEmail, """");

        Jmail.From 
= FromEmail;

        
//發(fā)件人郵件用戶名

        Jmail.MailServerUserName = "XXXX";

        
//發(fā)件人郵件密碼

        Jmail.MailServerPassWord = "XXXX";

        
//設(shè)置郵件標(biāo)題

        Jmail.Subject = Subject;

        
//郵件添加附件,(多附件的話,可以再加一條Jmail.AddAttachment( "c:\\test.jpg",true,null);)就可以搞定了。[注]:加了附件,講把上面的Jmail.ContentType="text/html";刪掉。否則會(huì)在郵件里出現(xiàn)亂碼。

        
//Jmail.AddAttachment( "c:\\test.jpg",true,null);

        
//郵件內(nèi)容

        Jmail.Body = body.ToString().Trim();

        
//Jmail發(fā)送的方法,可以修改,此為163郵箱服務(wù)器

        bool bSend = Jmail.Send("smtp.163.com"false);

        Jmail.Close();

        
return bSend;

    }


}


復(fù)制代碼



3.作為模板的HTML頁面(JavaScript事件無法觸發(fā),CSS樣式不能為引用文件)

復(fù)制代碼
<html xmlns="http://www./1999/xhtml">

    
<head><title>使用ASP.NET發(fā)送HTML格式郵件</title>

    
</head>  

    
<body>

    
<form>

        
<input type="text" id="lbText" value="<%Message%>" /><br />

        
<b>From:</b><href="http://zhoufeng.cnblogs.com/">http://zhoufeng.cnblogs.com/</a><br />

        
<img id="img" src="http://hiphotos.baidu.com/木合達(dá)/pic/item/30392594485a1413d21b70c9.jpg" width="139px"/>

        
</form>

    
</body>

</html>
復(fù)制代碼

   

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多