我正在WinRT
创建 Windows 8.1商店应用。
我无法使用逗号或半冒号分隔每封电子邮件,使用mailto uri创建一个包含多个收件人的新邮件,两者都给出了同样的错误。
Invalid URI: The hostname could not be parsed.
mailto字符串看起来像这样
"mailto:username@mail.com,username2@mail.com"
"mailto:username@mail.com,username2@mail.com,"
"mailto:username@mail.com, username2@mail.com"
"mailto:username@mail.com;username2@mail.com"
"mailto:username@mail.com;username2@mail.com;"
"mailto:username@mail.com; username2@mail.com"
我已经尝试了所有这些变体,所有这些都会在新手uri时给我同样的错误,就像这样。
var uri = new Uri(string.Format("mailto:{0}", mails));
我不知道我做错了什么,或者如果没有实施,为什么它不会?
我创建了一些单元测试,看看是否有任何变化可行,但没有..
[TestClass]
public class UriMailToTest
{
private Uri CreateMailToUri(string mail)
{
if (string.IsNullOrEmpty(mail)) throw new ArgumentNullException("mail");
var uriMailTo = string.Format("mailto:{0}", mail);
return new Uri(uriMailTo);
}
[TestMethod]
public void CreateMailToUriTest1()
{
const string mailto = "username@mail.com";
var uri = CreateMailToUri(mailto);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest2()
{
const string mailto = "username@mail.com,username2@mail.com";
var uri = CreateMailToUri(mailto);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest3()
{
const string mailto = "username@mail.com,username2@mail.com,";
var uri = CreateMailToUri(mailto);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest4()
{
const string mailto = "username@mail.com;username2@mail.com";
var uri = CreateMailToUri(mailto);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest5()
{
const string mailto = "username@mail.com;username2@mail.com;";
var uri = CreateMailToUri(mailto);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest6()
{
const string mailto = "username@mail.com, username2@mail.com";
var uri = CreateMailToUri(mailto);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest7()
{
const string mailto = "username@mail.com; username2@mail.com";
var uri = CreateMailToUri(mailto);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest8()
{
var mails = new[] { "username@mail.com", "username2@mail.com"};
var mailto = mails.Select(WebUtility.UrlEncode).Aggregate((c, n) => string.Format("{0},{1}", c, n));
var uri = CreateMailToUri(mailto);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest9()
{
var mails = new[] { "username@mail.com", "username2@mail.com" };
var mailto = mails.Select(WebUtility.UrlEncode).Aggregate((c, n) => string.Format("{0};{1}", c, n));
var uri = CreateMailToUri(mailto);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest10()
{
var mails = new[] { "username@mail.com", "username2@mail.com" };
var mailto = mails.Aggregate((c, n) => string.Format("{0},{1}", c, n));
var urlEncodedMailTo = WebUtility.UrlEncode(mailto);
var uri = CreateMailToUri(urlEncodedMailTo);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest11()
{
var mails = new[] { "username@mail.com", "username2@mail.com" };
var mailto = mails.Aggregate((c, n) => string.Format("{0};{1}", c, n));
var urlEncodedMailTo = WebUtility.UrlEncode(mailto);
var uri = CreateMailToUri(urlEncodedMailTo);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest12()
{
var mails = new[] { "username@mail.com", "username2@mail.com" };
var mailto = mails.Select(WebUtility.UrlEncode).Aggregate((c, n) => string.Format("{0}, {1}", c, n));
var uri = CreateMailToUri(mailto);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest13()
{
var mails = new[] { "username@mail.com", "username2@mail.com" };
var mailto = mails.Select(WebUtility.UrlEncode).Aggregate((c, n) => string.Format("{0}; {1}", c, n));
var uri = CreateMailToUri(mailto);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest14()
{
var mails = new[] { "username@mail.com", "username2@mail.com" };
var mailto = mails.Aggregate((c, n) => string.Format("{0}, {1}", c, n));
var urlEncodedMailTo = WebUtility.UrlEncode(mailto);
var uri = CreateMailToUri(urlEncodedMailTo);
uri.Should().NotBeNull();
}
[TestMethod]
public void CreateMailToUriTest15()
{
var mails = new[] { "username@mail.com", "username2@mail.com" };
var mailto = mails.Aggregate((c, n) => string.Format("{0}; {1}", c, n));
var urlEncodedMailTo = WebUtility.UrlEncode(mailto);
var uri = CreateMailToUri(urlEncodedMailTo);
uri.Should().NotBeNull();
}
}
使用Windows Key + R(运行)键入mailto:username@mail.com; username2@mail.com效果很好,我只是无法创建具有多个收件人的Uri对象...
根据mailto:Protocol @ msdn我应该能够将mailto协议与多个收件人一起使用。
语法
的mailto:sAddress [sHeaders]
令牌
sAddress
One or more valid e-mail addresses separated by a semicolon. You must use Internet-safe characters, such as %20 for the space character.
sHeaders
Optional. One or more name-value pairs. The first pair should be prefixed by a "?" and any additional pairs should be prefixed by a "&". The name can be one of the following strings.
subject
Text to appear in the subject line of the message.
body
Text to appear in the body of the message.
CC
Addresses to be included in the "cc" (carbon copy) section of the message.
BCC
Addresses to be included in the "bcc" (blind carbon copy) section of the message.
答案 0 :(得分:2)
有一个黑客,使用HyperLinkButton
(对不起,这是一个肮脏的黑客):
hyperlinkbutton
,XAmlReader
AutomationPeer
,点击
var uriString = "mailto:username@mail.com,username2@mail.com";
string xamlString = "<HyperlinkButton "
+ "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" "
+ "NavigateUri=\"" + uriString + "\"/>";
var c = (HyperlinkButton)XamlReader.Load(xamlString);
new HyperlinkButtonAutomationPeer(c).Invoke();
答案 1 :(得分:0)
尝试在主机名之后添加2个正斜杠:mailto://
答案 2 :(得分:-4)
cant you follow this code?
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
email.To.Add("abcd@mail.com");
email.CC.add("abcd1@mail.com");
email.CC.add("abcd2@mail.com");
email.CC.add("abcd2@mail.com");