我有使用 HTML 和 asp.net HTTP 处理程序的邮件发送程序。
这是我的 html 页面,它向 .ashx 页面发送请求
<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 页面?
如果出现任何异常,如何处理异常或异常消息?