Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create a new mail with multiple recipients with mailto uri

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:[email protected],[email protected]"
"mailto:[email protected],[email protected],"
"mailto:[email protected], [email protected]"
"mailto:[email protected];[email protected]"
"mailto:[email protected];[email protected];"
"mailto:[email protected]; [email protected]"

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 = "[email protected]";
        var uri = CreateMailToUri(mailto);
        uri.Should().NotBeNull();
    }

    [TestMethod]
    public void CreateMailToUriTest2()
    {
        const string mailto = "[email protected],[email protected]";
        var uri = CreateMailToUri(mailto);
        uri.Should().NotBeNull();
    }

    [TestMethod]
    public void CreateMailToUriTest3()
    {
        const string mailto = "[email protected],[email protected],";
        var uri = CreateMailToUri(mailto);
        uri.Should().NotBeNull();
    }

    [TestMethod]
    public void CreateMailToUriTest4()
    {
        const string mailto = "[email protected];[email protected]";
        var uri = CreateMailToUri(mailto);
        uri.Should().NotBeNull();
    }

    [TestMethod]
    public void CreateMailToUriTest5()
    {
        const string mailto = "[email protected];[email protected];";
        var uri = CreateMailToUri(mailto);
        uri.Should().NotBeNull();
    }

    [TestMethod]
    public void CreateMailToUriTest6()
    {
        const string mailto = "[email protected], [email protected]";
        var uri = CreateMailToUri(mailto);
        uri.Should().NotBeNull();
    }

    [TestMethod]
    public void CreateMailToUriTest7()
    {
        const string mailto = "[email protected]; [email protected]";
        var uri = CreateMailToUri(mailto);
        uri.Should().NotBeNull();
    }

    [TestMethod]
    public void CreateMailToUriTest8()
    {
        var mails = new[] { "[email protected]", "[email protected]"};
        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[] { "[email protected]", "[email protected]" };
        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[] { "[email protected]", "[email protected]" };
        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[] { "[email protected]", "[email protected]" };
        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[] { "[email protected]", "[email protected]" };
        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[] { "[email protected]", "[email protected]" };
        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[] { "[email protected]", "[email protected]" };
        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[] { "[email protected]", "[email protected]" };
        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:[email protected];[email protected] 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.
like image 874
furier Avatar asked Nov 26 '22 15:11

furier


1 Answers

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:[email protected],[email protected]";
      string xamlString = "<HyperlinkButton "
         + "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " 
         + "NavigateUri=\"" + uriString + "\"/>";
      var c = (HyperlinkButton)XamlReader.Load(xamlString);
      new HyperlinkButtonAutomationPeer(c).Invoke();
    
like image 169
Jonathan ANTOINE Avatar answered Dec 04 '22 21:12

Jonathan ANTOINE