Sending an email attachment from default email client

Posted on 2005-05-02
Last Modified: 2010-04-01
I'm developing a desktop application in C++ (using MS Visual stdio 6.0) and I have from following requirement:

 - the application must launch the default email client on the user's PC to send an email (easy using ShellExecuteEx)
 - it must then attach a binary file to the message (this seems to be the hard part!).
 - it needs to work for all the popular email clients (eg. Outlook Express, Eudora, MS Outlook etc)

I've seen several threads already that show how to program using the SMTP protocol to send the message with an attachment directly to the server, but this is NOT what I want do do. I also don't want the user to have to re-enter the the server address/authentication password etc in my program - after all, they've already configured their email client for this once!.

Thanks in advance for any help with this.
Question by:Schmapper
    LVL 86

    Accepted Solution

    You can do that quite easily using "Simple MAPI" - see e.g. ("Handling Attachments with Simple MAPI") and ("Sending a Message Simply: Simple MAPI Sample"):

    // Example 1:
    // Send a mail message containing a file and prompt for
    // recipients, subject, and note text.

    ULONG err;
    MapiFileDesc attachment = {0,         // ulReserved, must be 0
                               0,         // no flags; this is a data file
                               (ULONG)-1, // position not specified
                               "c:\\tmp\\tmp.wk3",  // pathname
                               "budget17.wk3",      // original filename
                               NULL};               // MapiFileTagExt unused
    // Create a blank message. Most members are set to NULL or 0 because
    // MAPISendMail will let the user set them.
    MapiMessage note = {0,            // reserved, must be 0
                        NULL,         // no subject
                        NULL,         // no note text
                        NULL,         // NULL = interpersonal message
                        NULL,         // no date; MAPISendMail ignores it
                        NULL,         // no conversation ID
                        0L,           // no flags, MAPISendMail ignores it
                        NULL,         // no originator, this is ignored too
                        0,            // zero recipients
                        NULL,         // NULL recipient array
                        1,            // one attachment
                        &attachment}; // the attachment structure
    err = MAPISendMail (0L,          // use implicit session.
                        0L,          // ulUIParam; 0 is always valid
                        &note,       // the message being sent
                        MAPI_DIALOG, // allow the user to edit the message
                        0L);         // reserved; must be 0
    if (err != SUCCESS_SUCCESS )
        printf("Unable to send the message\n");

    (You'll have to 'MAPIInitialize()' before)
    LVL 86

    Expert Comment

    OH, BTW, you might find ("Sending and receiving e-mails from your program") or ("Another simple MAPI class") to come in handy.

    Author Comment

    Thanks jkr.

    The MAPI solution certainly seems to work with my current email client (Eudora). Still need to check it out with MS Outlook and Outlook Express also and if these are successful, then I'll accept your solution. One thing I found was that I didn't need to call MAPIInitialise but did need to dynamically load in the MAPI32 dll to access the MAPISendMail function. Do you happen to know about the availability of the MAPI32 dll? Would it have been shipped with all Window O/S versions going back to, say, Windows 95, for example? Just need to determine whether I would need to bundle it with my application or not?
    LVL 86

    Expert Comment

    Both OL and OE will work with MAPI, I am not really worried about that.

    >>Do you happen to know about the availability of the MAPI32 dll?

    Check for detailed info on that, but MAPI certainly is available on all platforms that are able to run Outlook Express :o)

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now