这个问题类似,但没有显示如何添加收件人。
我该怎么做?
我们希望尽可能多地支持尽可能多的 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;
}