如何使用 C++ 中的 MAPI 以编程方式向已知收件人发送带附件的电子邮件? MAPIS发送邮件()

发布于 2024-08-23 04:59:49 字数 3888 浏览 9 评论 0原文

这个问题类似,但没有显示如何添加收件人。

我该如何做到这两点?

我们希望为尽可能多的 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;
}

This question is similar, but does not show how to add a recipient.

How do I do both?

We'd like the widest support possible for as many Windows platforms as possible (from XP and greater)

We're using visual studio 2008

Essentially we want to send an email with:

  • pre-filled destination address
  • file attachment
  • subject line

from our program and give the user the ability to add any information or cancel it.

EDIT
I am trying to use MAPISendMail()
I copied much of the code from the questions linked near the top, but I get no email dlg box and the error return I get from the call is: 0x000f - "The system cannot find the drive specified"

If I comment out the lines to set the recipient, it works fine (of course then I have no recipient pre-filled in)

Here is the code:

#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;
}

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

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

发布评论

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

评论(1

半透明的墙 2024-08-30 04:59:49

设置为“工作正常”。

recip.ulRecipClass = MAPI_TO;

哎呀 - 我忘了现在

Oops - I forgot to set

recip.ulRecipClass = MAPI_TO;

Works great now.

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