1. 程式人生 > >jquery ajax 發送郵件例子

jquery ajax 發送郵件例子

tex get 提交 class rem app tostring 發送郵件 col

                       <div class="form">
                        <dl>
                            <dt>您的稱呼<small>(必填)</small></dt>
                            <dd><input id="name" type="text" name="name" class="formText" require></dd>
                        </dl>
                        <dl>
                            <dt>您的郵箱<small>(必填)</small></dt>
                            <dd><input id="
email" type="text" name="email" class="formText" require></dd> </dl> <dl> <dt>聯系電話</dt> <dd><input id="phone" type="text" name="phone" class="formText"></dd> </dl> <dl> <dt>您的想法/意見<small>(必填)</small></dt> <dd><textarea id="
message" name="message" class="formText"></textarea></dd> </dl> <div class="formBtn" onclick="btnSend()">發送留言</div> <script> //提交數據 function btnSend() { $(
"#txtMsg").text(""); var name = $("#name").val();//您的稱呼 var email = $("#email").val();//您的郵箱 var phone = $("#phone").val();//聯系電話 var message = $("#message").val();//您的想法 if (name.length == 0) { alert("稱呼不能為空!"); return; } if (email.length == 0) { alert("郵箱不能為空!"); return; } var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; if (!myreg.test(email)) { alert(請輸入有效的E_mail!); return; } if (message.length < 20) { alert("意見不能不能少於20個字符!"); return; } $(".formBtn").val("正在發送,請稍等!"); var postData = "name=" + name + "&email=" + email + "&phone=" + phone + "&message=" + message; //向後臺發送處理數據 $.ajax({ type: "POST", //用POST方式傳輸 dataType: "text", //數據格式:JSON url: /Email.ashx, //目標地址 data: postData, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("發送失敗,請聯系在線客服!"); }, success: function (msg) { $(".formBtn").val("發送留言"); alert(msg); } }); } </script> </div>

Email.ashx

<%@ WebHandler Language="C#" Class="Email" %>

using System;
using System.Web;
using Ky.Common;


public class Email : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        string name = KYRequest.GetFormString("name");//您的稱呼
        string email = KYRequest.GetFormString("email");//您的郵箱
        string phone = KYRequest.GetFormString("phone");//聯系電話
        string message = KYRequest.GetFormString("message");//您的想法

        if (string.IsNullOrEmpty(name))
        {
            context.Response.Write("您的稱呼為空");
            return;
        }
        if (string.IsNullOrEmpty(email))
        {
            context.Response.Write("您的郵箱為空");
            return;
        }
        if (string.IsNullOrEmpty(message))
        {
            context.Response.Write("想法/意見為空");
            return;
        }
        string body = "時間:" + DateTime.Now + "<br>IP:" + KYRequest.GetIP() + "<br>稱呼:" + name + "<br>郵箱:" + email + "<br>電話:" + phone + "<br>想法/意見:" + message;

        try
        {
            string wwwEmail = System.Configuration.ConfigurationManager.AppSettings["wwwEmail"].ToString();
            SendMail.Send(wwwEmail, "【郵件助手】您的網站收到留言" + DateTime.Now, body);
            context.Response.Write("發送成功,感謝您的支持。");
        }
        catch
        {
            context.Response.Write("Web.config未設置接收郵箱,發送失敗");
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

jquery ajax 發送郵件例子