Search code examples
c#.netmailtolinklabel

Failover when mailto protocol handler is not present


I'm facing a little issue with a mailto link in a label.

In fact I added a label which contains a mailto link, and in a native way the program try to open it with the default program the user has defined.

But actually it doesn't work and raise a Win32Exception when there is no default program for this protocol.

So I force it to open browser, but it doesn't work either...

Here is a sample of my code :

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    // Specify that the link was visited.
    this.linkLabelContactEmail.LinkVisited = true;
    try
    {
        // Open the default program to send an email.
        System.Diagnostics.Process.Start("mailto:contact@mysite.fr");
    }
    catch (Win32Exception)
    {
        // Force opening in a browser
        System.Diagnostics.Process.Start("http://mailto:contact@mysite.fr");
    }
}

But it doesn't work :/ (it works if a default program is link to that protocol)

Does anyone knows how could i fix that problem ? Like forcing add a default protocol to mailto link ?


EDIT :

I've tried this, which worked fine ! But it still doesn't handle a linux browser :/ Moreover it's not .exe under Unix OS, how should I try it ? (I'm aware that firefox is install by default, will it be handle ?)

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    // Specify that the link was visited.
    this.linkLabelContactEmail.LinkVisited = true;

    try
    {
        // Open the default program to send an email.
        System.Diagnostics.Process.Start("mailto:contact@mysite.fr");
    }
    catch (Win32Exception)
    {
        try
        {
            // Force opening in Firefox
            System.Diagnostics.Process.Start("firefox", "mailto:contact@mysite.fr");
        }
        catch (Win32Exception)
        {
            try
            {
                // Force opening in IE
                System.Diagnostics.Process.Start("chrome", "mailto:contact@mysite.fr");
            }
            catch (Win32Exception)
            {
                try
                {
                    // Force opening in IE
                    System.Diagnostics.Process.Start("iexplore", "mailto:contact@mysite.fr");
                }
                catch (Win32Exception)
                {
                    MessageBox.Show(
                        "Vous n'avez aucun programme par défaut de configuré pour envoyer un mail, ni aucun des 3 navigateurs (Firefox, Chrome, Internet Explorer). Nous ne pouvons donc vous aidez à envoyer ce mail...",
                        "Erreur à l'envoi du mail",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning,
                        MessageBoxDefaultButton.Button1
                    );
                }
            }
        }
    }
}

Solution

  • Instead of adding the http:// protocol prefix, you could simply start a browser. Your URL syntax is invalid and I doubt it will ever work (Chrome doesn't for sure, just tested it).

    System.Diagnostics.Process.Start("iexplore", "mailto:contact@mysite.fr");
    

    This code opens Internet Explorer to execute the mailto:.