4

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.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • You can use ShellExecute to launch the email client with the recipient address (see the link below) but don't know how to inform the client about the attachement. http://stackoverflow.com/questions/3863231/easy-way-to-invoke-standard-mail-client-from-c-with-recipient-adress-and-subjec/3863259#3863259 – Christian Ammer Jan 27 '11 at 19:44

3 Answers3

4

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;
}
sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • I need to perform the same activity where I launch the default mail client with an attachment. Could you mention all the headers that you are using ? or the complete prototype? Thanks – Teja Jul 19 '16 at 15:30
  • I couldn't find the original project, I've probably lost it. – sashoalm Jul 23 '16 at 11:46
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.

Community
  • 1
  • 1
Eugen Constantin Dinca
  • 8,994
  • 2
  • 34
  • 51
  • Thank you, but mailto doesn't support file attachments. I tested a third party app that has this feature. They are calling Thunderbird with this command line: ""C:\Program Files\Mozilla Thunderbird\thunderbird.exe" /MAPIStartup -Embedding". So they seem to be using MAPI to achieve the results. I'll try to investigate that first. – sashoalm Jan 28 '11 at 08:25
  • @satuon: with the `mailto` I was thinking to including the MIME encoded attachment in the body field but it will be at least tricky and very likely to not work at all. Check out the links in the answer for alternative command line options for Thunderbird & Outlook. – Eugen Constantin Dinca Jan 28 '11 at 08:36
0

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

DAC84
  • 421
  • 1
  • 8
  • 20