Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register Windows program with the mailto protocol programmatically

Tags:

windows

mailto

How do I make it so mailto: links will be registered with my program?

How would I then handle that event in my program?

Most of the solutions I found from a quick Google search are how to do this manually, but I need to do this automatically for users of my program if they click a button, such as "set as default email client".

#Edit: Removed reference to Delphi, because the answer is independent of your language.

like image 757
Liron Yahdav Avatar asked Aug 01 '08 22:08

Liron Yahdav


2 Answers

@Dillie-O: Your answer put me in the right direction (I should have expected it to just be a registry change) and I got this working. But I'm going to mark this as the answer because I'm going to put some additional information that I found while working on this.

The solution to this question really doesn't depend on what programming language you're using, as long as there's some way to modify Windows registry settings.

Finally, here's the answer:

  • To associate a program with the mailto protocol for all users on a computer, change the HKEY_CLASSES_ROOT\mailto\shell\open\command Default value to:
    "Your program's executable" "%1"
  • To associate a program with the mailto protocol for the current user, change the HKEY_CURRENT_USER\Software\Classes\mailto\shell\open\command Default value to:
    "Your program's executable" "%1"

The %1 will be replaced with the entire mailto URL. For example, given the link:

<a href="mailto:[email protected]">Email me</a> 

The following will be executed:
"Your program's executable" "mailto:[email protected]"

Update (via comment by shellscape):
As of Windows 8, this method no longer works as expected. Win8 enforces the following key: HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associati‌​ons\URLAssociations\‌​MAILTO\UserChoice for which the ProgID of the selected app is hashed and can't be forged. It's a royal PITA.

like image 141
Liron Yahdav Avatar answered Sep 22 '22 13:09

Liron Yahdav


From what I've seen, there are a few registry keys that set the default mail client. One of them is:

System Key: [HKEY_CLASSES_ROOT\mailto\shell\open\command]

Value Name: (Default)

Data Type: REG_SZ (String Value)

Value Data: Mail program command-line.

I'm not familiar with Delphi 7, but I'm sure there are some registry editing libraries there that you could use to modify this value.

Some places list more than this key, others just this key, so you may need to test a little bit to find the proper one(s).

like image 24
Dillie-O Avatar answered Sep 25 '22 13:09

Dillie-O