如何使用 MAPI 发送邮件?

发布于 2024-12-10 11:07:39 字数 513 浏览 0 评论 0原文

我想使用用户 Windows 计算机上的邮件客户端发送电子邮件。据我从网上得知,MAPI 是可行的方法。然而,在阅读MSDN文档后,我发现MAPI相当庞大,没有源代码示例。而且我不需要99%的功能,我只想发送一封电子邮件。我该怎么做?

我在 SO 和网络上找到了示例,但它们似乎依赖于一种称为 Simple MAPI 的东西,微软显然已将其列为过时的:“不鼓励使用 Simple MAPI。它可能会在后续版本中更改或不可用视窗”。所以我不想使用这些功能。

我找到了一个很好的例子这里 ,但不幸的是它是针对 Windows CE 的,与 Win32 API 不完全兼容。我设法从该链接实现代码,直到它到达尝试打开草稿文件夹的位置,GetProps 的参数不兼容。有谁知道在哪里可以找到类似的 PC 代码示例? C++ 优先。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女山的月亮 2024-12-17 11:07:39

在各种互联网资源的帮助下,我自己解决了这个问题。

官方 MSDN 文档

MAPIEx:扩展 MAPI 包装器

记录下来,我会尝试将其发布在这里以供将来参考。

I have solved this by my own, with the help from various internet sources.

Official MSDN documentation

MAPIEx: Extended MAPI Wrapper

Once the code is properly tested & documented, I will try to post it here for future reference.

小兔几 2024-12-17 11:07:39

请参阅使用 MAPI - 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文