English 中文(简体)
如何以编程方式发送电子邮件,就像我可以在Windows资源管理器中“发送到邮件收件人”一样?
原标题:
  • 时间:2008-11-04 16:52:28
  •  标签:

ShellExecute()允许我执行简单的shell任务,让系统负责打开或打印文件。我想以类似的方式编程发送电子邮件附件。

我不想直接操纵Outlook,因为我不想假设用户默认使用哪个电子邮件客户端。我也不想直接发送电子邮件,因为我希望用户有机会使用他们 prefer 来编写电子邮件正文。因此,我真的想要实现的就是当我右键单击文件并选择发送到 - > 邮件收件人时,Windows资源管理器所做的事情。

我正在寻找一个C++解决方案。

最佳回答

这是我的MAPI解决方案:

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

        lpfnMAPIInitialize = (LPMAPIINITIALIZE)GetProcAddress( hMapiModule, "MAPIInitialize" );
        lpfnMAPIUninitialize = (LPMAPIUNINITIALIZE)GetProcAddress( hMapiModule, "MAPIUninitialize" );
        lpfnMAPILogonEx = (LPMAPILOGONEX)GetProcAddress( hMapiModule, "MAPILogonEx" );
        lpfnMAPISendDocuments = (LPMAPISENDDOCUMENTS)GetProcAddress( hMapiModule, "MAPISendDocuments" );

        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 with "C:attachment.txt" as an attachment
                    hr = (*lpfnMAPISendDocuments)( 0, ";", "C:\attachment.txt", NULL, NULL );

                    if ( SUCCEEDED( hr ) )
                    {
                        hr = lplhSession->Logoff( 0, 0, 0 );
                        hr = lplhSession->Release();
                        lplhSession = NULL;
                    }
                }
            }

            (*lpfnMAPIUninitialize)();
        }

        FreeLibrary( hMapiModule );
    }

    return 0;
}
问题回答

您可以在Windows shell中使用标准的“mailto:”命令。它将运行默认的邮件客户端。

以下C++示例显示如何调用Windows资源管理器使用的“SendTo”邮件快捷方式:

请将此翻译为中文:http://www.codeproject.com/KB/shell/sendtomail.aspx http://www.codeproject.com/KB/shell/sendtomail.aspx

你需要实现一个MAPI客户端。这将允许您在向用户发送消息之前预填文档、添加附件等。您可以使用默认消息存储来使用他们的默认邮件客户端。





相关问题
热门标签