Reputation: 213832
I want to send an e-mail using the mail client on the user's Windows computer. As far as I can tell from the net, MAPI is the way to go. However, after reading through the MSDN documentation, I find out that MAPI is quite vast, with no source code examples. And I have no need for 99% of the features, I just want to send an e-mail. How do I do this?
I have found examples here on SO and on the web, but they seem to rely on something called Simple MAPI, which Microsoft has apparently listed as obsolete: "The use of Simple MAPI is discouraged. It may be altered or unavailable in subsequent versions of Windows". So I don't want to use those functions.
I found a very good example here, but unfortunately it is for Windows CE and isn't fully compatible with the Win32 API. I managed to implement the code from that link until it got to where it attempts to open the drafts folder, the parameters to GetProps aren't compatible. Does anyone know where I can find a similar code example for PC? C++ prefered.
Upvotes: 3
Views: 18550
Reputation: 213832
I have solved this by my own, with the help from various internet sources.
Once the code is properly tested & documented, I will try to post it here for future reference.
Upvotes: 2
Reputation: 79467
See Sending Email using MAPI - A COM DLL
[Edit]
I've used MAPI once and I'll post the code. I'm not sure if it's what you're looking for. This sends a message with optionally attached files ( but no body ).
Header:
#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;
}
Upvotes: 0