C#简单实现在网页上发邮件的案例
程序员文章站
2024-01-17 14:36:52
1.前端html使用了jquery,大家如果做演示不要忘记引入jquery的库
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 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="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,供前台异步调用
<%@ 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辅助类
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); } }
上一篇: 为装win8准备着 win8
下一篇: python jinja2模板的使用示例