1

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
                    );
                }
            }
        }
    }
}
Fikkwix
  • 167
  • 3
  • 10

1 Answers1

0

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:.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • @Fik: need more help? If not, please accept the answer if it was helpful to you. – Patrick Hofman Mar 17 '15 at 11:13
  • Thanks ! How could I handle it if I'm running under Linux OS ? Or if the user has uninstall IE ? How could I call Firefox or Chrome ? Or Linux browser ? – Fikkwix Mar 17 '15 at 14:36
  • It works with firefox, I assume it will work with IE like you answer, I hope it will work with Chrome 'cause it launch when I try to launch Chrome via the Commande Prompt. – Fikkwix Mar 17 '15 at 14:42
  • @Fikkwix: Just put in another browser exe as first parameter. You might need to do some work to actually find that exe. – Patrick Hofman Mar 17 '15 at 14:58