見Sending Email using MAPI - A COM DLL
[編輯]
我使用MAPI一次,我會後的代碼。我不確定這是你在找什麼。這會發送帶有可選附加文件的消息(但不包含主體)。
頁眉:
#pragma once
class MailSender
{
public:
MailSender();
~MailSender();
void AddFile(LPCTSTR path, LPCTSTR name = NULL);
bool Send(HWND hWndParent, LPCTSTR szSubject = NULL);
private:
struct attachment { tstring path, name; };
vector<attachment> m_Files;
HMODULE m_hLib;
};
.cpp的:
#include "stdafx.h"
#include "MySendMail.h"
#include <mapi.h>
MailSender::MailSender()
{
m_hLib = LoadLibrary(_T("MAPI32.DLL"));
}
MailSender::~MailSender()
{
FreeLibrary(m_hLib);
}
void MailSender::AddFile(LPCTSTR file, LPCTSTR name)
{
attachment a;
a.path = file;
if (!name)
a.name = PathFindFileName(file);
else
a.name = name;
m_Files.push_back(a);
}
bool MailSender::Send(HWND hWndParent, LPCTSTR szSubject)
{
if (!m_hLib)
return false;
LPMAPISENDMAIL SendMail;
SendMail = (LPMAPISENDMAIL) GetProcAddress(m_hLib, _T("MAPISendMail"));
if (!SendMail)
return false;
vector<MapiFileDesc> filedesc;
for (std::vector<attachment>::const_iterator ii = m_Files.begin(); ii!=m_Files.end(); ii++)
{
MapiFileDesc fileDesc;
ZeroMemory(&fileDesc, sizeof(fileDesc));
fileDesc.nPosition = (ULONG)-1;
fileDesc.lpszPathName = (LPTSTR) ii->path.c_str();
fileDesc.lpszFileName = (LPTSTR) ii->name.c_str();
filedesc.push_back(fileDesc);
}
tstring subject;
if (szSubject)
subject = szSubject;
else
{
for (std::vector<attachment>::const_iterator ii = m_Files.begin(); ii!=m_Files.end(); ii++)
{
subject += ii->name.c_str();
if (ii+1 != m_Files.end())
subject += ", ";
}
}
MapiMessage message;
ZeroMemory(&message, sizeof(message));
message.lpszSubject = (LPTSTR) subject.c_str();
message.nFileCount = filedesc.size();
message.lpFiles = &filedesc[0];
int nError = SendMail(0, (ULONG)hWndParent, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
return false;
return true;
}
如果您安裝了Outlook或其他MAPI客戶端,則僅在Windows計算機上存在完整的MAPI。 Windows只帶有簡單的MAPI。如果您知道您已安裝Outlook,爲何不使用Outlook的API發送電子郵件? –
這個想法是使用他們擁有的任何MAPI客戶端,我寧願不強迫人們使用特定的電子郵件客戶端。但出於好奇,Outlook API究竟是什麼?是VBA還是其他? – Lundin
如果你不想強迫某人使用特定的電子郵件客戶端,那麼你可能會被簡單的mapi所困住。如果他們使用網絡郵件,它將無法工作,但大多數電子郵件客戶端都支持簡單的MAPI。點擊此處查看:http://msdn.microsoft.com/en-us/library/gg608200.aspx瞭解outlook的apis信息。 –