如何以与在 Windows 资源管理器中“发送给邮件收件人”相同的方式以编程方式发送电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/262451/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:33:15  来源:igfitidea点击:

How do I programmatically send an email in the same way that I can "Send To Mail Recipient" in Windows Explorer?

c++windowsemailshell

提问by KRFournier

ShellExecute() allows me to perform simple shell tasks, allowing the system to take care of opening or printing files. I want to take a similar approach to sending an email attachment programmatically.

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

I don't want to manipulate Outlook directly, since I don't want to assume which email client the user uses by default. I don't want to send the email directly, as I want the user to have the opportunity to write the email body using their preferred client. Thus, I really want to accomplish exactly what Windows Explorer does when I right click a file and select Send To -> Mail Recipient.

我不想直接操作 Outlook,因为我不想假设用户默认使用哪个电子邮件客户端。我不想直接发送电子邮件,因为我希望用户有机会使用他们首选的客户端编写电子邮件正文。因此,当我右键单击一个文件并选择“发送到”->“邮件收件人”时,我真的很想完成 Windows 资源管理器所做的工作。

I'm looking for a C++ solution.

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

采纳答案by Jeff Hillman

This is my MAPI solution:

这是我的 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;
}

回答by TcKs

You can use a standard "mailto:" command in windows shell. It will run the default mail client.

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

回答by Craig Lebakken

The following C++ example shows how to invoke the SendTo mail shortcut used by Windows Explorer:

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

http://www.codeproject.com/KB/shell/sendtomail.aspx

http://www.codeproject.com/KB/shell/sendtomail.aspx

回答by Eclipse

You'll need to implement a MAPI client.This will let you prefill the document, add attachments, etc.. before presenting the message to the user to send off. You can use the default message store to use their default mail client.

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