启动默认电子邮件客户端以打开“发送电子邮件”窗口与预选的文件附件 [英] Launch default email client to open a "send email" window with a pre-selected file attachment

查看:167
本文介绍了启动默认电子邮件客户端以打开“发送电子邮件”窗口与预选的文件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我们的应用添加创建和电子邮件功能。我们的程序创建一个输出文件,然后我必须启动默认的电子邮件客户端来打开一个写电子邮件窗口,并且输出文件被预先选择为附件。

I need to add a "Create and email" feauture to our app. Our program creates an output file, and I must then launch the default email client to open a "write email" window, and with the output file preselected as an attachment.

我已经看过其他程序,即使默认客户端是Thunderbird而不是Outlook。

I've seen other programs do it, even if the default client is Thunderbird instead of Outlook.

推荐答案

我最终使用MAPI来实现它。我使用LoadLibrary和GetProcAddress获取所需的函数。

I ended up using MAPI to achieve it. I used LoadLibrary and GetProcAddress to get the needed functions.

我使用的代码是:

bool MailSender::Send(HWND hWndParent, LPCTSTR szSubject)
{
    if (!m_hLib)
        return false;

    LPMAPISENDMAIL SendMail;
    SendMail = (LPMAPISENDMAIL) GetProcAddress(m_hLib, "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 = (LPSTR) ii->path.c_str();
        fileDesc.lpszFileName = (LPSTR) ii->name.c_str();
        filedesc.push_back(fileDesc);
    }

    std::string subject;
    if (szSubject)
        subject = utf16to8(szSubject).c_str();
    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 = (LPSTR) 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;
}

这篇关于启动默认电子邮件客户端以打开“发送电子邮件”窗口与预选的文件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆