我正在尝试将asp.net mvc 5中的: / .
字符发送到API控制器端点,但是当我尝试包含某些字符的内容时它会失败。例如,我无法发送message:hi
,我必须将其更改为message_hi
才能使其正常运行。
我正在尝试使用Exchange发送电子邮件,并且正文(包含网址和其他信息)将无法通过。
我的API控制器:
[Route("send/{adress}/{subject}/{body}")]
public void SendEmail(string adress, string subject, string body)
{
Office365MailSender ms = new Office365MailSender();
EmailDto email = new EmailDto(adress, subject, body);
ms.Send(email);
}
从我的应用程序调用上述端点:
public static async Task<string> SendMail(IPhoneCall phoneCall)
{
var email = new EmailEntity(phoneCall);
using (var client = new HttpClient())
{
var uri = new Uri("http://url/email/send/" + email.Recipient + "/" + email.Title + "/" + email.body);
var msg = await client.GetAsync(uri);
}
return "Email Sent";
}
uri
变量的值的示例是:
http://url/email/send/myemail@outlook.com/Hello There/Hi,\nThis is a url you can use for stuff: https://thisisit.com. \n Thanks bye.
在我发送之前,我已经在身体上尝试了HttpUtility.UrlEncode
,但这没有做任何事情。有谁知道如何发送包含这些类型字符的字符串?
答案 0 :(得分:1)
我建议您使用POST动词来发送消息正文。所以你可以从写一个视图模型开始:
public class MailMessageViewModel
{
public string Address { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
您的Web API操作将作为参数:
[Route("send")]
[HttpPost]
public IHttpActionResult SendEmail(MyViewModel model)
{
Office365MailSender ms = new Office365MailSender();
EmailDto email = new EmailDto(model.Address, model.Subject, model.Body);
ms.Send(email);
return this.Ok();
}
然后你可以像这样调用:
var email = new EmailEntity(phoneCall);
using (var client = new HttpClient())
{
var uri = new Uri("http://url/email/send");
var content = new StringContent(
JsonConvert.SerializeObject(new
{
Address = email.Recipient,
Subject = email.Title,
Body = email.body,
}),
UnicodeEncoding.UTF8,
"application/json");
var msg = await client.PostAsync(uri, content);
}