Launch default email client to open a “send email” window with a pre-selected file attachment

不问归期 提交于 2019-12-06 04:14:32

问题


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.

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


回答1:


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

The code I used is this:

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



回答2:


Using the mailto scheme may be a solution but it's going to be tricky due to restrictions on what fields are considered safe (see the RFC 2368 and 6067 for the full details if you want to go that route).

Another solution would be to figure out what email client is installed and - wherever possible - launch it and specify all you need via command line. See here for Thunderbird & here for Outlook.




回答3:


You can use the following command to start the start the default client app with attachment

"Path to default mail client.exe" -mail -compose subject='Subject',attachment='File path',body='body'"

Path to default mail client-> can be taken from registry path

HKEY_LM\SOFTWARE\Clients\Mail\Email Client Name\shell\open\command

Mail Client Name -> can be taken from

HKEY_LM\Software\Clients\Mail



来源:https://stackoverflow.com/questions/4819450/launch-default-email-client-to-open-a-send-email-window-with-a-pre-selected-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!