如何以编程方式发送电子邮件,就像在 Windows 资源管理器中"Send To Mail Recipient"一样?
How do I programmatically send an email in the same way that I can "Send To Mail Recipient" in Windows Explorer?
ShellExecute()允许我执行简单的shell任务,允许系统负责打开或打印文件。我想采用类似的方法以编程方式发送电子邮件附件。
我不想直接操作Outlook,因为我不想假设用户默认使用哪个电子邮件客户端。我不想直接发送电子邮件,因为我希望用户有机会使用他们喜欢的客户端编写电子邮件正文。因此,当我右键单击一个文件并选择"发送到"->"邮件收件人"时,我真的很想完成Windows资源管理器所做的事情。
我正在寻找一个C++解决方案。
这是我的MAPI解决方案:
#include <tchar.h>
#include <windows.h>
#include <mapi.h>
#include <mapix.h>
int _tmain( int argc, wchar_t *argv[] )
{
HMODULE hMapiModule = LoadLibrary( _T( "mapi32.dll" ) );
if ( hMapiModule != NULL )
{
LPMAPIINITIALIZE lpfnMAPIInitialize = NULL;
LPMAPIUNINITIALIZE lpfnMAPIUninitialize = NULL;
LPMAPILOGONEX lpfnMAPILogonEx = NULL;
LPMAPISENDDOCUMENTS lpfnMAPISendDocuments = NULL;
LPMAPISESSION lplhSession = NULL;
lpfnMAPIInitialize = (LPMAPIINITIALIZE)GetProcAddress( hMapiModule, "MAPIInitialize" );
lpfnMAPIUninitialize = (LPMAPIUNINITIALIZE)GetProcAddress( hMapiModule, "MAPIUninitialize" );
lpfnMAPILogonEx = (LPMAPILOGONEX)GetProcAddress( hMapiModule, "MAPILogonEx" );
lpfnMAPISendDocuments = (LPMAPISENDDOCUMENTS)GetProcAddress( hMapiModule, "MAPISendDocuments" );
if ( lpfnMAPIInitialize && lpfnMAPIUninitialize && lpfnMAPILogonEx && lpfnMAPISendDocuments )
{
HRESULT hr = (*lpfnMAPIInitialize)( NULL );
if ( SUCCEEDED( hr ) )
{
hr = (*lpfnMAPILogonEx)( 0, NULL, NULL, MAPI_EXTENDED | MAPI_USE_DEFAULT, &lplhSession );
if ( SUCCEEDED( hr ) )
{
// this opens the email client with "C:attachment.txt" as an attachment
hr = (*lpfnMAPISendDocuments)( 0, ";", "C:\attachment.txt", NULL, NULL );
if ( SUCCEEDED( hr ) )
{
hr = lplhSession->Logoff( 0, 0, 0 );
hr = lplhSession->Release();
lplhSession = NULL;
}
}
}
(*lpfnMAPIUninitialize)();
}
FreeLibrary( hMapiModule );
}
return 0;
}
您可以在windowsshell中使用标准的"mailto:"命令。它将运行默认的邮件客户端。
下面的C++示例显示了如何调用Windows资源管理器使用的SendTo邮件快捷方式:
http://www.codeproject.com/KB/shell/sendtomail.aspx
您需要实现一个MAPI客户端。这将允许您在向用户发送邮件之前预先填充文档、添加附件等。您可以使用默认邮件存储来使用其默认邮件客户端。
相关文章:
- 如何维护资源管理器项目视图中当前可见的项目列表
- 编译器资源管理器和 GCC 具有不同的输出
- Windows 资源管理器中的图标在使用 resource.rc 时显示 2 个不同的图标
- 编译器资源管理器(godbolt)如何安全地运行代码?
- 从 GUI 文件资源管理器执行程序,并且不生成终端
- Visual Studio 测试资源管理器找不到我的提升测试
- 通过文件资源管理器获取文件夹目录
- Windows 资源管理器打开文件夹还原
- 如何使用C 浏览目录以创建文件资源管理器
- C++ Android 资源管理器从应用内部目录中读取文件
- 停止SetVolumeMountPoint打开文件资源管理器
- 重新启动资源管理器.exe使用 C++
- 资源管理器
- Visual Studio,在(解决方案资源管理器的)某个筛选器内的所有文件中查找/替换文本
- SHOpenFolder并选择资源管理器更换程序的项目
- 如何在 Windows 资源管理器中捕获用户的左键单击?
- 如何将设备隐藏在资源管理器中
- 资源管理器在 TILE 视图模式下不显示 shell 命名空间扩展的项目详细信息
- 模板中的错误可能是错误的设计资源管理器
- 如何以编程方式发送电子邮件,就像在 Windows 资源管理器中"Send To Mail Recipient"一样?