使用jQuery和Web Services发送电子邮件

时间:2017-12-05 17:03:17

标签: c# jquery asp.net web-services

我正在尝试向纯HTML网站添加发送电子邮件功能。我在VS 2017中创建了一个ASP.Net项目,并将html文件添加到其中。在“联系我们”表单的页面上,我添加了jQuery以使用Web服务发送电子邮件。我一直收到错误,不知道如何调试它。我在Web服务中单独测试了该方法并且工作正常,它发送了电子邮件,这意味着我的jQuery中必定存在错误。我需要帮助,看看是否有人能发现我错过的东西。

当我运行它时,我会返回状态代码500.

<section id="contact" class="bg-light">
    <div class="container">
        <div class="row">
            <div class="col-sm-12 ml-sm-auto col-md-10 pt-4">
                <h1>Contact us</h1>

                <form id="sendmail" method="POST" action="">
                    <div class="form-group">
                        <input type="email" class="form-control" id="inputEmail" aria-describedby="emailHelp" placeholder="Email Address">
                    </div>
                    <div class="form-group">
                        <input type="email" class="form-control" id="inputName" aria-describedby="nameHelp" placeholder="First and Last Name">
                    </div>
                    <div class="form-group">
                        <input type="email" class="form-control" id="inputPhone" aria-describedby="phoneHelp" placeholder="Contact Number">
                    </div>
                    <div class="form-group">
                        <label for="inputMsg">Feedback/Questions</label>
                        <textarea class="form-control" id="inputMsg" rows="3"></textarea>
                    </div>
                    <button type="submit" id="btnSubmit" class="btn btn-primary">Submit</button>
                </form>
            </div>
        </div>
    </div>
</section>

<script type="text/javascript">
    $("#btnSubmit").click(function () {
        debugger
        $("#sendmail").validate();

        if ($("#sendmail").valid()) {
            $.ajax({
                type: "POST",
                url: "/services/easg.asmx/SendEmail",
                cache: false,
                contentType: "application/json; charset=utf-8",
                data: "{ 'Msg': '" + $("#inputMsg").val() + "', " + "'To': 'someone@somewhere.com'," + "'From': '" + $("#inputEmail").val() + "'," + "'Name': '" + $("#inputName").val() + "'," + "'Subject': 'Guide Feedback'," + "'Phone': '" + $("#inputPhone").val() + "'" + "}",
                dataType: "json",
                complete: function (transport) {
                    alert(transport.status);
                    if (transport.status == 200) {
                        alert('Your message was sent; thank you for your feedback');
                    }
                    else {
                        alert("Failed to send feedback; please try again later. Status Code: " + transport.status);
                    }
                }
            });
        }
        //so that the page doesn't post back
        return false;
    });
</script>

网络服务:

namespace EAStyleGuide
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]

    public class easg : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]

        private void SendEmail(string Msg, string To, string From, string Name, string Subject, string Phone)
        {
            using (var message = new MailMessage())
            {
                message.From = new MailAddress(From);
                message.To.Add(new MailAddress(To));
                message.Subject = Subject;
                message.Body = GetMailBody(From, Msg, Phone, Name);
                message.Priority = MailPriority.Normal;
                message.IsBodyHtml = false;

                SmtpClient sc = new SmtpClient("mailrelay.blah.com");
                sc.Credentials = new NetworkCredential();

            try
            {
                sc.Send(message);
            }
            catch (SmtpFailedRecipientsException FRE)
            {
            }
            catch (SmtpException smtpEx)
            {
            }
            catch (Exception generalEx)
            {
            }
        }
        return;
    }

项目根目录下有一个“Services”文件夹,其中包含easg.asmx。

1 个答案:

答案 0 :(得分:1)

经过一些调试后,由于对上述问题的评论...

  

respnseText是&#34;未知的网络方法SendEmail。参数名称:methodName&#34;

这意味着框架根据正在发出的请求根本无法找到Web方法。仔细观察,Web方法似乎仅限于GET请求:

[ScriptMethod(UseHttpGet = true)]

但是,您正在发出POST请求:

type: "POST",

您需要更改其中一个。这取决于你。