windows 如何使用 MAPI 发送邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7792070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a mail using MAPI?
提问by Lundin
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?
我想使用用户 Windows 计算机上的邮件客户端发送电子邮件。据我从网上可以看出,MAPI 是要走的路。但是,在阅读了 MSDN 文档后,我发现 MAPI 非常庞大,没有源代码示例。而且我不需要 99% 的功能,我只想发送电子邮件。我该怎么做呢?
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.
我在 SO 和网上找到了一些示例,但它们似乎依赖于称为 Simple MAPI 的东西,Microsoft 显然已将其列为已过时:“不鼓励使用 Simple MAPI。它可能会在后续版本中更改或不可用视窗”。所以我不想使用这些功能。
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.
我在这里找到了一个很好的例子,但不幸的是它适用于 Windows CE,并且与 Win32 API 不完全兼容。我设法实现了该链接中的代码,直到它到达尝试打开草稿文件夹的位置,GetProps 的参数不兼容。有谁知道我在哪里可以找到类似的 PC 代码示例?C++ 优先。
采纳答案by Lundin
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.
一旦代码经过正确测试和记录,我将尝试将其发布在这里以供将来参考。
回答by sashoalm
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 ).
我已经使用过一次 MAPI,我会发布代码。我不确定这是否是你要找的。这会发送一条带有可选附件的消息(但没有正文)。
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:
cp:
#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;
}