我看到两个语法错误。尝试这个:
$("#btnSendMail").click(function(){
$.ajax({
type: "POST",
url: "http://localhost:16189/MailSenderWS.asmx/SendMail",
data: "{'sendermail':'aelectricwala52@gmail.com', 'type':'standalone app', 'body':'success'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg)
},
error: function(msg){
alert(msg);
}
});
});
不久前,我写了一篇关于 asmx webservice 方法并从 Jquery 调用它的文章。这可能会对您有所帮助:http ://www.tomot.de/en-us/article/8/asp.net/how-to-use-jquery-to-call-a-webservice-asmx-method
编辑:根据您的更新,我尝试按 Ajax 调用它,但这对我也不起作用。该服务似乎部署正确,因为您可以在浏览器中调用 URL。我将您的服务添加为服务参考,调用您的方法并使用Fiddler捕获通信。
连接到服务器没有问题,但是服务的响应表明与您的电子邮件帐户相关的身份验证问题:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SendMailResponse xmlns="http://tempuri.org/">
<SendMailResult>
Fail Reason: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. sk1sm6949585pbc.0
</SendMailResult>
</SendMailResponse>
</soap:Body>
</soap:Envelope>
尝试在没有 Web 服务的情况下从本地计算机发送电子邮件,看看是否可以正常工作。之后重新部署,你应该准备好了。
我还建议您更改 Web 服务的 Url,因为既然您已将其发布到网上并且不涉及身份验证,那么有人可能会滥用它。
Edit2:我会告诉你我是如何使用 Fiddler 的。首先,您需要下载并安装它。
创建一个新的 .net 应用程序(例如 ConsoleApplication)。在 中Solution Explorer
,右键单击项目并单击Add Service Reference
。使用网址 http://audiomedia.dev.asentechdev1.com/MailSender.asmx
这会在您的项目中生成一些代码。
启动 Fiddler 并在您的 ConsoleApplication 中执行此代码(根据生成的名称,您需要调整此示例):
ServiceReference1.MailSenderSoapClient client = new ServiceReference1.MailSenderSoapClient();
client.SendMail();
在 Fiddler 中,您将在左侧站点上看到 Web 会话。添加了带有您的网址的新条目。点击它。在底部区域的右侧,您会看到消息
响应已编码,可能需要在检查之前进行解码。单击此处进行转换。
如果您单击该消息并选择 TextView TabPage,您将从您的服务中获得以下结果:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SendMailResponse xmlns="http://tempuri.org/">
<SendMailResult>Success</SendMailResult>
</SendMailResponse>
</soap:Body>
</soap:Envelope>