【发布时间】:2018-02-20 03:24:37
【问题描述】:
我有使用 HTML 和 asp.net HTTP 处理程序的邮件发送程序。
这是我向 .ashx 页面发送请求的 html 页面
<html>
<form id="ContactForm" action="EmailHandler.ashx" method="post">
Name: <input class="input" type="text" id="contactname" name="contactname" autocomplete="on" required/>
Email <input class="input" type="email" id="contactemail" name="contactemail" autocomplete="on" required />
Comments<textarea cols="50" rows="5" id="contactmessage" name="contactmessage" required></textarea>
<input type="submit" id="emailsubmit" name="emailsubmit" value="Send" />
</form>
</html>
这是我的 httphandler (EmailHandler.ashx)
public class EmailHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string name = context.Request.Form["contactname"];
string email = context.Request.Form["contactemail"];
string content = context.Request.Form["contactmessage"];
try
{
SendEmail(name, email, content);
}
catch (Exception)
{
*******
}
}
public bool IsReusable { get { return false; } }
public void SendEmail(string name, string emailid, string emailcontent)
{
SmtpClient serverobj = new SmtpClient();
MailMessage msgobj = new MailMessage();
msgobj.To.Add("abc@ymail.com");
msgobj.Subject = "Its From Guest";
msgobj.Body = emailcontent;
msgobj.IsBodyHtml = true;
msgobj.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
serverobj.Send(msgobj);
}
现在如何处理来自这个处理程序的响应。
如果邮件发送成功,如何将成功信息反映到同一个html页面?
如果出现异常,如何处理异常或异常消息?
【问题讨论】:
-
查看 HttpContext 类 (msdn.microsoft.com/en-us/library/system.web.httpcontext.aspx),看看是否没有任何有用的地方可以放置您的通知/错误消息。