gpt4 book ai didi

c++ - 如何以编程方式发送电子邮件,就像我在 Windows 资源管理器中发送 "Send To Mail Recipient"一样?

转载 作者:可可西里 更新时间:2023-11-01 12:43:16 24 4
gpt4 key购买 nike

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;
}

关于c++ - 如何以编程方式发送电子邮件,就像我在 Windows 资源管理器中发送 "Send To Mail Recipient"一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/262451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com