sashoalm
sashoalm

Reputation: 79665

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

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.

Upvotes: 4

Views: 6143

Answers (3)

DAC84
DAC84

Reputation: 489

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

Upvotes: 0

sashoalm
sashoalm

Reputation: 79665

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

Upvotes: 4

Eugen Constantin Dinca
Eugen Constantin Dinca

Reputation: 9150

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.

Upvotes: 2

Related Questions