C#简单实现在网页上发邮件的案例
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# 利用ICSharpCode.SharpZipLib实现在线压缩和解压缩
栏 目:C#教程
下一篇:利用C#实现网络爬虫
本文标题:C#简单实现在网页上发邮件的案例
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/6611.html
您可能感兴趣的文章
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10C#停止线程的方法
- 01-10WinForm实现仿视频播放器左下角滚动新闻效果的方法
- 01-10C#实现清空回收站的方法
- 01-10C#实现读取注册表监控当前操作系统已安装软件变化的方法
- 01-10C#实现多线程下载文件的方法
- 01-10C#实现Winform中打开网页页面的方法
- 01-10C#实现远程关闭计算机或重启计算机的方法
- 01-10C#自定义签名章实现方法
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法实例总结
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10SublimeText编译C开发环境设置
- 01-11ajax实现页面的局部加载
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 04-02jquery与jsp,用jquery