C#简单实现在网页上发邮件的案例
1.前端HTML使用了Jquery,大家如果做演示不要忘记引入Jquery的库
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <scriptsrc="jquery-1.8.0.min.js"type="text/javascript"></script> <scripttype="text/javascript"> functionsendemail(){ varsmtp=$('#txtSmtp').val(); varcontent=$('#txtContent').val(); vartitle=$('#txtTitle').val(); varfrom=$('#txtFrom').val(); varto=$('#txtTo').val(); $.post("Handler.ashx",{'smtp':smtp,'content':content,'title':title,'from':from,'to':to}, function(data){ varn=eval('('+data+')'); if(n.success){ alert(n.message); } }); } </script> </head> <body> <table> <tr><td>smtp:</td> <td><inputtype="text"id="txtSmtp"value="SmtpServer"/> </td> </tr> <tr><td>fromaddr:</td> <td><inputtype="text"id="txtFrom"value="xxx@xxx.com"/> </td> </tr> <tr><td>toaddr:</td> <td><inputtype="text"id="txtTo"value="xxx@xxx.com"/> </td> </tr> <tr><td>title:</td> <td><inputtype="text"id="txtTitle"value="title"/> </td> </tr> <tr><td>content:</td> <td><inputtype="text"id="txtContent"value="Content"/> </td> </tr> <tr> <td> <inputtype="button"id="btnSend"value="send"onclick="sendemail()"/> </td> </tr> </table> </body> </html>
2.后台代码是一般处理类ashx,供前台异步调用
<%@WebHandlerLanguage="C#"class="Handler"%> usingSystem; usingSystem.Web; usingUtility; publicclassHandler:IHttpHandler{ publicvoidProcessRequest(HttpContextcontext) { context.Response.ContentType="text/plain"; stringsmtp=HttpContext.Current.Request.Form["smtp"].ToString(); stringtitle=HttpContext.Current.Request.Form["title"].ToString(); stringcontent=HttpContext.Current.Request.Form["content"].ToString(); stringfrom=HttpContext.Current.Request.Form["from"].ToString(); stringto=HttpContext.Current.Request.Form["to"].ToString(); try { EmailClientemailClient=newEmailClient(smtp);//localhost::25 emailClient.SendEmail(from,to,title,content); System.Web.Script.Serialization.JavaScriptSerializerjss=newSystem.Web.Script.Serialization.JavaScriptSerializer(); System.Collections.Generic.Dictionary<string,object>d=newSystem.Collections.Generic.Dictionary<string,object>(); d.Add("message","success"); d.Add("success",true); context.Response.Write(jss.Serialize(d)); } catch(Exceptionex) { System.Web.Script.Serialization.JavaScriptSerializerjss=newSystem.Web.Script.Serialization.JavaScriptSerializer(); System.Collections.Generic.Dictionary<string,object>d=newSystem.Collections.Generic.Dictionary<string,object>(); d.Add("message",ex.Message); d.Add("success",true); context.Response.Write(jss.Serialize(d)); } } publicboolIsReusable{ get{ returnfalse; } } }
3.最后是用到的SMTP辅助类
publicclassEmailClient { privatestringsmtpServer; privatestringsenderAddress; publicEmailClient(stringsmtpServer) { this.smtpServer=smtpServer; this.senderAddress=string.Empty; } publicvoidSendEmail(stringfromAddress,stringtoAddress,stringsubject,stringmessageBody) { SmtpClientsmtp=newSmtpClient(smtpServer); MailMessageemail=newMailMessage(); email.From=newMailAddress(fromAddress); email.To.Add(toAddress); email.Subject=subject; email.Body=messageBody; smtp.Send(email); } }