使用jQuery和Web服务发送电子邮件



我正在尝试将发送电子邮件功能添加到纯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>

Web服务:

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;
    }

在项目根部有一个"服务"文件夹。

在对上述问题的评论进行了一些调试之后...

RespnSetext是"未知的Web方法sendemail。参数名称:方法名称"

这意味着该框架无法基于所提出的请求找到Web方法。仔细观察,Web方法似乎仅限于获得请求:

[ScriptMethod(UseHttpGet = true)]

但是,您正在提出邮政请求:

type: "POST",

您需要更改一个或另一个。这取决于您。

相关内容

  • 没有找到相关文章

最新更新