使用jquery ajax,webservice和c#发送电子邮件

时间:2017-07-19 19:31:39

标签: c# jquery ajax web-services

我不知道自己做错了什么。我的代码一直给我这个错误。

  

jquery.js:10254 POST http://localhost:7855/Services/svUsers.asmx/EmailNotification 500(内部服务器错误)

这是我的所有代码

[WebMethod]
public bool EmailNotification(string from,string to,string username)
{
    MailMessage tmail = new MailMessage();
    tmail.From = new MailAddress(from);
    tmail.To.Add(new MailAddress(to));
    StringBuilder builder = new StringBuilder();
    builder.Append(string.Format("Dear {0} <br/><br/>",username));
    builder.Append("How are you john iliya i am testing Sending Email Messages with Webservice...i have faith it would work...");
    builder.Append(("<br/><br/>"));
    builder.Append("<b>Powered By JINGBREEDS.com");
    tmail.Subject = "Testing Of Mail Messages";
    tmail.Body = builder.ToString();
    tmail.IsBodyHtml = true;
    tmail.Priority = MailPriority.Normal;
    var smtp = new SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.Credentials = new System.Net.NetworkCredential("yourun@gmail.com","yourpswd");
    }
    smtp.Send(tmail);
    return true;
}
<input type="text" placeholder="Email Address" name="txtEMF"  class="form-control" />
<input type="text" placeholder="Email Address" name="txtEMT"  class="form-control" />
<input type="button" class="btn-info btn form-control" name="btnSend" value="Send"  />

<script type="text/javascript">
    $(document).ready(function () {
        $("input[name$='btnSend']").click(function () {
            var un = "airmarshalg";
            $.ajax({
                url: "../Services/svUsers.asmx/EmailNotification",
                method: "POST",
                data: { from: $("input[name$='txtEMF']").val(), to: $("input[name$='txtEMT']").val(), username: un },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    if(response.data == true)
                    {
                        alert("Hurray message sent...");
                    }                               
                },
                error: function (err) {
                    console.log(err);
                }
            });
        });
    });
</script>

1 个答案:

答案 0 :(得分:0)

我能够通过在我的网络服务的最顶端取消注释这行代码来解决问题..我的错误是什么  [System.Web.Script.Services.ScriptService]

谢谢你们的代码像魅力hahahah一样使用角度