分享

使用ASP.NET发送HTML格式邮件

 行走在理想边缘 2019-03-25

1.在页面添加一个输入框和一个按钮


复制代码
<%@ 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发送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>
复制代码

2.添加后台事件

复制代码
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相关的命名空间

using System.IO;//文件流相关的命名空间



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发送HTML格式邮件";

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

        
//读取HTML模板,即发送的页面

        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());

        
//关闭文件流

        sr.Close();

        
//替换指定内容,通常为需要变动的内容

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



        
string FromEmail = "XXXX@163.com";

        
string ToEmail = "XXXX@XXX";

        
//Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false

        Jmail.Silent = true;

        
//Jmail创建的日志,前提loging属性设置为true

        Jmail.Logging = true;

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

        Jmail.Charset = "GB2312";

        
//信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/html"即可。

        Jmail.ContentType = "text/html";

        
//添加收件人

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

        Jmail.From 
= FromEmail;

        
//发件人邮件用户名

        Jmail.MailServerUserName = "XXXX";

        
//发件人邮件密码

        Jmail.MailServerPassWord = "XXXX";

        
//设置邮件标题

        Jmail.Subject = Subject;

        
//邮件添加附件,(多附件的话,可以再加一条Jmail.AddAttachment( "c:\\test.jpg",true,null);)就可以搞定了。[注]:加了附件,讲把上面的Jmail.ContentType="text/html";删掉。否则会在邮件里出现乱码。

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

        
//邮件内容

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

        
//Jmail发送的方法,可以修改,此为163邮箱服务器

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

        Jmail.Close();

        
return bSend;

    }


}


复制代码



3.作为模板的HTML页面(JavaScript事件无法触发,CSS样式不能为引用文件)

复制代码
<html xmlns="http://www./1999/xhtml">

    
<head><title>使用ASP.NET发送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/木合达/pic/item/30392594485a1413d21b70c9.jpg" width="139px"/>

        
</form>

    
</body>

</html>
复制代码

   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多