gpt4 book ai didi

c++ - 如何使用 C++ 中的 MAPI 以编程方式将带附件的电子邮件发送给已知收件人? MAPISendMail()

转载 作者:行者123 更新时间:2023-11-30 04:39:19 24 4
gpt4 key购买 nike

This question类似,但未显示如何添加收件人。

我该如何做到这两点?

我们希望为尽可能多的 Windows 平台(从 XP 及更高版本)提供尽可能广泛的支持

我们正在使用 visual studio 2008

本质上我们想发送一封电子邮件:

  • 预填目的地地址
  • 文件附件
  • 主题行

从我们的程序中,让用户能够添加任何信息或取消它。

编辑我正在尝试使用 MAPISendMail()我从顶部附近链接的问题中复制了大部分代码,但我没有收到电子邮件 dlg 框,我从调用中得到的错误返回是:0x000f - “系统找不到指定的驱动器”

如果我注释掉设置收件人的行,它工作正常(当然我没有预先填写收件人)

代码如下:

#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;
LPMAPISENDMAIL lpfnMAPISendMail = NULL;

lpfnMAPIInitialize = (LPMAPIINITIALIZE)GetProcAddress( hMapiModule, "MAPIInitialize" );
lpfnMAPIUninitialize = (LPMAPIUNINITIALIZE)GetProcAddress( hMapiModule, "MAPIUninitialize" );
lpfnMAPILogonEx = (LPMAPILOGONEX)GetProcAddress( hMapiModule, "MAPILogonEx" );
lpfnMAPISendDocuments = (LPMAPISENDDOCUMENTS)GetProcAddress( hMapiModule, "MAPISendDocuments" );
lpfnMAPISendMail = (LPMAPISENDMAIL)GetProcAddress( hMapiModule, "MAPISendMail" );

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
// create the msg. We need to add recipients AND subject AND the dmp file

// file attachment
MapiFileDesc filedesc;
::ZeroMemory(&filedesc, sizeof(filedesc));
filedesc.nPosition = (ULONG)-1;
filedesc.lpszPathName = "E:\\Development\\Open\\testmail\\testmail.cpp";

// recipient(s)
MapiRecipDesc recip;
::ZeroMemory(&recip, sizeof(recip));
recip.lpszName = "QA email";
recip.lpszAddress = "qa@myaccount.com";

// the message
MapiMessage msg;
::ZeroMemory(&msg, sizeof(msg));
msg.lpszSubject = "Test";
msg.nRecipCount = 1; // if I comment out this line it works fine...
msg.lpRecips = &recip;
msg.nFileCount = 1;
msg.lpFiles = &filedesc;

hr = (*lpfnMAPISendMail)(0, NULL, &msg, MAPI_LOGON_UI|MAPI_DIALOG, 0);

if ( SUCCEEDED( hr ) )
{
hr = lplhSession->Logoff( 0, 0, 0 );
hr = lplhSession->Release();
lplhSession = NULL;
}
}
}

(*lpfnMAPIUninitialize)();
}

FreeLibrary( hMapiModule );
}

return 0;
}

最佳答案

糟糕 - 我忘了设置

recip.ulRecipClass = MAPI_TO;

现在效果很好。

关于c++ - 如何使用 C++ 中的 MAPI 以编程方式将带附件的电子邮件发送给已知收件人? MAPISendMail(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2350011/

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