以编程方式使用 mailto 协议注册 Windows 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Register Windows program with the mailto protocol programmatically
提问by Liron Yahdav
How do I make it so mailto:
links will be registered with my program?
如何使mailto:
链接注册到我的程序中?
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".
我从 Google 快速搜索中找到的大多数解决方案都是如何手动执行此操作,但是如果我的程序用户单击按钮(例如“设置为默认电子邮件客户端”),我需要为他们自动执行此操作。
Edit:
编辑:
Removed reference to Delphi, because the answer is independent of your language.
删除了对 Delphi 的引用,因为答案与您的语言无关。
采纳答案by Liron Yahdav
@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.
@Dillie-O:您的回答使我朝着正确的方向前进(我应该期望它只是注册表更改)并且我得到了这个工作。但我会将其标记为答案,因为我将添加一些我在处理此问题时发现的其他信息。
The solution to this question really doesn't matter what programming language you're using, as long as there's some way to modify Windows registry settings.
这个问题的解决方案实际上与您使用的编程语言无关,只要有某种方法可以修改 Windows 注册表设置。
Finally, here's the answer:
最后,这是答案:
- To associate a program with the mailto protocol for all userson 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"
- 要将程序与计算机上所有用户的 mailto 协议相关联,请将 HKEY_CLASSES_ROOT\mailto\shell\open\command 默认值更改为:
“您的程序的可执行文件”“%1” - 要将程序与当前用户的 mailto 协议相关联,请将 HKEY_CURRENT_USER\Software\Classes\mailto\shell\open\command 默认值更改为:
“您的程序的可执行文件”“%1”
The %1 will be replaced with the entire mailto URL. For example, given the link:
%1 将替换为整个 mailto URL。例如,给定链接:
<a href="mailto:[email protected]">Email me</a>
The following will be executed:
"Your program's executable" "mailto:[email protected]"
将执行以下内容:
“您的程序的可执行文件”“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
更新(通过 shellscape 的评论):
从 Windows 8 开始,此方法不再按预期工作。Win8 强制使用以下键:HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associati??ons\URLAssociations\??MAILTO\UserChoice,其中所选应用程序的 ProgID 已散列且无法伪造。这是皇家皮塔饼
回答by Dillie-O
From what I've seen, there are a few registry keys that set the default mail client. One of them being:
据我所知,有一些注册表项可以设置默认邮件客户端。其中之一是:
System Key: [HKEY_CLASSES_ROOT\mailto\shell\open\command]
系统密钥: [HKEY_CLASSES_ROOT\mailto\shell\open\command]
Value Name: (Default)
值名称: (Default)
Data Type: REG_SZ
(String Value)
数据类型:(REG_SZ
字符串值)
Value Data: Mail program command-line
.
价值数据:Mail program command-line
。
I'm not familar with Delphi 7
, but I'm sure there are some registry editing librariesin there that you could use to modify this value.
我不熟悉Delphi 7
,但我确定那里有一些注册表编辑库可用于修改此值。
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).
有些地方列出的不仅仅是这个键,其他的只是这个键,所以你可能需要稍微测试一下才能找到合适的。
回答by Michael Stum
Here is the official Microsoft Solution to programmatically change the default mail client. It's Visual Basic, but I hope the concept is clear from this:
这是以编程方式更改默认邮件客户端的官方 Microsoft 解决方案。它是 Visual Basic,但我希望从这里可以清楚地了解这个概念: