0

I'm trying to learn UWP/XAML, and am trying to open the default email client when an email link is clicked. Here is what I have

                <TextBlock FontSize="36"     Foreground="Navy">
                    <LineBreak/>
                    <Run Foreground="Navy" FontFamily="Segoe UI Light" FontSize="36">
                        Contact This Person
                    </Run>
                    <LineBreak/>
                    <LineBreak/>
                    <Run Foreground="Navy" FontFamily="Segoe UI Light" FontSize="30">
                        Bob Smith
                    </Run>
                    <LineBreak/>
                    <Run Foreground="Navy" FontFamily="Segoe UI Light" FontSize="30">
                        123 Fake Street
                    </Run>
                    <LineBreak/>
                    <Run Foreground="Navy" FontFamily="Segoe UI Light" FontSize="30">
                        FooBar, Foo
                    </Run>
                    <LineBreak/>

                    <Run Foreground="Navy" FontFamily="Segoe UI Light" FontSize="30">
                       5551212
                    </Run>
                    <LineBreak/>

                    <Hyperlink  NavigateUri="mailto:test@blahblah.com" >Email Bob</Hyperlink>

                </TextBlock>

            </StackPanel>
        </Border>

When I click on the email link, I get a popup asking me if I want to switch to Chrome. Do I need .cs code, or can this all be handled in the XAML?

Amanda_Panda
  • 1,156
  • 4
  • 26
  • 68

2 Answers2

3

By default the MAILTO: protocol is not associated with a mail client.

A client can associate an application application with the protocol by selecting the application in the dialog you mentioned. Doing this will create registry keys at HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\URLAssociations\‌​MAILTO\UserChoice to store the URL association. The value of what needs to be set in these keys depends if you are pre-windows 8 or later as explained here.

So to summarize, you are seeing that dialog because you haven't set a default mail client to handle urls with the mailto: protocol.

Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
2

I had to add a handler for the Click event, then use the Launcher class in code-behind.

XAML:

<Hyperlink Click="ContactHyperlink_Click">
    example@example.com
</Hyperlink>

Code-behind:

private async void ContactHyperlink_Click(Hyperlink sender, Microsoft.UI.Xaml.Documents.HyperlinkClickEventArgs args)
{
    await Windows.System.Launcher.LaunchUriAsync(new Uri("mailto:example@example.com?subject=Message from My App"));
}
Jared
  • 764
  • 7
  • 17