在 C# 中将正文传递给 mailto 时如何尊重空格
How to respect spaces when passing body to an mailto in C#
我有一个模板样式的页面,它构建了一个 mailto link 以在用户默认邮件客户端中打开预构建的电子邮件
我知道默认情况下使用 URLencode 会将空格替换为 +(而 pathencode 将替换为 %20 )- 但是是否可以在尊重内容中的空格的同时传递正文和主题到 mailto? - 例如,在 javascript 中使用 encodeURIComponent(yourMessage) 将保留 "yourmessage" 变量
中的空格
private void SubmitEmail(object sender, RoutedEventArgs e)
{
string txt = "text that is an example";
string rnt = "this is also an example";
string PrioStr = "this is an example, of text that will come from a radio buttons contents";
var yourMessage = txt;
var subject =$"firstvariable:{Rnt} secondvariable:{PrioStr}";
var url =$"mailto:test@test.com.com?subject={HttpUtility.UrlEncode(subject)}&body={HttpUtility.UrlEncode(yourMessage)}";
Process.Start(url);
}
- 这是我已经使用 javascript 构建的东西的转换,它在 javascript 中按预期工作,但是当将逻辑应用于 C# 时,它将 subject/body 中的空格替换为'+' 符号
使用 Uri.EscapeDataString
,这会将空格转义为 %20
。
private void SubmitEmail(object sender, RoutedEventArgs e)
{
string txt = "text that is an example";
string rnt = "this is also an example";
string PrioStr = "this is an example, of text that will come from a radio buttons contents";
var yourMessage = txt;
var subject =$"firstvariable:{Rnt} secondvariable:{PrioStr}";
var url = $"mailto:test@test.com.com?subject={Uri.EscapeDataString(subject)}&body={Uri.EscapeDataString(yourMessage)}";
}
我有一个模板样式的页面,它构建了一个 mailto link 以在用户默认邮件客户端中打开预构建的电子邮件
我知道默认情况下使用 URLencode 会将空格替换为 +(而 pathencode 将替换为 %20 )- 但是是否可以在尊重内容中的空格的同时传递正文和主题到 mailto? - 例如,在 javascript 中使用 encodeURIComponent(yourMessage) 将保留 "yourmessage" 变量
中的空格 private void SubmitEmail(object sender, RoutedEventArgs e)
{
string txt = "text that is an example";
string rnt = "this is also an example";
string PrioStr = "this is an example, of text that will come from a radio buttons contents";
var yourMessage = txt;
var subject =$"firstvariable:{Rnt} secondvariable:{PrioStr}";
var url =$"mailto:test@test.com.com?subject={HttpUtility.UrlEncode(subject)}&body={HttpUtility.UrlEncode(yourMessage)}";
Process.Start(url);
}
- 这是我已经使用 javascript 构建的东西的转换,它在 javascript 中按预期工作,但是当将逻辑应用于 C# 时,它将 subject/body 中的空格替换为'+' 符号
使用 Uri.EscapeDataString
,这会将空格转义为 %20
。
private void SubmitEmail(object sender, RoutedEventArgs e)
{
string txt = "text that is an example";
string rnt = "this is also an example";
string PrioStr = "this is an example, of text that will come from a radio buttons contents";
var yourMessage = txt;
var subject =$"firstvariable:{Rnt} secondvariable:{PrioStr}";
var url = $"mailto:test@test.com.com?subject={Uri.EscapeDataString(subject)}&body={Uri.EscapeDataString(yourMessage)}";
}