C#简单实现在网页上发邮件的案例
程序员文章站
2022-05-14 17:37:57
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); } }
下一篇: C#中的静态成员、静态方法、静态类介绍
推荐阅读
-
C#在WinForm中使用WebKit传递js对象实现与网页交互的方法
-
C# 计时周期数(Ticks)在不同数据库上的实现
-
使用Docker在Nginx上运行简单的HTML网页
-
在 Linux 上用 DNS 实现简单的负载均衡的方法
-
SQL Server存储过程在C#中调用的简单实现方法
-
在多台服务器上简单实现Redis的数据主从复制
-
C#中泛型在Class上的实现
-
写了一个简单的html/php上传的网页,在安卓和电脑上都正常,但ipad上都有问题,即图片上传名称都自动变成image.jpg
-
C#中TrimStart,TrimEnd,Trim在javascript上的实现_javascript技巧
-
C#中泛型在Class上的实现