1
This question是類似的,但不顯示如何添加收件人。如何以編程方式將帶有附件的電子郵件發送給使用C++中的MAPI的已知收件人? MAPISendMail()
我該怎麼做?
我們想最廣泛的支持可能是很多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 = "[email protected]";
// 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;
}
我認爲你不需要登錄和初始化的東西。 (* lpfnMAPISendMail)()將獨立工作。 – thomiel 2013-11-06 11:30:02