1.前端HTML使用了Jquery,大家如果做演示不要忘记引入Jquery的库
| smtp: | |
| from addr: | |
| to addr: | |
| title: | |
| content: | |
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 d = new System.Collections.Generic.Dictionary();
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 d = new System.Collections.Generic.Dictionary();
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);
}
}



