Attach an Image File to Outlook Message

Universal Document Converter is the software based on virtual printing. It can save any document you print as a raster PDF or as an image file. You can use a post-print feature of Universal Document Converter to apply additional processing to every output file. The example below is just one of many post-print processing solutions.

// Microsoft Office Outlook 2000 or above should be installed
//
// 1) Open your project in Microsoft Visual C++ 6.0
//
// 2) Press "Ctrl+W" in Visual C++ to open ClassWizard
//
// 3) In ClassWizard window press "Add Class->From a type library"
//    button and select "MSOUTL.OLB" file.
//
// 5) Save files "msoutl.h" and "msoutl.cpp" and add into
//    your "stdafx.h" file this include: #include "msoutl.h"
//
// 6) You must initialize the COM before you call any COM method.
//    Please call "::CoInitialize(0);" before using COM and
//    ::CoUninitialize(); after using COM.

#include "msoutl.h"
#include <comdef.h>

enum OlAttachmentType
{
  olByValue = 1,
  olByReference = 4,
  olEmbeddedItem = 5,
  olOLE = 6
};

enum OlDefaultFolder
{
  olFolderDeletedItems = 3,
  olFolderOutbox = 4,
  olFolderSentMail = 5,
  olFolderInbox = 6,
  olFolderCalendar = 9,
  olFolderContacts = 10,
  olFolderJournal = 11,
  olFolderNotes = 12,
  olFolderTasks = 13,
  olFolderDrafts = 16
};

enum OlItemType
{
  olMailItem = 0,
  olAppointmentItem = 1,
  olContactItem = 2,
  olTaskItem  = 3,
  olJournalItem = 4, 
  olNoteItem = 5,
  olPostItem = 6
};

void CreateMsg(char* subj, char* body, char* email, char* filePath)
{
  _Application* app = new _Application;
  app->CreateDispatch( "Outlook.Application" );
////
  _MailItem msg = app->CreateItem( olMailItem );
  msg.SetTo( email );
  msg.SetSubject( subj );	
////
  Attachments atts = msg.GetAttachments();
  
  CString sFileName = filePath;
  sFileName = sFileName.Mid( sFileName.ReverseFind('\\') + 1 );

  atts.Add( _variant_t(filePath), _variant_t( (long)olByValue ),\
            _variant_t("1"), _variant_t(sFileName) );
////
  msg.SetBody( body );
// Show message modal and wait when user send or close it
  msg.Display( _variant_t( (bool)1 ) );

// Show Outlook for message sending
  _NameSpace ns = app->GetNamespace("MAPI");
  MAPIFolder folder = ns.GetDefaultFolder( olFolderInbox );
  folder.Display();
}


  • Thomas Vass

    Owner of The Private Capital Market Crowd Funding Website

    «I have used Universal Document Converter since the very first versions as my sole tool for making document conversions. It works flawlessly and is very user-friendly. I recommend the product for all business applications.»