国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#簡單實(shí)現(xiàn)在網(wǎng)頁上發(fā)郵件的案例

2019-10-29 21:30:35
字體:
供稿:網(wǎng)友
本文分享一個(gè)C#利用SMTP發(fā)送郵件的案例,提供了前后臺代碼,方便大家學(xué)習(xí)。
 

1.前端HTML使用了Jquery,大家如果做演示不要忘記引入Jquery的庫

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>  <title></title>  <script src="jquery-1.8.0.min.js" type="text/javascript"></script>  <script type="text/javascript">    function sendemail() {      var smtp = $('#txtSmtp').val();      var content = $('#txtContent').val();      var      var from = $('#txtFrom').val();      var to = $('#txtTo').val();      $.post("Handler.ashx", { 'smtp': smtp, 'content': content,'title':title, 'from': from, 'to': to },    function (data) {      var n = eval('(' + data + ')');      if (n.success) {        alert(n.message);      }    });        }  </script></head><body>  <table>    <tr><td>smtp:</td>      <td><input type="text" id = "txtSmtp" value="Smtp Server" />      </td>    </tr>        <tr><td>from addr:</td>      <td><input type="text" id = "txtFrom" value="xxx@xxx.com" />      </td>    </tr>    <tr><td>to addr:</td>      <td><input type="text" id = "txtTo" value="xxx@xxx.com" />      </td>    </tr>    <tr><td>title:</td>      <td><input type="text" id = "txtTitle" value="title" />      </td>    </tr>    <tr><td>content:</td>      <td><input type="text" id = "txtContent" value="Content" />            </td>    </tr>    <tr>      <td>        <input type="button" id="btnSend" value="send" onclick="sendemail()"/>      </td>    </tr>  </table></body></html>

2.后臺代碼是一般處理類 ashx,供前臺異步調(diào)用

<%@ WebHandler Language="C#" class="Handler" %>using System;using System.Web;using Utility;public class Handler : IHttpHandler {    public void ProcessRequest (HttpContext context)  {    context.Response.ContentType = "text/plain";    string smtp = HttpContext.Current.Request.Form["smtp"].ToString();    string    string content = HttpContext.Current.Request.Form["content"].ToString();    string from = HttpContext.Current.Request.Form["from"].ToString();    string to = HttpContext.Current.Request.Form["to"].ToString();            try    {      EmailClient emailClient = new EmailClient(smtp);// localhost::25      emailClient.SendEmail(from, to, title, content);      System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();      System.Collections.Generic.Dictionary<string, object> d = new System.Collections.Generic.Dictionary<string, object>();      d.Add("message", "success");      d.Add("success", true);      context.Response.Write(jss.Serialize(d));    }    catch (Exception ex)    {      System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();      System.Collections.Generic.Dictionary<string, object> d = new System.Collections.Generic.Dictionary<string, object>();      d.Add("message", ex.Message);      d.Add("success", true);      context.Response.Write(jss.Serialize(d));    }            }   public bool IsReusable {    get {      return false;    }  }}

3.最后是用到的SMTP輔助類

public class EmailClient  {    private string smtpServer;    private string senderAddress;        public EmailClient(string smtpServer)    {      this.smtpServer = smtpServer;      this.senderAddress = string.Empty;    }   public void SendEmail(string fromAddress, string toAddress, string subject, string messageBody)    {      SmtpClient smtp = new SmtpClient(smtpServer);      MailMessage email = new MailMessage();      email.From = new MailAddress(fromAddress);      email.To.Add(toAddress);      email.Subject = subject;      email.Body = messageBody;      smtp.Send(email);    }}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泉州市| 南部县| 湘阴县| 宜兰市| 昭苏县| 安顺市| 马尔康县| 孟连| 桃源县| 蒙自县| 容城县| 奈曼旗| 运城市| 高密市| 临江市| 土默特右旗| 马尔康县| 邵武市| 白银市| 云南省| 龙井市| 嵊泗县| 新闻| 镇平县| 冕宁县| 公安县| 四平市| 通辽市| 荔波县| 冷水江市| 六盘水市| 富顺县| 信宜市| 仙居县| 高雄市| 贺州市| 宁城县| 阿坝县| 沙雅县| 泽州县| 呼和浩特市|