Unable to create a new mail with multiple recipien

2019-05-03 08:15发布

问题:

I am using creating a Windows 8.1 Store App in WinRT.

I am unable to create a new mail with multiple recipients with the mailto uri by separating each email with either comma or semi colon, both gives me the same error.

Invalid URI: The hostname could not be parsed.

the mailto strings looks like this

"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"

I have tried all these variants an all give me the same error when newing up the uri, like this.

var uri = new Uri(string.Format("mailto:{0}", mails));

I have no idea what I am doing wrong, or in case its not implemented why it wouldn't be?

I created a few unit tests to see if any variations would work, but no..

[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();
    }
}

with Windows Key + R (Run) typing in mailto:username@mail.com;username2@mail.com works great, I'm just not able to create an Uri object with multiple recipients...

According to the mailto:Protocol @ msdn i should be able to use the mailto protocol with multiple recipients.

Syntax

mailto:sAddress[sHeaders]

Tokens

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.

回答1:

There is a hack, using the HyperLinkButton (sorry, this is a dirty hack) :

  1. Load an hyperlinkbutton using a XAmlReader,
  2. Retrieve its AutomationPeer,
  3. Launch a click

      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();
    


回答2:

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");