Launching the default mail client in .NET#

If you want to launch the default mail client to create a new email message, it's really simple and there's plenty of samples available:

System.Diagnostics.Process.Start( "mailto:user@example.com?subject=TestSubject&body=TestBody" );

But what if you just want to launch the client and nothing more? You'll have to read the registry (remember that you need the proper RegistryPermission for this) to find the default mail client and launch that executable yourself. Here's a method that will do just that:

/// <summary>
/// Launches the default mail client.
/// </summary>
public void LaunchDefaultMailClient()
{
    // Open the "HKLM\SOFTWARE\Clients\Mail" key.
    Microsoft.Win32.RegistryKey mailKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( @"SOFTWARE\Clients\Mail" );

    // The default mail application is stored in the default value of that key.
    string defaultMailApp = (string)mailKey.GetValue( null );

    if( defaultMailApp != null && defaultMailApp.Length > 0 )
    {
        // Open the subkey of the default mail application and the "shell\open\command" key below that.
        Microsoft.Win32.RegistryKey cmdKey = mailKey.OpenSubKey( defaultMailApp + @"\shell\open\command" );

        // We're now in "HKLM\SOFTWARE\Clients\Mail\<default-mail-app>\shell\open\command".
        // The default value of this key is the command line to start.
        string command = (string)cmdKey.GetValue( null );

        // If there are command arguments, extract them out of the main command string.
        string args = string.Empty;
        if( command.IndexOf( " " ) > 0 )
        {
            args = command.Substring( command.IndexOf( " " ) + 1 );
            command = command.Substring( 0, command.IndexOf( " " ) );
        }

        // Start a new process for the mail application.
        System.Diagnostics.Process.Start( command, args );
    }
}

Friday, November 26, 2004 11:31:29 AM (Romance Standard Time, UTC+01:00)
This is the same conclusion as I came to in the end. That is, read it out from the registry. The only problem with your code, however, is the parsing and separation of the executable path and the arguments. If the executable path contains spaces (as in "%ProgramFiles%\Outlook Express\msimn.exe") and is quoted then you end up splitting the string at the wrong point.
Friday, November 26, 2004 12:51:00 PM (Romance Standard Time, UTC+01:00)
Unless, of course, your main point was just to present a rough guideline. :-)
Friday, November 26, 2004 2:54:22 PM (Romance Standard Time, UTC+01:00)
Yes I know it's not perfect, I already admitted that in a comment on Brad Abrams' blog: http://blogs.msdn.com/brada/archive/2004/09/20/232002.aspx#232059 :-)

But the point was just to show how you could do it, not to hand out the perfect everlasting solution once-and-for-all ;-)
Friday, November 26, 2004 6:35:19 PM (Romance Standard Time, UTC+01:00)
Yup. I realized that my comment could appear to be an unnecessarily picky judgement of an otherwise perfectly valid illustration. Thus my second comment shortly after. :-) Incidentally, the CreateProcess API does all you to pass in a full command-line in a single string, but this version is not exposed from a Process.Start overload. That's what I would have expected from Process.Start(string), but that overload is working with a document filename. It would have been more sensible for the framework authors to call this version something along the lines of StartFromDocument instead. Anyway, no point in discussing this to death. :-)
Friday, November 26, 2004 6:59:59 PM (Romance Standard Time, UTC+01:00)
You're right, it's a shame the CreateProcess API isn't fully supported in managed code, I shouldn't be writing that parsing code like I did in my post. Actually I kinda like the idea of a ProcessStartInfo.Parse method as I pointed out in that comment I mentioned. But I guess it's a feature suggestion for nitpickers ;-)
Tuesday, August 09, 2005 7:13:34 AM (Romance Standard Time, UTC+01:00)
Hey guy! Just respect to you for what you are doing! And for you know exactly the idea what u r talking about! It's very informative and splendid page! A lot of intresting stuff could be found here. Anyhow I wish you luck and all the best in your life and work!
Wednesday, August 10, 2005 8:42:18 AM (Romance Standard Time, UTC+01:00)
Hello!I found here a plenty of useful information for myself! I will visit you soon...
Thursday, March 08, 2007 9:51:52 AM (Romance Standard Time, UTC+01:00)
TODAY I LOOK A GOOD ARCHIVES!!
Thursday, March 08, 2007 9:53:46 AM (Romance Standard Time, UTC+01:00)
In the same calification we can find drugs like!
Thursday, March 08, 2007 9:54:41 AM (Romance Standard Time, UTC+01:00)
Have a great day!
Thursday, March 08, 2007 9:57:26 AM (Romance Standard Time, UTC+01:00)
Hello I like your page is interesting...
10/325 - Vicoprofen - Lortab- Tylenol #3- - Ativan
All Major Medications are available right here at:
Comments are closed.
All content © 2008, Jelle Druyts
On this page

Recent Photos
www.flickr.com
This is a Flickr badge showing public photos from Jelle Druyts. Make your own badge here.
Advertising
Top Picks
Statistics
Total Posts: 344
This Year: 7
This Month: 0
This Week: 0
Comments: 522
Archives
Sitemap
Disclaimer
This is my personal website, not my boss', not my mother's, and certainly not the pope's. My personal opinions may be irrelevant, inaccurate, boring or even plain wrong, I'm sorry if that makes you feel uncomfortable. But then again, you don't have to read them, I just hope you'll find something interesting here now and then. I'll certainly do my best. But if you don't like it, go read the pope's blog. I'm sure it's fascinating.

Powered by:
newtelligence dasBlog 2.0.7226.0

Sign In