E-Mail File Attachment Using MIME (with HTML support) (Plus Authentication)


This is an update of pcouderc's original article adding the ability to connect to a server with authentication, sending the user and password when stablishing the connection.

When a codeguru "afficionado" wants to send automatic mail or needs a mailer for any reason, he may use the classes designed by Wes Clyburn under the title E-Mail file attachment using MIME. See there, where there is a good description of MIME mechanisms and a description of the classes used.

Anyway, there was the need for an update to these classes to add a few more features:

  • Use of HTML text with images.
  • Optimisation of speed due to the use of long strings.
  • Support of CC and BCC.
  • Progess notification for long transfers.
  • Support of BASE64 coding directly from memory (and not only from a file).

With these updates, a nearly full compatibility had been kept. "Nearly" means that the compatibility is at the source level, but the numeric values of some enums has been changed.


  • Unzip the temail project.
  • Update MYSELF, MAILSERVER, and FROM defines in temail.cpp to your own addresses
  • Compile, build and execute it
You will receive five HTML emails with image and attached file, or not.


A mail message may contain one or many elementary "parts" : HTML text, GIF image, attached file...

All mail messages are not MIME, but...

A MIME mail message (CMIMEMessage) is considered as having a main MIME part(CMIMEPart), and only one. Some MIME parts are considered as "containers" of some other "elementary" MIME parts. This was not the case with the original classes of Wes, where elementary MIME parts where added to the message itself: the concept of "container" was implicit but not formalized.

The containers are of type MIXED, ALTERNATIVE, or RELATED (Mmm... you are allowed to ignore all these kind of containers and simply copy the example).

The elementary MIME parts supported are of type TEXT_PLAIN, TEXT_HTML, APPLICATION_OCTETSTREAM (for attachment), APPLICATION_OCTETSTREAM_IMAGE (for embedded images)

Well, this should be enough to see an example.


void TestHTMLMailWithGifWithAttach()
  // Create and initialize a message
  CMIMEMessage *pMsg= new CMIMEMessage;
  pMsg->m_sFrom = FROM;
  pMsg->m_sSubject = "Test CMIMEmessage";
  // Create MIME containers
  CMIMEMessage::CMIMEPart *pMIMEmixed =
  CMIMEMessage::CMIMEPart *pMIMErelated =
  CMIMEMessage::CMIMEPart *pMIMEalternative =
  // Alternative 1 : mail client does not support HTML...

  //      tell it in plain text 7Bits (warning : no
  //      conversion is done)
  CString Text(
"Text that appear when client does not support HTML\r\n\r\n");
  // Alternative 2 : mail client does support HTML...
  //      tell it in HTML text quoted-printable (warning :
  //      no conversion is done)
  CString Html;
  // Prepare GIF image

  char* Gif;
  int Len;
  GetGIFResource(IDR_TOLLOGO, &Gif, &Len);
  // GIFS are related to HTML text : note
  //    the string "IDR_TOLLOGO" which appears somewhere
  //    in IDR_HTML1 text
  GetGIFResource(IDR_HR, &Gif, &Len);
  // Add attachment

  // Do not forget to...
  // Then ...

This example should be enough to solve most of the complex cases. On the contrary, simpler cases are possible. See temail.cpp for simpler examples.


Calling AddMIMEPart()

The parameters of AddMIMEPart() are:
  CMIMEPart* AddMIMEPart(eMIMETypeCode nContentType,
     LPCTSTR szContent= NULL,
     eMIMEEncodingCode nEncoding = DEFAULT,
     LPCTSTR szParameters = NULL,
     int Len=0);
  • nContentType is one of the values seen in the overview.
  • szContent may be the contents of the source of data or a file path to the source of data.
  • nEncoding is one of the following basic values: DEFAULT,_7BIT, QUOTED_PRINTABLE, BASE64. DEFAULT is very fine, but you can try other values. Please note that only BASE64 does effective encoding.
    Other bits may be or'ed with the basic value:
    • MEMORY or FILE indicate the source of data. In the case of FILE szContent contains the file name, else it contains the data itself.
    • SOONCODED and ENCODE indicate if data is soon coded or must be coded by AddMIMEPart.
    Please note the following limitations :
    • In TEXT_PLAIN and TEXT_HTML, BASE64, FILE and ENCODE are not implemented.
    • In APPLICATION_OCTETSTREAM and APPLICATION_OCTETSTREAM_IMAGE, the only implemented basic valueis BASE64. Moreover, SOON_ENCODED is not implemented.
    Please note the default values (when nEncoding is left to DEFAULT):
  • szParameters may be used to add some text after the Content-Type except in the case of APPLICATION_OCTETSTREAM or APPLICATION_OCTETSTREAM_IMAGE. In this last case, it contains an identifier string for the image (cf IDR_TOLLOGO in the example).
  • Len indicates the length of szContent. In case of 0, a strlen is done.

Progress notification

Derive you own class from CMIMEmessage and override NotifyProgress(). For a basic example search CNotifiedMIMEMessage in temain.c.

To do

  • Test better : the mailer has been tested only with Outlook express 5.
  • No conversion are done except binary BASE64 for attached files and images.
  • Credits

    These classes come directly from the work of Wes Clyburn and pcouderc.



    • Interesting

      Posted by snareenactina on 11/06/2012 04:43pm

      There is increasing investment from China, which has already built hospitals, schools, and roads and is a potential investor in the tourism industry. derogations Yes because it was in rude health when the government inherited from Labour wasn't it?! erased tortsi swpms observ creciente

    • connect call to smtp.live.com returns false

      Posted by krshn_m on 09/28/2009 09:37am

      Hey Goa, Could you please help me out in comming out of this issue, "not able to connect to smtp.live.com". Thanks, krshn_m

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • More than ever, agile Software Life Cycle Development and DevOps drives critical top-line business impact for customers across a broad range of industries. Learn how XtremIO is fundamentally enabling the next generation of agile Software Development & DevOps to: Radically improve developer efficiency and overall product quality via an All-Flash Dev Ops platform Enable modern lightning fast dev methodologies with zero-cost application & datasets full copies. Allow Developers to have zero storage obstacles, …

    • Rocket Mobile® for IBM i is an enterprise mobile application development and deployment platform for customers who rely on the IBM i system. Rocket Mobile for IBM i enables customers to leave proven applications in-place and rapidly repurpose them into new managed and secure mobile applications. Fast, easy creation of mobile, web, and hybrid mobile applications that deploy to any iOS, Android, or Windows mobile phone or tablet Built-in integration for seamless repurposing of existing IBM i applications …

    Most Popular Programming Stories

    More for Developers

    RSS Feeds

    Thanks for your registration, follow us on our social networks to keep up-to-date