程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#簡單實現在網頁上發郵件的案例

C#簡單實現在網頁上發郵件的案例

編輯:關於C語言

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

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 <!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 title = $('#txtTitle').val(); 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="[email protected]" /> </td> </tr> <tr><td>to addr:</td> <td><input type="text" id = "txtTo" value="[email protected]" /> </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,供前台異步調用

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 <%@ 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 title = HttpContext.Current.Request.Form["title"].ToString(); 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輔助類

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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); } }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved