我在asp.net网络应用程序中实现了一个弹出窗口,允许用户选择某些内容并生成电子邮件模板。完成所有必填字段后,用户只需点击一下按钮即可发送电子邮件。
此按钮调用javascript函数,我收集所有用户输入数据,如from,to,subject,body,并希望将此数据发送到我创建的将发送邮件的Web服务。
我不想使用mailto,所以我选择了网络服务。
问题在于我无法发送数据:
$.ajax({
type: 'POST',
url: "Services/MailService.asmx/SendMail",
dataType: "json",
contentType: 'application/json; charset=utf-8',
data: { 'loginName': "'" + loginName + "'", 'fromAddress': "'" + fromAddress + "'", 'toAddress': "'" + toAddress + "'", 'mailSubject': "'" + mailSubject + "'", 'mailBody': "'" + mailBody + "'"},
success: function ()
{
alert("The email was sent successfully!");
},
error: function(data)
{
alert("An error occurred while trying to send the email. " + data.responseText);
}
});
我知道我在data属性中插入的内容是错误的。我尝试了以下内容(一次一个):
data: JSON.stringify({ 'loginName': loginName, 'fromAddress': fromAddress, 'toAddress': toAddress, 'mailSubject': mailSubject, 'mailBody': mailBody }),
data: { 'loginName': loginName, 'fromAddress': fromAddress, 'toAddress': toAddress, 'mailSubject': mailSubject, 'mailBody': mailBody },
data: { loginName: loginName, fromAddress: fromAddress, toAddress: toAddress, mailSubject: mailSubject, mailBody: mailBody },
data: "{ 'loginName': '" + loginName + "', 'fromAddress': '" + fromAddress + "', 'toAddress': '" + toAddress + "', 'maliSubject': '" + mailSubject + "', 'mailBody': '" + mailBody + "' }",
所有这些选项都会给我以下错误:
An error occurred while trying to send the email. {"Message":"Invalid web service call, missing value for parameter: \u0027mailBbody\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
或
An error occurred while trying to send the email. {"Message":"Invalid JSON primitive: loginName.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
我不明白我做错了什么。
Web服务如下所示:
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public void SendMail(string loginName, string fromAddress, string toAddress, string mailSubject, string mailBody)
{
MailMessage mailMsg = new MailMessage(fromAddress, toAddress);
mailMsg.Subject = mailSubject;
mailMsg.Body = mailBody;
string pathToCreate = "~/Upload/" + loginName + "/";
if (Directory.Exists(Server.MapPath(pathToCreate)))
{
if (Directory.GetFiles(Server.MapPath(pathToCreate)).Length > 0)
{
string[] attachedFiles = Directory.GetFiles(Server.MapPath(pathToCreate));
foreach (string a in attachedFiles)
{
Attachment data = new Attachment(a);
mailMsg.Attachments.Add(data);
}
}
}
SmtpClient smtp = new SmtpClient();
smtp.Send(mailMsg);
}
}
请与我分享我该怎么做才能解决这个问题。
谢谢!
答案 0 :(得分:0)
我能够使用以下方式成功发送数据:
data: '{loginName:' + JSON.stringify(loginName) + ', fromAddress:' + JSON.stringify(fromAddress) + ', toAddress:' + JSON.stringify(toAddress) + ', mailSubject:' + JSON.stringify(mailSubject) + ', mailBody:' + JSON.stringify(mailBody) + '}',
我不知道这是不是正确的方式,但它是否有效。